gpt4 book ai didi

javascript - Chrome 扩展 : Cannot access contents of URL starting with "chrome-extension://"

转载 作者:行者123 更新时间:2023-12-05 00:33:13 24 4
gpt4 key购买 nike

在我的扩展中,我正在创建一个选项卡并打开一个名为 results.html 的 html 文件来自我的后台脚本,名为 background.js .创建选项卡后,我将注入(inject)一个名为 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 中添加以下权限:
  • <all_urls>
  • chrome-extension://*抛出的错误:权限“chrome-extension://*”未知或 URL 模式格式错误。

  • 但以上都没有奏效。

    还有 results.js注入(inject)后应该发送消息到 background.js得到一些数据以反馈到 results.html .

    我的代码:

    list .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"]
    }

    背景.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});

    }
    }

    结果.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 中添加 host_permissions。
    在 manifest.json 中添加主机权限的示例"host_permissions":["*://ahainstructornetwork.americanheart.org/"]详情请查看这篇文章:https://developer.chrome.com/docs/extensions/mv3/declare_permissions/#host-permissions

    关于javascript - Chrome 扩展 : Cannot access contents of URL starting with "chrome-extension://",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61323116/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com