如何在Chrome中模拟Greasemonkey / Firefox的unsafeWindow功能? [英] How can I mimic Greasemonkey/Firefox's unsafeWindow functionality in Chrome?

查看:143
本文介绍了如何在Chrome中模拟Greasemonkey / Firefox的unsafeWindow功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在只是在用chrome来操作用户脚本,所以请耐心等待我的潜在无知/愚蠢。



在页面中,我正在编写一个脚本中,有一个声明变量 x < script> 元素。
这是否意味着,在我的用户脚本中,我可以从全局命名空间访问 x



<例如,如果我的用户脚本中的唯一一行是 alert(x); ,应该如预期那样工作(假设 x 是一个字符串)?我知道铬不支持unsafewindow,但由于某种原因,我发现不可能弄清楚如何模仿功能。它甚至有可能吗?

解决方案

contentWindow 但在Chrome 4中已删除。仅适用于Chrome 4的解决方案:

  location.href =javascript:(function(){alert('Hello'); })()


I'm just fiddling around with user scripts in chrome right now, so please bear with my potential ignorance/idiocy.

In the page I'm writing a script for, there is a <script> element that declares a variable x. Does this mean that, in my user script, I can just access x from the global namespace?

For example, if the only line in my userscript is alert(x);, should that work as expected (assuming x is a String)? I understand chrome doesn't support unsafewindow, but for some reason I'm finding it impossible to figure out how to mimic the functionality. Is it even possible?

解决方案

contentWindow was available in Chrome 3, but removed in Chrome 4. Only possible solution for Chrome 4:

location.href="javascript:(function(){ alert('Hello'); })()"

这篇关于如何在Chrome中模拟Greasemonkey / Firefox的unsafeWindow功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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