0

i can't figure out how to make it work. My script works by itself. but doesn't work with background.js. I want my google extension to work only if the user clicks on it's icon, so I have created the file background.js and putted the code:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null, {file: "change_content.js"});
});

my manifest.json here:

{
  "manifest_version": 2,
  "name": "Name",
  "description": "change content.",
  "version": "3.0",


  "browser_action": {

    "default_icon": "icon.png"
  },
   "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["change_content.js"]
    }
  ],


    "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "permissions": [
  "tabs", "http://*/*"
]

}

and here is the change_content.js:

var oldSource = document.documentElement.innerHTML;
document.body.innerHTML = changeContent(oldSource);
function changeContent(source){
.....
}
4

2 回答 2

3

您在按下按钮之前遇到执行问题的原因change_content.js是因为这就是内容脚本的工作方式。如果您在其中包含内容脚本,manifest.json它将加载并执行该脚本。尝试"content_scripts"从清单中删除该部分,您应该会看到它正常工作。

于 2013-07-20T18:41:25.437 回答
-2
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null, {file: "change_content.js"});
});

我感觉错误在于您使用“null”,因为它可能正在搜索带有 tabId - null 的选项卡,您应该尝试这样做吗?

chrome.tabs.executeScript({file: "change_content.js"});
于 2013-07-20T16:54:20.810 回答