Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展:无法访问以“开始”开头的URL的内容;chrome extension:/“扩展名”;_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

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

Javascript Chrome扩展:无法访问以“开始”开头的URL的内容;chrome extension:/“扩展名”;,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,在我的扩展中,我正在创建一个选项卡,并从名为background.js的后台脚本中打开一个名为results.html的html文件。创建一个选项卡后,我将一个名为results.js的javascript文件注入到新创建的选项卡中 但是它在我的background.js控制台中抛出以下错误: Unchecked runtime.lastError: Cannot access contents of url "chrome-extension://hcffonddipongohnggcbmlm

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

  • chrome扩展名://*
    引发错误:权限“chrome扩展名://*”未知或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"]
    }
    
    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*/
          }
      );
    

    您需要在manifest.json中添加主机权限。 在manifest.json中添加主机权限的示例
    “主机权限”:[“*://ahaornetwork.americanheart.org/”]


    查看本文了解详细信息:

    executeScript仅用于在网页中运行内容脚本,您不能在此处使用它。请参阅@wOxxOm感谢从第四种方法中获得灵感,我将results.js添加到results.html,并从results中启动了消息。js@wOxxOm你能在这里写下一个答案,这样我就可以接受它作为一个解决方案。如果你从你的工作解决方案中发布片段,可能会更有意义。
    /*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*/
          }
      );