0

I have a simple Chrome developer extension which injects JavaScript into a target page.

It accesses the DOM no problem but will not allow me to access the variables.

    <head>
    <script>
        window.myVar = 10;
    </script>
    </head>
    <body>

    <div id="testDiv">
        some div content
    </div>

    </body>

Calling this snippet:

            chrome.tabs.executeScript(
                chrome.devtools.inspectedWindow.tabId, 
                {   
                    code: scriptToExecute,
                    frameId: targetId
                },
                function(response) {
                    let result = [];
                    if(response && response.length > 0) {
                        result = response[0];
                    }
                    results.value = result;
                }
            );

This works:

    scriptToExecute = document.getElementById('testDiv').innerHTML;

This returns null:

    scriptToExecute = window.myVar;

Is there some way to access the variables or is it prevented for security or other reasons?

Gordon Kushner
  • 165
  • 1
  • 9
  • I think you have to use `postMessage` to send it from your user script to your extension. Or include it in the DOM somewhere. – Xotic750 Sep 06 '19 at 13:48
  • Related: https://stackoverflow.com/questions/9602022/chrome-extension-retrieving-global-variable-from-webpage – Xotic750 Sep 06 '19 at 13:52

0 Answers0