本文介绍了未经检查的 runtime.lastError:无法访问 url“"的内容.扩展清单必须请求访问此主机的权限.在清单 3 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我!我收到错误 Unchecked runtime.lastError: 无法访问 url 的内容.扩展清单必须请求访问此主机的权限.Unchecked runtime.lastError: 无法建立连接.接收端不存在.在谷歌浏览器清单3中.

Help me, please! I have gotten error Unchecked runtime.lastError: Cannot access contents of url. Extension manifest must request permission to access this host. and Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. in google chrome manifest 3.

来自内容脚本的监听器

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

    return true;
}
);
);

manifest.json

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

    });
});

先谢谢你!我等你的答案.美好的一天!

Thank you in advance! I wait your answers. Good day!

推荐答案

  1. 站点权限应该添加到host_permissions,而不是permissions更多信息.
  2. 在 ManifestV3 中创建 + 执行脚本有问题,所以要么 a) 返回 ManifestV2 直到修复或 b)使用下面的解决方法.
  1. Site permissions should be added to host_permissions, not permissions, more info.
  2. create + executeScript in ManifestV3 is bugged so either a) go back to ManifestV2 until this is fixed or b) use the workaround below.

等待 URL 设置:

(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)();
    }
  });
}

这篇关于未经检查的 runtime.lastError:无法访问 url“"的内容.扩展清单必须请求访问此主机的权限.在清单 3 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:22