Abhay
Abhay

Reputation: 526

Chrome Extension: Cannot access contents of URL starting with "chrome-extension://"

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*/
      }
  );

Upvotes: 7

Views: 11130

Answers (2)

Kolja
Kolja

Reputation: 51

Put this in your manifest.json:

"host_permissions": ["*://*/*"],

Upvotes: 1

Salman A Mughal
Salman A Mughal

Reputation: 21

You need to add the host_permissions in the manifest.json. An example of adding host permission in manifest.json "host_permissions":["*://ahainstructornetwork.americanheart.org/"]

Check this article for details: https://developer.chrome.com/docs/extensions/mv3/declare_permissions/#host-permissions

Upvotes: 2

Related Questions