Chrome扩展-background.js需要什么

问题描述 投票:0回答:1

我正在尝试安装一个小 chrome 扩展以进行测试。该功能工作正常,但在扩展本身中却不行。我认为问题是background.js。我不确定代码的哪一部分必须在其中。希望有人可以帮助我。

这是到目前为止的代码。 清单:

{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "permissions": ["activeTab", "declarativeContent", "storage"],
  "options_page": "options.html",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/16x16.png",
      "19": "images/19x19.png",
      "38": "images/38x38.png",
      "128": "images/128x128.png"
    }
  },
  "icons": {
    "16": "images/16x16.png",
    "19": "images/19x19.png",
    "38": "images/38x38.png",
    "128": "images/128x128.png"
  },
  "manifest_version": 2
}

popup.html:

<!DOCTYPE html>
<html>
  <head>
    <style>
      button {
        height: 30px;
        width: 30px;
        outline: none;
      }
    </style>
  </head>
  <body>
        <span id="params"></span>
    <pre id="results">{}</pre>
    <script src="popup.js"></script>
  </body>
</html>

popup.js:

 function getAllUrlParams(url) {
  var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
  var obj = {};

  if (queryString) {
    queryString = queryString.split('#')[0];
    var arr = queryString.split('&');

    for (var i = 0; i < arr.length; i++) {
      var a = arr[i].split('=');
      var paramName = a[0];
      var paramValue = typeof (a[1]) === 'undefined' ? true : a[1];

      if (paramName.match(/\[(\d+)?\]$/)) {
        var key = paramName.replace(/\[(\d+)?\]/, '');
        if (!obj[key]) obj[key] = [];

        if (paramName.match(/\[\d+\]$/)) {
          var index = /\[(\d+)\]/.exec(paramName)[1];
          obj[key][index] = paramValue;
        } else {
          obj[key].push(paramValue);
        }
      } else {
        if (!obj[paramName]) {
          obj[paramName] = paramValue;
        } else if (obj[paramName] && typeof obj[paramName] === 'string'){
          obj[paramName] = [obj[paramName]];
          obj[paramName].push(paramValue);
        } else {
          obj[paramName].push(paramValue);
        }
      }
    }
  }

  return obj;
}

var params = document.getElementById('params');
var results = document.getElementById('results');

url=window.location.href; 
params.innerText =url;  
results.innerText = JSON.stringify(getAllUrlParams(url), null, 2);

最后但并非最不重要的background.js(这只是入门包中的代码,没有任何改变,因为我不知道到底要在里面发布什么):

  chrome.runtime.onInstalled.addListener(function() {
  chrome.storage.sync.set({color: '#3aa757'}, function() {
    console.log('The color is green.');
  });
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [new chrome.declarativeContent.PageStateMatcher({
        pageUrl: {hostEquals: 'developer.chrome.com'},
      })],
      actions: [new chrome.declarativeContent.ShowPageAction()]
    }]);
  });
});

javascript google-chrome-extension
1个回答
0
投票

{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "permissions": ["activeTab", "declarativeContent", "storage"],
  "options_page": "options.html",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/16x16.png",
      "19": "images/19x19.png",
      "38": "images/38x38.png",
      "128": "images/128x128.png"
    }
  },
  "icons": {
    "16": "images/16x16.png",
    "19": "images/19x19.png",
    "38": "images/38x38.png",
    "128": "images/128x128.png"
  },
  "manifest_version": 2
}

© www.soinside.com 2019 - 2024. All rights reserved.