可以通过Chrome扩展程序修改窗口对象吗? [英] Can the window object be modified from a Chrome extension?
问题描述
我想制作一个Chrome扩展程序,它在窗口
中提供了一个新对象。当浏览器中加载了扩展的浏览器中查看网页时,我想通过Javascript提供 window.mything
。 window.mything
对象将具有一些我将在扩展中定义的函数,这些函数应该可以从 console.log
或任何Javascript文件,当页面在启用了扩展名的浏览器中查看时。
我可以通过使用 Content Script :
var s = document.createElement(script);
s.src = chrome.extension.getURL(mything.js);
document.getElementsByTagName(head)[0] .appendChild(s);
mything.js看起来像这样:
window.mything = {thing:true};
console.log(window);
每当页面加载时,我会看到整个窗口
对象,因为我期望它在控制台中。但是,我无法与控制台中的 window.mything
对象进行交互。看来如果注入的脚本还没有真正修改全局窗口
对象。
如何修改Chrome扩展中的全局窗口
对象?
你不能,不能直接。从内容脚本文档:
< blockquote>
但是,内容脚本有一些限制。它们不能:
$ b
- 使用chrome。* API(除chrome.extension的部分外)
- 使用由其扩展页面定义的变量或函数
- 使用由网页或其他内容脚本定义的变量或函数 $ b ul>
(强调增加)
$ b 窗口
内容脚本看到的code>对象与页面看到的窗口
对象不同。
您可以通过DOM传递消息,但是可以使用 window.postMessage
方法。你的页面和内容脚本都会收听消息事件,并且每当你从其中一个地方调用 window.postMessage
时,另一个会收到它。在内容脚本文档页上有一个这个例子 。
编辑:
您可以通过从内容脚本中注入脚本来向页面添加一些方法。它仍然无法与扩展的其余部分进行通信,而不使用类似于 postMessage
的东西,但是您至少可以将一些内容添加到页面的 window
var elt = document.createElement(script);
elt.innerHTML =window.foo = {bar:function(){/ * whatever * /}};
document.head.appendChild(elt);
I would like to make a Chrome extension that provides a new object inside window
. When a web page is viewed in a browser with the extension loaded, I would like window.mything
to be available via Javascript. The window.mything
object will have some functions that I will define in the extension, and these functions should be callable from console.log
or any Javascript file when the page is viewed in a browser with the extension enabled.
I was able to successfully inject a Javascript file into the page by using a Content Script:
var s = document.createElement("script");
s.src = chrome.extension.getURL("mything.js");
document.getElementsByTagName("head")[0].appendChild(s);
mything.js looks like this:
window.mything = {thing: true};
console.log(window);
Whenever a page loads, I see the entire window
object as I expect it to be in the console. However, I can't interact with the window.mything
object from the console. It seems at if the injected script hasn't really modified the global window
object.
How can I modify the global window
object from a Chrome extension?
You can't, not directly. From the content scripts documentation:
However, content scripts have some limitations. They cannot:
- Use chrome.* APIs (except for parts of chrome.extension)
- Use variables or functions defined by their extension's pages
- Use variables or functions defined by web pages or by other content scripts
(emphasis added)
The window
object the content script sees is not the same window
object that the page sees.
You can pass messages via the DOM, however, by using the window.postMessage
method. Both your page and content script listen to the message event, and whenever you call window.postMessage
from one of those places, the other will receive it. There's an example of this on the "Content Scripts" documentation page.
edit:
You could potentially add some methods to the page by injecting a script from the content script. It still wouldn't be able to communicate back with the rest of the extension though, without using something like postMessage
, but you could at least add some things to the page's window
var elt = document.createElement("script");
elt.innerHTML = "window.foo = {bar:function(){/*whatever*/}};"
document.head.appendChild(elt);
这篇关于可以通过Chrome扩展程序修改窗口对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!