Chrome Extension: Get Page Variables in Content Script

后端 未结 9 914
抹茶落季
抹茶落季 2020-11-27 03:44

Is there any way to retrieve a page\'s javascript variables from a Google Chrome Content Script?

相关标签:
9条回答
  • 2020-11-27 04:35

    No.

    Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.

    Isolated worlds allow each content script to make changes to its JavaScript environment without worrying about conflicting with the page or with other content scripts. For example, a content script could include JQuery v1 and the page could include JQuery v2, and they wouldn't conflict with each other.

    Another important benefit of isolated worlds is that they completely separate the JavaScript on the page from the JavaScript in extensions. This allows us to offer extra functionality to content scripts that should not be accessible from web pages without worrying about web pages accessing it.

    0 讨论(0)
  • 2020-11-27 04:35

    If you know which variables you want to access, you can make a quick custom content-script to retrieve their values.

    In popup.js :

    chrome.tabs.executeScript(null, {code: 'var name = "property"'}, function() {
        chrome.tabs.executeScript(null, {file: "retrieveValue.js"}, function(ret) {
            for (var i = 0; i < ret.length; i++) {
                console.log(ret[i]); //prints out each returned element in the array
            }
        });
    });
    

    In retrieveValue.js :

    function returnValues() {
        return document.getElementById("element")[name];
        //return any variables you need to retrieve
    }
    returnValues();
    

    You can modify the code to return arrays or other objects.

    0 讨论(0)
  • 2020-11-27 04:38

    Using Liran's solution, I'm adding some fix for Objects, here's correct solution:

    function retrieveWindowVariables(variables) {
        var ret = {};
    
        var scriptContent = "";
        for (var i = 0; i < variables.length; i++) {
            var currVariable = variables[i];
            scriptContent += "if (typeof " + currVariable + " !== 'undefined') $('body').attr('tmp_" + currVariable + "', JSON.stringify(" + currVariable + "));\n"
        }
    
        var script = document.createElement('script');
        script.id = 'tmpScript';
        script.appendChild(document.createTextNode(scriptContent));
        (document.body || document.head || document.documentElement).appendChild(script);
    
        for (var i = 0; i < variables.length; i++) {
            var currVariable = variables[i];
            ret[currVariable] = $.parseJSON($("body").attr("tmp_" + currVariable));
            $("body").removeAttr("tmp_" + currVariable);
        }
    
         $("#tmpScript").remove();
    
        return ret;
    }
    
    0 讨论(0)
提交回复
热议问题