为什么来自用户脚本的窗口(和不安全窗口)与来自 <script> 的窗口不同?标签? [英] Why is window (and unsafeWindow) not the same from a userscript as from a <script> tag?

查看:21
本文介绍了为什么来自用户脚本的窗口(和不安全窗口)与来自 <script> 的窗口不同?标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发这个小用户脚本.当我想用我的脚本阻止正在运行的网站中的每个 XMLHttpRequest 时,什么也没有发生(至少对于 Chrome):

I was facing an issue while developing this small userscript. When I wanted to block every XMLHttpRequest from the running website with my script, nothing was happening (at least with Chrome):

function main() {
  // Override XHR.open with a custom function
  window.XMLHttpRequest.prototype.open = function() {
    // Nothing... so it's supposed to block every xhr.open() call
  }
}
main();

unsafeWindow 替换 window 也是一样.

Same thing when replacing window by unsafeWindow.

然而,当我使用这个小技巧时,一切都像一个魅力:

However, when I used this little trick, everything worked like a charm:

// No more call to main(), and:
var script = document.createElement("script");
script.textContent = "(" + main.toString() + ")();";
document.body.appendChild(script);

xhr.open 的每次调用都被我的自定义函数取代,不再是 AJAX.

Every call to xhr.open is replaced by my custom function, no more AJAX.

所以我猜 window 元素在从脚本内部调用 main 和从

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆