Chrome Extension: Get Page Variables in Content Script

后端 未结 9 950
抹茶落季
抹茶落季 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

    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.

提交回复
热议问题