1

我创建了一个 chrome 扩展,我想在源代码当前页面中附加一个 javascript 文件。我写了这段代码,但不起作用。事实上,我可以在源代码当前页面中附加一个标签,如 javascript,但它不起作用,我接受了这个错误

Denying load of chrome-extension://lmainnigamjlkokflgjdkjdjbphifefb/remove2.js?_=1372832784584. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.
GET chrome-extension://invalid/  
Failed to load resource 

这是我的代码:

清单.json

{
    "manifest_version" : 2,
    "name" : "Get URL",
    "description" : "This is extension test-2",
    "version" : "1.0",

    "permissions" : ["tabs"],

    "browser_action" : {
        "default_title": "This is extension test-2",
        "default_icon": "icon.png",
        "default_popup" : "popup.html"
    },
    "content_scripts": [
        {
          "matches": [ "http://*/*", "https://*/*" ],
          "js": ["jquery.js", "content.js"]
        }
    ],

    "icons" : {
        "19": "icon.png"
    }
}

内容.js

insertTooltip();

function insertTooltip () {

    var ic = chrome.extension.getURL('remove2.js');
    console.log(ic);
    var tooltip = '<script type="text/javascript" src="'+ic+'"></script>';
    console.log(tooltip);
    $('body').append(tooltip);
};

popup.html

<html>
    <head>
        <script src="get.js"></script>
        <script src="content.js"></script>
    </head>
    <body>
    <div id="show"></div>

    </body>
</html>

获取.js

var currentURL;
chrome.tabs.getSelected(null, function(tab) { //<-- "tab" has all the information
    currentURL = tab.url       //<-- return the url.
    //console.log(currentURL)
    if(currentURL)
    {
        //alert(currentURL);
        var div = document.getElementById('show');
        div.innerHTML = currentURL;
    }
});

请指导我......

4

1 回答 1

1

您需要将 remove2.js 脚本添加到清单中。

{
  ...
  "web_accessible_resources": [
    "remove2.js"
  ],
  ...
}

更多信息在这里

于 2013-07-03T06:55:24.287 回答