Accessing a pages object using a Chrome Extension

痴心易碎 提交于 2020-01-04 10:39:07

问题


I simply have to access an object that is a variable on the page that I am running my content script on from my Chrome Extension.

I know about the environments and their isolated worlds in which the content scripts and injected scripts run and that it's possible to get some variables using the injected scripts and then send them back.

I have searched for other answers regarding this question and most work for other type of variables and are the basic way of doing it but none currently work for accessing objects.

Any current solutions or workarounds?

EDIT: The solution that I used:

Content script:

//Sends an object from the page to the background page as a string
window.addEventListener("message", function(message) {
    if (message.data.from == "myCS") {
        chrome.runtime.sendMessage({
            siteObject: message.data.prop
        });
    }
});
var myScript = document.createElement("script");
myScript.innerHTML = 'window.postMessage({from: "myCS", prop: JSON.stringify(OBJECT)},"*");';
document.body.appendChild(myScript);

Background.js:

//Info receiver
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {

    //When the content script sends the sites object to extract the needed data
    if (message.siteObject !== undefined) {
        console.log(message.siteObject);
        //Process the data
    }

});

回答1:


You can try to inject a script tag in the page to access the object. If needed, you could use messaging to communicate with your extension. For example, assuming the object you want to access in your page is called pageObject:

content1.js

//this code will add a new property to the page's object
var myOwnData = "createdFromContentScript";
var myScript = document.createElement("script");
myScript.innerHTML = "pageObject.myOwnData = " + myOwnData;
document.body.appendChild(myScript);

content2.js

//this code will read a property from the existing object and send it to background page

window.addEventListener("message", function(message) {
    if (message.data.from == "myCS") {
        chrome.runtime.sendMessage({theProperty: message.data.prop});
    }
});

var myScript = document.createElement("script");
myScript.innerHTML = 'window.postMessage({from: "myCS", prop: pageObject.existingProperty},"*");';
document.body.appendChild(myScript);



回答2:


No, there is no way. There is no point having the isolated worlds for security and then there being a workaround whereby an extension can hack the content script and variables if it really needs to.

Presumably the object on the page interacts with the page or has some effect on the page or something on the page affects the state of the variable. You can trigger actions on the page (via the DOM) that might change the state of that variable but you should stop looking for ways to access variables directly.

Of course if the page author is cooperative then it's a different ball game - a mechanism could be provided in the author's script, a getter and setter mechanism. But somehow I doubt that's what you're after.



来源:https://stackoverflow.com/questions/47523564/accessing-a-pages-object-using-a-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!