Search code examples
google-chrome-extensionchrome-extension-manifest-v3

chrome extension manifest v3, accessing window variable


How can I access a page's window variable through Manifest v3.

In V2, I could have a content script access the window variable of a page by injecting javascript into the main page. However, appendChild() for javascript code does not work on V3.

I tried using scripting.executeScript but this still has no effect in being able to access the window variable (the DOM is accessible).

For example the following code supposedly injects on frameid 0 but the window of the top javascript context is still not readable:

chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
      // since only one tab should be active and in the current window at once
      // the return variable should only have one entry
      const activeTab = tabs[0];
      const activeTabId = activeTab.id; // or do whatever you need

      chrome.scripting.executeScript({
        target: { tabId: activeTabId, frameIds: [0] },
        files: [filePath],
      });
    });

Solution

  • Maybe I have not understand well your problem but:

    1. If the page you want to access to is a page extension, you don't have to inject any content script to retreive any variable.

    2. If you want to get a content script variable you have created\set in a tab, you have to send a message to content script in that tab asking to return its value along that message.

    3. If you want to get a variable of the main page (out of your possible content script) you don' have any hope due to isolated world rules.