关于google chrome:资源必须在web_accessible_resources清单密钥中列出,以便由扩展程序外部的页面加载

Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension

我尝试了多种方法(所有记录的过程),以便在onUpdated.addListener中检查URL后将脚本注入到特定页面中。 最后,下面带有" executescript"的代码似乎可以正常工作,但并非完美。 我可以获取警报,但无法通过getElementById / getElementsByName查找页面的文档元素。

当我检查页面时,脚本被注入。 但是在错误控制台中,我得到:

Denying load of chrome-extension://jfeiadiicafjpmaefageabnpamkapdhe/js/Leoscript.js. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.

Manifest.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
 "name":"Leo Extension for Job Boards",
 "version":"1.6",
 "manifest_version": 2,
 "content_security_policy":"script-src 'self'; object-src 'self'",
 "description":"Leo Extension",
 "background": {
   "scripts": ["js/Leojshelper.js"],
   "persistent": true
  },
 "content_scripts": [
    {
     "matches": [""],
     "js": ["js/eventPage.js"],
     "run_at" :"document_start"
    }
  ],
 "icons":{"48":"images/bob48.png","128":"images/bob128.png"}, //Define any icon sizes and the files that you want to use with them. 48/128 etc.
 "browser_action": {
   "default_icon":"images/bob.png",       // What icon do you want to display on the chrome toolbar
   "default_popup":"LeoExtwatch.html"     // The page to popup when button clicked.
  },
 "permissions": [
   "tabs",""      //"http://*/*","https://*/*"             // Cross Site Access Requests
  ],
  "web_accessible_resources": ["js/LeoScript.js"]
}

我还为脚本授予了" web_accessible_resources"权限,但仍然没有成功。 后台脚本中的代码:

1
2
3
4
5
6
7
8
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (changeInfo.status == 'complete') {
        if (tab.url.indexOf("in.yahoo") !== -1) {
            chrome.tabs.update(tabId, { url:"https://login.yahoo.com/config/mail?.intl=us" });
            chrome.tabs.executeScript(tabId, {
                code:"document.body.appendChild(document.createElement('script')).src='" +
    chrome.extension.getURL("js/LeoScript.js") +"';"
            }, null);

LeoScript.js中的代码,将注入到特定页面中。

1
2
3
4
$(document).ready(function () {
    alert('injected');
    document.getElementById('username').value='aaaaaaa';
});

我用来注入脚本的内容脚本:eventPage.js。

1
2
3
var script = document.createElement('script');
    script.src = chrome.extension.getURL("js/Leoscript.js");
    (document.body || document.head || document.documentElement).appendChild(script);

请指出上述代码中可以解决权限问题的任何更改。 提前致谢。


更新:终于解决了您的问题。 在eventPage.js中,您尝试注入未列入白名单的js / Leoscript.js,而不是已列入白名单的js / LeoScript.js(大写的" S")。 请注意,URL区分大小写!

1
chrome.tabs.executeScript(tabId, {file: 'js/LeoScript.js'});

LeoScript.js:

1
2
alert('injected');
document.getElementById('username').value='aaaaaaa';


编辑:

这是工作版本,其中结合使用了web_accessible_resources和Injection

manifest.json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name":"Off Screen Tabs Demo",
"description":"This demonstrates Off Screen Tabs API",
"manifest_version":2,
"version":"1",
"permissions":["tabs",""],
"browser_action":{
   "default_icon":"screen.png",
   "default_popup":"popup.html"
},
"web_accessible_resources": ["js/LeoScript.js"] ,
"permissions":["tabs",""]
}

LeoScript.js

1
alert("Injected..");

popup.html

1
2
3
4
5
6
7
<html>
<head>
<script src="popup.js">
</head>
<body>
</body>
</html>

popup.js *

1
2
3
document.addEventListener("DOMContentLoaded",function (){
    chrome.tabs.executeScript( {"file":"js/LeoScript.js"});
});

让我知道您是否仍然无法运行它

  • 感谢Sudarshan,当我尝试您的解决方案时,在tabs.executeScript中遇到错误:无法加载文件:" chrome-extension:// jfeiadiicafjpmaefageabnpamkapdhe / js / LeoS cript.js"。可能是我在注入脚本时做错了事,因为我正在使用内容脚本:eventPage.js注入代码。
  • @VineelGogineni:可以消除jsLeoScript.js的文件夹结构并将其保留在根路径中吗?将文件移动到根路径后使用{file: chrome.extension.getURL("LeoScript.js") },检查答案我已对其进行了编辑
  • 我尝试消除文件夹结构,但是出现相同的错误,并且如果我使用文件:chrome.extension.getURL(" LeoScript.js"),我也没有收到警报。
  • @VineelGogineni:我已经用工作版本编辑了我的答案,以它为参考,让我知道您是否仍然有问题
  • 我尝试了您的代码,它工作正常并且没有控制台错误,但是我需要将脚本注入到另一个页面的DOM中,以便可以访问其元素。当您尝试将代码插入另一个页面时,将出现该错误。
  • 您是否正在使用失败的document.getElementById(username).value=aaaaaaa;脚本?
  • 即使我起飞document.getElementById(username).value=aaaaaaa;,也会出现错误。不论注入脚本中的内容如何,?龅蹦⑹越疟咀⑷肓硪桓鲆趁媸保么砦蟛呕岢鱿帧N胰衔Ω糜幸恍┡渲蒙柚美唇饩龃巳ㄏ尬侍狻