Chrome 资源必须列在web_accessible_resources清单项中,以便由扩展外部的页加载

6yoyoihd  于 2023-08-01  发布在  Go
关注(0)|答案(4)|浏览(347)

我已经尝试了很多方法(所有文档化的过程),在onUpdated. addListener检查URL时将脚本注入到特定页面。最后,下面的代码与'executescript'似乎工作,但不是完美的。我可以通过getElementById/getElementsByName获得警报,但无法找到页面的文档元素。
当我检查页面时,脚本被注入。但在错误控制台中,我得到:
拒绝加载chrome扩展://jfeiadiicafjpmaefageabnpamkapdhe/js/Leoscript.js。资源必须列在web_accessible_resources清单项中,以便由扩展外部的页面加载。
Manifest.json:

{
  "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": ["<all_urls>"],
      "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", "<all_urls>"      // "http://*/*","https://*/*"             // Cross Site Access Requests
  ],
   "web_accessible_resources": ["js/LeoScript.js"]
}

字符串
我也给了脚本'web_accessible_resources'权限,但仍然没有成功。后台脚本中的代码:

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中的代码,将注入特定页面。

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


Content Script:eventPage.js,我用来注入脚本。

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


请告诉我上面代码中的任何修改,以解决权限问题。先谢谢你。

vnjpjtjt

vnjpjtjt1#

**更新:**终于解决了你的问题。在eventPage.js中,您尝试注入js/Leoscript.js,它没有被列入白名单,而不是js/LeoScript.js(大写“S”),它被列入白名单。注意URL是 * 区分大小写 * 的!

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

字符串
LeoScript.js:

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

holgip5t

holgip5t2#

编辑:
这是使用web_accessible_resources和注入的组合的工作版本

manifest.json

{
"name":"Off Screen Tabs Demo",
"description":"This demonstrates Off Screen Tabs API",
"manifest_version":2,
"version":"1",
"permissions":["tabs","<all_urls>"],
"browser_action":{
    "default_icon":"screen.png",
    "default_popup":"popup.html"
},
 "web_accessible_resources": ["js/LeoScript.js"] ,
 "permissions":["tabs","<all_urls>"]
}

字符串

  • LeoScript.js*
alert("Injected..");

  • popup.html*
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>

popup.js****

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


让我知道如果你仍然有问题,让它运行

jhkqcmku

jhkqcmku3#

许多人会因为这个错误而出现在这个页面上,因为他们没有在manifest.json文件中包含他们的图像/Web资源。API文档的链接很有帮助,所以分享一下:web resource in manifest

enyaitl3

enyaitl34#

对我来说,当我禁用扩展时,这个问题得到了解决,它工作了!这是错误代码:
要解决这个问题,只需进入chrome://extensions/搜索扩展ID(这里是“nllcnknpjnininklegdoijpljgdjkijc”)并关闭该扩展。

相关问题