【问题标题】:Run Content Script From Toolbar从工具栏运行内容脚本
【发布时间】:2013-11-13 10:51:27
【问题描述】:

我想要一个 Chrome 扩展程序来替换页面上的文本。我已经让所有代码都在 Javascript 方面工作,并且它在页面加载时运行完美,问题是我只希望它在您单击工具栏上的按钮时替换页面上的文本。

我在工具栏上设置了一个按钮,但替换的 Javascript 仍然只是在页面加载时运行,而不是在您单击按钮时运行。此外,当您单击工具栏按钮时,尽管它没有执行任何操作,但它仍然会显示一个弹出窗口。我要做的就是在单击工具栏按钮时运行文本替换代码,而不显示 popup.html 框。

目前代码如下,

Manifest.json

{
  "name": "Browser Action",
  "version": "0.0.1",
    "manifest_version": 2,
  "description": "Show how options page works",
  // Needed to retrieve options from content script
  "background": "background.html",

  // This is how you load Browser Action. Nearly equal to Page one.
  "browser_action": {
      "default_icon": "icon.png",
      "popup": "popup.html"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js" : ["popup.js"]
    }
  ]
}

popup.js

function htmlreplace(a, b, element) {    
    if (!element) element = document.body;    
    var nodes = element.childNodes;
    for (var n=0; n<nodes.length; n++) {
        if (nodes[n].nodeType == Node.TEXT_NODE) {
            var r = new RegExp(a, 'gi');
            nodes[n].textContent = nodes[n].textContent.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}

htmlreplace('a', 'IT WORKS!!!');

popup.html - 空白

background.html

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

【问题讨论】:

    标签: javascript html google-chrome google-chrome-extension browser-extension


    【解决方案1】:

    必须进行一些更改(rsanchez 提到了其中大部分 - 但不是全部),还有一些可以/应该进行的更改。

    因此,我将演示一个示例扩展,而不是列出可以/应该/必须更改的内容。


    首要任务 - 有关与您的问题/问题相关的几个关键概念的更多信息:


    扩展目录结构:

              extension-root-directory/
               |_____manifest.json
               |_____background.js
               |_____content.js
    

    ma​​nifest.json:

    {
        "manifest_version": 2,
        "name":    "Test Extension",
        "version": "0.0",
        "offline_enabled": true,
    
        "background": {
            "persistent": false,
            "scripts": ["./bg/background.js"]
        },
    
        "browser_action": {
            "default_title": "Test Extension"
            //"default_icon": {
            //    "19": "img/icon19.png",
            //    "38": "img/icon38.png"
            //},
        },
    
        "permissions": [
            "activeTab"
        ]
    }
    

    background.js:

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

    content.js:

    function htmlReplace(a, b, element) {
        if (!element) {
            element = document.body;
        }
    
        var r = new RegExp(a, "gi");
        var nodes = element.childNodes;
        for (var n = 0; n < nodes.length; n++) {
            if (nodes[n].nodeType == Node.TEXT_NODE) {
                nodes[n].textContent = nodes[n].textContent.replace(r, b);
            } else {
                htmlReplace(a, b, nodes[n]);
            }
        }
    }
    htmlReplace("a", "IT WORKS !!!");
    

    【讨论】:

      【解决方案2】:

      您只需对清单进行以下更改:

      • 删除content_scripts 部分。
      • 删除browser_action.popup 条目。
      • 添加栏目:"permissions": ["activeTab"]
      • background 部分更改为:"background": { "scripts": ["background.js"] } 并将文件background.html 重命名为background.js

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-24
        • 1970-01-01
        • 1970-01-01
        • 2015-08-19
        • 2016-09-09
        • 2016-06-09
        • 1970-01-01
        • 2017-02-21
        相关资源
        最近更新 更多