Unchecked runtime.lastError: Cannot access contents of url “”. Extension manifest must request permission to access this host. In manifest 3

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

Leave a Comment