How to access all Windows objects from the Chrome extension?

I am developing a Chrome extension to work, and one of the things he needs to do is read (read only, not modify) the object that we send to the website after it makes an asynchronous request on our servers. Basically I need to read the window.<our object name> and get what is there.

Now I know that this is possible, because I did it in the Tampermonkey script that I wrote. I was able to console.log(window.<our object name>) and it went in.

Tampermonkey is a Chrome extension, so there is no reason why it can access something, and another extension cannot.

But when I try to access this object, both from content scripts and from the entered code, I get nothing. When I get only the window object, it only partially arises, as if the extension were blind for certain parts of it. But if I am in the console on the page and I call window , I return the full window object. Rabies.

So, if the content scripts do not work, and the nested scripts do not work, and there is no reason why the pop-up scripts will be good here, how to do it?

Thank you very much!

UPDATE: as requested, here is the manifest. json (I took the page_redder example and worked it out to make sure I didn't make any weird errors):

 { "name": "Page Redder", "description": "Make the current page red", "version": "2.0", "permissions": [ "activeTab" ], "background": { "scripts": ["background.js"], "persistent": false }, "browser_action": { "default_title": "get my object" }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"] } ], "manifest_version": 2 } 

And here is the content.js:

 var getWindow = window.setTimeout(function() { console.log("From content script: " + window.<OBJECT NAME>); }, 5000); 

And here is background.js:

 // Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Called when the user clicks on the browser action. chrome.browserAction.onClicked.addListener(function(tab) { // No tabs or host permissions needed! chrome.tabs.executeScript({ code: 'console.log("From injected script:" + window.<OBJECT NAME>);' }); }); 

On startup, I get:

From the contents of the script: undefined From the entered script: undefined

But if I make a window. from the console, I understand. I even added a timeout to make sure the content of the script was not trying to get what had not yet loaded. But I can get the object manually before running the script, and it still gives me undefined.

+5
source share
3 answers

Interest Ask. Here is a quick and probably incomplete answer:

  • each tab has its own separate window object
  • background.js also has its own
  • Content scripts are a little complicated in that, although they nominally live inside the page, they actually maintain a respectful distance: see isolated worlds
  • I am not familiar with chrome.tabs.executeScript , but somehow I will not trust him with anything that goes beyond

One approach may be as follows:

Open the corresponding page from the background script with chrome.tabs.create : therefore, backgound will have full control and dominance over this tab and window , document and your_object in it. It will also be easier to handle the asynchronous side: you will learn to love callbacks .

Depending on what is required for UX, another option would be to process the async request and fetch your_object , completely in background.js

One final tip: the extensions you download from the store are just zip files in your Chrome profile. Find tapermonkey.crx or anything else, unzip it and read the sources to find out what it does.

And oh, relying on a timeout to handle asynchrony, is tied to random results.

0
source

Su, this is hacky, but I was able to do it, and it worked.

To get access to everything available for the host window , I had to create a script element, put all the code that I need, and add document.body.appendChild(script) for it to work.

Not the sexiest way to do something, but it will do the job for small tasks.

0
source

According to the documentation at https://developer.chrome.com/extensions/content_scripts

However, content scripts have some limitations. They cannot :

  • Use variables or functions defined by web pages or other content scripts.

This way, you can access the shared variables of the window from the contents of the script, but not the variables created from the javascript webpage, other script content, or, in your case, the object that you sent to the website.

-2
source

Source: https://habr.com/ru/post/1247295/


All Articles