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

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

我有一个 chrome 扩展,它使用内容脚本将特定单词注入(inject)网页。它在某个网站 x 中有效,但在 y 中无效。

list

 {
  "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"
  ]
}

背景.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 以及其他常见网站。

最佳答案

刚刚将 "*://*/*", 添加到您的 list 中以使其正常工作。

list .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"
  ]
}

背景.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

关于javascript - Chrome扩展程序的内容脚本不会影响特定网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48222555/

相关文章:

javascript - 跨浏览器方式子类化 JavaScript Array 并获取 array[i] 方法?

java - 突出显示 JSF 数据表的行

javascript - 下拉列表脚本

javascript - Strongloop 中的基本身份验证

html - 使用 Google Chrome Web Inspector 更改页面布局

javascript - 如何在json2html中使用onclick

json - Angular2 从 JSON 解析为对象

json - 从 Handlebars 模板中的 JSON 数组中获取最后一个元素

google-chrome - 谷歌浏览器在控制台中不显示错误

exception - DATA_ERR : DOM IDBDatabase Exception 5 in Chrome, 如何修复?