【问题标题】:Unchecked runtime.lastError: Cannot access contents of url "". Extension manifest must request permission to access this host. In manifest 3未经检查的 runtime.lastError:无法访问 url "" 的内容。扩展清单必须请求访问此主机的权限。在清单 3
【发布时间】:2021-07-18 11:51:59
【问题描述】:

请帮帮我!我在谷歌浏览器清单 3 中收到错误 Unchecked runtime.lastError: Cannot access contents of url. Extension manifest must request permission to access this host.Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

来自内容脚本的监听器

chrome.runtime.onMessage.addListener(
function(req, sender, sendResponse) {
    if(req.msg === "analysis background") {
        let obj = parse();
        sendResponse(obj);
    }
        
    return true;
}
); 
); 

manifest.json

{
    "manifest_version": 3,
    "name": "extensionParser",
    "version": "1.0.0",
    "action": { 
        "default_popup": "popups/popup.html"
      },
    "background": {
        "service_worker": "background.js"
    },
    "permissions": ["tabs", "scripting",
        "http://localhost/site_for_parsing"]
}

后台文件中的代码

const siteUrl = "http://localhost/site_for_parsing";

chrome.runtime.onConnect.addListener(port => {
    port.onMessage.addListener(msg => {
        if(msg.message === 'analysis') {
            chrome.tabs.create({active: false, url: siteUrl}, tab => {
                chrome.scripting.executeScript({
                    target: {tabId:tab.id},
                    files: ['dist/parser.js']
                }, (results) => {
                    chrome.tabs.sendMessage(tab.id, {msg: "analysis background"}, res => {
                        port.postMessage(res)
                        chrome.tabs.remove(tab.id)
                    })
                })
            });
        }

    });
});

提前感谢您!我等你的答案。美好的一天!

【问题讨论】:

  • @wOxxOm 我尝试使用 host_permissions,但它不起作用。我也犯了同样的错误

标签: javascript google-chrome-extension manifest.json


【解决方案1】:
  1. 应将站点权限添加到host_permissions,而不是permissionsmore info
  2. ManifestV3 中的 create + executeScript 在 Chrome 100 之前的版本中是 bugged,因此 a) 回到 ManifestV2 直到 Chrome 100 稳定或 b) 使用以下解决方法。

等待设置网址

(async () => {
  const tab = await chrome.tabs.create({url: 'https://www.example.com'});
  const tabId = tab.id;
  if (!tab.url) await onTabUrlUpdated(tabId);
  const results = await chrome.scripting.executeScript({
    target: {tabId},
    files: ['content.js'],
  });
  chrome.tabs.sendMessage(tabId, {msg: 'analysis background'}, res => {
    port.postMessage(res);
    chrome.tabs.remove(tabId);
  });
})();

function onTabUrlUpdated(tabId) {
  return new Promise((resolve, reject) => {
    const onUpdated = (id, info) => id === tabId && info.url && done(true);
    const onRemoved = id => id === tabId && done(false);
    chrome.tabs.onUpdated.addListener(onUpdated);
    chrome.tabs.onRemoved.addListener(onRemoved);
    function done(ok) {
      chrome.tabs.onUpdated.removeListener(onUpdated);
      chrome.tabs.onRemoved.removeListener(onRemoved);
      (ok ? resolve : reject)();
    }
  });
}

【讨论】:

  • 你推荐哪种方式?
  • 这取决于您是否愿意花时间迁移到ManifestV3,它仍然充满了Bug。
  • @wOxxOm 可能是愚蠢的问题,但是我可以从 v3 切换到 v2 并且一切都应该正常工作吗?我在移动设备上,所以我还不能尝试,但我不知道为什么我没有考虑降级,所以我很好奇这是否受支持(即任何有效的 v3 配置也适用于 v2 )
  • 视情况而定。如果您遇到问题,请使用正确的MCVE 发布新问题。
  • 谢谢@wOxxOm,它为我节省了很多时间。没想到新的 manifest 会有这么多错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2023-03-10
  • 2012-11-14
  • 2016-06-04
相关资源
最近更新 更多