[英]Chrome Extension: Cannot access contents of URL starting with “chrome-extension://”
在我的扩展中,我正在创建一个选项卡并从名为background.js
的后台脚本中打开一个名为results.html
的 html 文件。 创建选项卡后,我将一个名为results.js
的 javascript 文件注入到新创建的选项卡中。
但它在我的background.js
控制台中引发以下错误:
Unchecked runtime.lastError: Cannot access contents of url "chrome-extension://hcffonddipongohnggcbmlmfkeaepfcm/results.html".
Extension manifest must request permission to access this host.
通过其他 stackoverflow 问题的解决方案,我尝试在manifest.json
中添加以下权限:
<all_urls>
chrome-extension://*
抛出错误:权限“chrome-extension://*”未知或 URL 模式格式错误。但以上都没有奏效。
此外,注入后的results.js
应该向background.js
发送消息,以响应一些数据以输入results.html
。
我的代码:
manifest.json
{
"manifest_version":2,
"name":"Extension Name",
"description":"This is description of extension.",
"version":"1.0.0",
"icons":{"128":"icon_128.png"},
"browser_action":{
"default_icon":"icon.png",
"default_popup":"popup.html"
},
"permissions":["activeTab", "background", "tabs", "http://*/*", "https://*/*","<all_urls>"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"web_accessible_resources": ["addAlias.js","results.html","results.js"]
}
背景.js
/*Some code*/
function loadResult()
{
chrome.tabs.query({active:true},function(tabs){
//creating tab and loading results.html in it
chrome.tabs.create({url : 'results.html'}, function(tab){
//injecting results.js file in the tab
chrome.tabs.executeScript(tab.id, {file: 'results.js'});
});
});
}
/*somecode*/
if(/*some condtion*/)
{
loadResult(); //calling function
}
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse)
{
//Listening for results.js request for data
if( request.greeting === "sendResults")
{
console.log(" Results request received .");
//sending data back to results.js
sendResponse({failed:failedToAdd,succeed:succeedToAdd});
}
}
结果.js
/*Some code*/
console.log("I got loaded");
console.log("Now requesting for sendResults");
//Below sending request for data
chrome.runtime.sendMessage({greeting: "sendResults"},
function (response) {
console.log('Got data from Background.js');
/*Some Code*/
}
);
您需要在 manifest.json 中添加 host_permissions。 在 manifest.json "host_permissions":["*://ahainstructornetwork.americanheart.org/"]
中添加主机权限的示例
查看这篇文章了解详情: https://developer.chrome.com/docs/extensions/mv3/declare_permissions/#host-permissions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.