是的,您以前看过这个主题。但是,Chrome 关闭了最常见的端口之一以使用 unsafeWindow
来与内容脚本一起使用。
var unsafeWindow;
(function() {
var div = document.createElement("div");
div.setAttribute("onclick", "return window");
unsafeWindow = div.onclick();
})();
现在 div.onclick
返回 null。
我的问题是:目前是否有另一种方法可以通过内容脚本访问unsafeWindow
?例如,我想访问页面本身使用的 jQuery。
unsafeWindow.jQuery().jquery; // <- 1.9.0
最佳答案
好吧,我有一份决定草案给你。也许这就是您所需要的?
Injected.js 在原始窗口范围内工作,contentscript.js 在其独立窗口范围内工作。问题是您没有 unsafeWindow 变量,但我认为您可以使用此变量将代码包装在某个函数中。
*manifest.json
"web_accessible_resources": ["injected.js"],
"content_scripts": [{
"matches": ["*://*/*"],
"js": ["js/content.js"]
}],
...
contentscript.js
var internalScript = document.createElement("script");
internalScript.src = chrome.runtime.getURL("injected.js");
document.body.appendChild(internalScript);
注入(inject).js
console.log(window);
console.log(window.$);
关于javascript - 访问 Chrome unsafeWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286834/