Chrome扩展程序执行脚本不会呈现浏览器内容值

时间:2017-11-06 09:15:16

标签: javascript google-chrome-extension

我想要做的就是根据特定URL更改popup.html的值。所以当用户点击popup.html中H2的更改ID时,我会向background.js发送消息,使用特定的JavaScript文件呈现内容

的manifest.json

{
    "manifest_version": 2,
    "name": "Helpfullio",
    "version": "0.1",
    "content_scripts": [{
        "matches": [
          "*://google.com/*/*",
        ],
        "js": ["jquery-3.2.1.js"]
    }],
    "browser_action": {
        "default_popup": "popup.html"
    },
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": ["tabs","http://*/*", "https://*/*"]
}

popup.html

<html>
<head>
</head>
    <body>
    <h2 id="change">Change ___________</h2> 
    <script src="popup.js"></script>
</body>
    </html>

popup.js

    function clickHandler(e) {
        chrome.runtime.sendMessage({directive: "popup-click"}, function(response) {
            // this.close(); finishes processing request
        });
    }


    document.addEventListener('DOMContentLoaded', function () {
        document.getElementById('change').addEventListener('click', clickHandler);
    })

background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        switch (request.directive) {
        case "popup-click":
          chrome.tabs.query({
            'active': true, 'lastFocusedWindow': true
            }, function (tabs) {
                var url = tabs[0].url;
                var result = url.split('/');
                var hostname = result[2];
                if("bitlock" == hostname){
                  chrome.tabs.executeScript(null, { 
                      file: "render.js",
                      // allFrames: true
                  });
                }else{
                   hrome.tabs.executeScript(null, { 
                      file: "secondrender.js",
                      // allFrames: true
                  }); 
                }
            sendResponse({});
          });
        break;

        default:
            alert("Unmatched request of '" + request + "' from script to background.js from " + sender);
        }
    }
);;

JavaScript没有呈现popup.html的内容,因为它没有找到elementID&#34;更改&#34; 。那么我怎么能在render.js文件中引用popup.html文件

render.js

document.getElementById("change").textContent = 'new text';

0 个答案:

没有答案