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:

  1. <all_urls>
  2. 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:

  1. <all_urls>
  2. 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
Add a ment  | 

2 Answers 2

Reset to default 2

You 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": ["*://*/*"],

本文标签: