Chrome扩展:无法访问以“chrome-extension://”开头的URL的内容

8aqjt8rx  于 2023-06-19  发布在  Go
关注(0)|答案(2)|浏览(508)

在我的扩展中,我创建了一个选项卡,并从名为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中添加以下权限:

  1. <all_urls>
  2. 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*/
      }
  );
hyrbngr7

hyrbngr71#

您需要在manifest.json中添加host_permissions。在manifest.json "host_permissions":["*://ahainstructornetwork.americanheart.org/"]中添加主机权限示例
查看这篇文章了解详细信息:https://developer.chrome.com/docs/extensions/mv3/declare_permissions/#host-permissions

ev7lccsx

ev7lccsx2#

把这个放到你的舱单里json:

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

相关问题