Chrome扩展程序的内容脚本不会影响特定网站

时间:2018-01-12 08:49:35

标签: javascript json google-chrome google-chrome-extension content-script

我有一个chrome扩展程序,它使用内容脚本将特定单词注入网页。它适用于某个网站x,但在y中不起作用。

清单

 {
  "name": "test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "word",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": true
  },
  "browser_action": {
    "default_title": "chat"
  },
  "permissions": [
    "https://*/*",
    "http://*/*",
    "tabs"
  ]
}

Background.js

// listen for our browerAction to be clicked

    chrome.browserAction.onClicked.addListener(function (tab) {
        // for the current tab, inject the "inject.js" file & execute it
        chrome.tabs.executeScript(tab.ib, {
            file: 'inject.js'
        });
    });

Inject.js

// this is the code which will be injected into a given page...
(function() {
// just place a div at top right
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.top = 600;
div.style.right = 700;
div.textContent = 'Hello!';
document.body.appendChild(div);
})();

此代码适用于在网站中添加该字词:https://developer.chrome.com/extensions/examples/api/browserAction/make_page_red/background.js

在web.whatsapp.com和其他常见网站中不起作用。

1 个答案:

答案 0 :(得分:1)

刚刚将"*://*/*",添加到您的清单中并且工作正常。

的manifest.json

{
  "name": "test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "word",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": true
  },
  "browser_action": {
    "default_title": "chat"
  },
  "permissions": [
    "*://*/*",
    "tabs"
  ]
}

background.js

// listen for our browerAction to be clicked

    chrome.browserAction.onClicked.addListener(function (tab) {
        // for the current tab, inject the "inject.js" file & execute it
        chrome.tabs.executeScript(tab.ib, {
            file: 'inject.js'
        });
    });

inject.js

(function() {
    console.log("Inject successfully.");
    // just place a div at top right
    var div = document.createElement('div');
    div.style.position = 'fixed';
    div.style.top = 600;
    div.style.right = 700;
    div.style.zIndex = 9999;
    div.textContent = 'Hello!';
    document.body.appendChild(div);
})();

enter image description here