admin管理员组文章数量:1397158
In my extension, I am creating a tab and opening a html file named results.html
in it from my background script named background.js
. After creating a tab I am injecting a javascript file named results.js
to that newly created tab.
But it throws the following error in my background.js
console:
Unchecked runtime.lastError: Cannot access contents of url "chrome-extension://hcffonddipongohnggcbmlmfkeaepfcm/results.html".
Extension manifest must request permission to access this host.
Going through the solutions on other stackoverflow questions I have tried adding following permissions in manifest.json
:
<all_urls>
chrome-extension://*
Error thrown: Permission 'chrome-extension://*' is unknown or URL pattern is malformed.
But none of above worked.
Also the results.js
after injected is supposed to send message to background.js
to get in response some data to feed in results.html
.
My Code:
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*/
}
);
In my extension, I am creating a tab and opening a html file named results.html
in it from my background script named background.js
. After creating a tab I am injecting a javascript file named results.js
to that newly created tab.
But it throws the following error in my background.js
console:
Unchecked runtime.lastError: Cannot access contents of url "chrome-extension://hcffonddipongohnggcbmlmfkeaepfcm/results.html".
Extension manifest must request permission to access this host.
Going through the solutions on other stackoverflow questions I have tried adding following permissions in manifest.json
:
<all_urls>
chrome-extension://*
Error thrown: Permission 'chrome-extension://*' is unknown or URL pattern is malformed.
But none of above worked.
Also the results.js
after injected is supposed to send message to background.js
to get in response some data to feed in results.html
.
My Code:
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*/
}
);
Share
Improve this question
asked Apr 20, 2020 at 12:59
AbhayAbhay
5263 gold badges13 silver badges30 bronze badges
5
- 2 executeScript exists only to run content scripts in web pages, you can't use it here. See Pass data or modify extension html in a new tab/window – woxxom Commented Apr 20, 2020 at 13:01
- @wOxxOm thanks taking inspiration from 4th method I added results.js to results.html and initiated message from results.js – Abhay Commented Apr 20, 2020 at 14:04
- @wOxxOm can you please write down an answer here so that I could accept it as a solution. – Abhay Commented Apr 20, 2020 at 14:05
- 1 It may make more sense if you post fragments from your working solution. – woxxom Commented Apr 20, 2020 at 14:07
- did you figure out how to inject there? – JobaDiniz Commented Oct 31, 2023 at 15:22
2 Answers
Reset to default 2You need to add the host_permissions in the manifest.json.
An example of adding host permission in manifest.json
"host_permissions":["*://ahainstructornetwork.americanheart/"]
Check this article for details: https://developer.chrome./docs/extensions/mv3/declare_permissions/#host-permissions
Put this in your manifest.json:
"host_permissions": ["*://*/*"],
本文标签:
版权声明:本文标题:javascript - Chrome Extension: Cannot access contents of URL starting with "chrome-extension:" - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744151458a2593075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论