2

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],
      });
    });
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Michael
  • 1,759
  • 4
  • 19
  • 29
  • Use a separate js file for your DOM `script` element's `src`, not `textContent`. And expose that file via web_accessible_resources. – wOxxOm May 20 '21 at 13:55

1 Answers1

0

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.

Robbi
  • 1,254
  • 2
  • 8
  • 11
  • It is option3 and it works under V2. You can just appendchild any script on the main page (e.g., head). That script can then access all variables on the main page and you can even retrieve data back by either printing results on DOM or using postmessage. It is the injection that does not work under V3 as it used to work. – Michael May 08 '21 at 16:47
  • 1
    Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". With MV3 no hope – Robbi May 10 '21 at 08:36
  • 2
    What I don't understand is there is many extensions that rely on accessing the main page's javascript. It seems that V3 will break a lot of extensions. Why Chrome would go this way makes no sense especially since much of the code that is injected can be verified by looking at the source. – Michael May 10 '21 at 14:51
  • Injecting script with scripting.executeScript you can access window "default" properties and functions but not the user defined object and global variables. I'm not the man who made the rules, I'm sorry – Robbi May 10 '21 at 15:21
  • #3 is wrong: extensions can access variables of the page, see [Use a content script to access the page context variables and functions](https://stackoverflow.com/q/9515704) – wOxxOm May 20 '21 at 13:56