不在当前页面chrome扩展中执行附加的JavaScript标记

时间:2013-07-03 06:43:21

标签: google-chrome google-chrome-extension

我创建了一个chrome扩展,我想在源代码当前页面中附加一个javascript文件。我写这段代码但不起作用。 事实上,我可以在源代码当前页面中添加一个像javascript这样的标签,但不起作用,我采用 ERRORS

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 

这是我的代码:

的manifest.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"
    }
}

content.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>

get.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;
    }
});
请指导我......

1 个答案:

答案 0 :(得分:1)

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

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

更多信息here

相关问题