0

I have an array of valid URLs for a user.

let validUrls = ["*.example.com", "*.foo.com"]

I want to request permission to run my contentScript.js if the activeTab is on a domain that is a validUrl.

Without the user accepting permission first, I don't seem to get the active tab URL, so I am in a catch 22.

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  console.log("Tab Changed: ", changeInfo, tab);

  chrome.scripting.executeScript(
    {
      target: { tabId: tabId },
      func: checkPermissions,
      args: [tab.url], // Null because no permission is granted yet
    },
    (result) => {
      console.log(result);
    }
  );
});

I would like only to be requesting the activeTab permission and not the tabs permission.

Phill Duffy
  • 2,805
  • 3
  • 33
  • 45
  • 1
    Looks like you can simply remove `tab.url` and read `location.href` inside the function. – wOxxOm Nov 16 '21 at 12:12
  • I've just tried and had no luck. The URL I get is that of the servicer worker. I don't yet have permission to the active tab to run the code in its context, which keeps me at a stalemate. I am pretty sure it's just me thinking about the logic all wrong, but I can't flip my brain the right way up yet. I don't want a user to need to enable the extension on each page just to see if it's one they have access to via our software. – Phill Duffy Nov 16 '21 at 16:09
  • You should use `location.href` *inside the function*. – wOxxOm Nov 16 '21 at 16:14
  • The check permissions looks like this (for my test) ```function checkPermissions() { console.log("Checking Permissions: ", location.hostname); }``` but in this scenario, I need to enable the extension by clicking it first to allow the ```chrome.scripting.executeScript``` to run – Phill Duffy Nov 16 '21 at 16:17
  • 1
    This won't work so here's another solution. Assuming you want to provide an inconspicuous hint that your extension can work on this URL, you can use chrome.declarativeContent with its SetIcon action ([example](/a/64475504)) + chrome.browserAction.onClicked event in the background script to grant the permission for the active URL via chrome.permissions.request. – wOxxOm Nov 16 '21 at 16:22

0 Answers0