6

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.

Listener from content script

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

Code from background file

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'm waiting your answers. Good day!

Yaroslav
  • 88
  • 2
  • 10

1 Answers1

17
  1. Site permissions should be added to host_permissions, not permissions, more info.
  2. create + executeScript in ManifestV3 is bugged in Chrome older than 100 so either a) go back to ManifestV2 until Chrome 100 is stable or b) use the workaround below.

Wait for the URL to be set:

(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)();
    }
  });
}
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Which way do you recommend? – Yaroslav Apr 24 '21 at 15:25
  • It depends on whether you are willing to spend your time on the migration to ManifestV3 which is still full of bugs all around. – wOxxOm Apr 24 '21 at 15:28
  • @wOxxOm potentially dumb question, but can I just switch from v3 to v2 and everything should still work? I'm on mobile so I can't try just yet, but I don't know why I didn't think about downgrading and so I'm curious if that's something thats supported (i.e. any valid v3 configuration will also work in v2) – c_idle May 30 '21 at 04:00
  • It depends. If you encounter problems, post a new question with a proper [MCVE](/help/mcve). – wOxxOm May 30 '21 at 04:11
  • 1
    thank you @wOxxOm it saved me so much time. I didn't expect new manifest would be so buggy. – Serge P Sep 20 '21 at 03:49