[英]How do I access ALL window objects from a Chrome extension?
I am developing a Chrome extension for work, and one of the things it needs to do is to read (only read, not modify) an object that we send back to the website after it makes an asynchronous request to our servers. 我正在开发一个用于工作的Chrome扩展程序,它需要做的一件事就是读取(仅读取,而不是修改)我们在向服务器发出异步请求后发送回网站的对象。 Basically I need to read the
window.<our object name>
object and get what's in there. 基本上我需要阅读
window.<our object name>
对象并获取其中的内容。
Now, I know this is possible, because I did this in a Tampermonkey script that I wrote. 现在,我知道这是可能的,因为我在我写的Tampermonkey脚本中这样做了。 I was able to
console.log(window.<our object name>)
and it came in. 我能够访问
console.log(window.<our object name>)
,它进来了。
Tampermonkey is a Chrome extension, so there's no intrinsic reason why it can access something and another extension can't. Tampermonkey是一个Chrome扩展程序,所以它没有内在的原因可以访问某些内容而另一个扩展程序无法访问。
But when I try to access this object, both from content scripts and from injected code, I get nothing. 但是当我尝试从内容脚本和注入的代码访问这个对象时,我什么也得不到。 When I get the
window
object only, it comes up only partially, as if the extension were blind to certain parts of it. 当我只得到
window
对象时,它只是部分出现,好像扩展对它的某些部分是盲目的。 But if I'm in the console on the page, and I call window
, I get a full window object back. 但如果我在页面的控制台上,并且我调用了
window
,我会得到一个完整的窗口对象。 Infuriating. 真气。
So if content scripts don't work, and injected scripts don't work, and there's no reason why popup scripts would be any good here, how does one do this? 因此,如果内容脚本不起作用,并且注入的脚本不起作用,并且弹出脚本没有理由在这里有任何好处,那么如何做到这一点呢?
Many thanks! 非常感谢!
UPDATE: As requested, here is the manifest.json (I took the page_redder example and worked off that to make sure I wasn't making any weird mistakes): 更新:根据要求,这里是manifest.json(我拿了page_redder示例并解决了这个问题,以确保我没有犯任何奇怪的错误):
{
"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 content.js: 这是content.js:
var getWindow = window.setTimeout(function() { console.log("From content script: " + window.<OBJECT NAME>); }, 5000);
And here is background.js: 这是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>);'
});
});
When run, I get: 跑步时,我得到:
From content script: undefined From injected script: undefined 来自内容脚本:undefined从注入的脚本:undefined
But if I do window. 但如果我做窗户。 from the console, I get it.
从控制台,我明白了。 I even added a timeout to make sure that the content script wasn't trying to get something that hadn't loaded in yet.
我甚至添加了一个超时,以确保内容脚本没有尝试获取尚未加载的内容。 But I can retrieve the object manually before the script runs, and it still gives me undefined.
但我可以在脚本运行之前手动检索对象,它仍然给我未定义。
Interesting question. 有趣的问题。 Here is a quick and probably incomplete answer :
这是一个快速且可能不完整的答案:
window
object window
对象 background.js
also has its own background.js
也有自己的 chrome.tabs.executeScript
but somehow I would'nt trust it with anything beyond basics chrome.tabs.executeScript
但不知怎的,我不相信任何超出基础知识的东西 One approach could be as follow : 一种方法可以如下:
Open the relevant page from the background script with chrome.tabs.create
: hence the backgound will have complete control and dominance over said tab and the window
, document
and your_object
therein. 使用
chrome.tabs.create
从后台脚本打开相关页面:因此,背景将完全控制并your_object
于所述选项卡以及其中的window
, document
和your_object
。 It will also be easier to handle the asynchronous side of thing : you'll learn to love callbacks . 处理事物的异步方面也会更容易:你将学会喜欢回调 。
Depending on what is required regarding the UX, another option would be to handle the async request, and fetch your_object
, entirely in background.js
根据有关UX的要求,另一种选择是处理异步请求,并完全在
background.js
获取your_object
。
One last hint : extensions you download from the store are just zipped files in your Chrome profile. 最后一个提示:您从商店下载的扩展程序只是Chrome配置文件中的压缩文件。 Find
tapermonkey.crx
or whatever, unzip it and read the sources to figure out what it does. 找到
tapermonkey.crx
或其他什么,解压缩它并阅读源代码以找出它的作用。
And oh, relying on timeout to handle asynchronicity is bound to random results. 哦,依靠超时来处理异步性必然会产生随机结果。
Soo, this is kind of hacky, but I was able to do it and it worked. 所以,这有点像hacky,但我能够做到这一点并且有效。
To gain access to everything available to the host window
, I had to create a script
element, put all the code I wanted in there, and add document.body.appendChild(script)
for it to work. 为了访问主机
window
可用的所有内容,我必须创建一个script
元素,将所需的所有代码放在那里,并添加document.body.appendChild(script)
以使其工作。
Not the sexiest way of doing things, but it will get the job done for small tasks. 这不是最性感的做事方式,但它可以完成小任务的工作。
According to documentation in https://developer.chrome.com/extensions/content_scripts 根据https://developer.chrome.com/extensions/content_scripts中的文档
However, content scripts have some limitations.
但是,内容脚本有一些限制。 They cannot :
他们不能 :
- Use variables or functions defined by web pages or by other content scripts.
使用由网页或其他内容脚本定义的变量或函数。
So you can access the common window variables from the content script, but not the variables created from the webpage's javascript, another content script or, as in your case, an object you have sent to the website. 因此,您可以从内容脚本访问公共窗口变量,但不能访问从网页的javascript,另一个内容脚本创建的变量,或者在您的情况下,您已发送到网站的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.