在我的扩展中,我创建了一个选项卡,并从名为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"]
}
background.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});
}
}
results.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*/
}
);
2条答案
按热度按时间hyrbngr71#
您需要在manifest.json中添加host_permissions。在manifest.json
"host_permissions":["*://ahainstructornetwork.americanheart.org/"]
中添加主机权限示例查看这篇文章了解详细信息:https://developer.chrome.com/docs/extensions/mv3/declare_permissions/#host-permissions
ev7lccsx2#
把这个放到你的舱单里json: