![](/img/trans.png)
[英]How do I replace unsafeWindow when migrating a pre-Firefox 30 Greasemonkey script to GM4+?
[英]How can I mimic Greasemonkey/Firefox's unsafeWindow functionality in Chrome?
我现在正在摆弄chrome中的用户脚本,所以请忍受我潜在的无知/白痴。
在我正在编写脚本的页面中,有一个声明变量x
的<script>
元素。 这是否意味着,在我的用户脚本中,我只能从全局命名空间访问x
?
例如,如果我的用户脚本中的唯一一行是alert(x);
,应该按预期工作(假设x
是一个字符串)? 我知道chrome不支持unsafewindow,但由于某种原因,我发现无法弄清楚如何模仿功能。 它甚至可能吗?
这将为您提供对窗口对象的引用(如p):
var p = unsafeWindow;
if(window.navigator.vendor.match(/Google/)) {
var div = document.createElement("div");
div.setAttribute("onclick", "return window;");
p = div.onclick();
};
更新:
onclick
漏洞不再适用于最新的Chrome版本。
要获得unsafeWindow
在Chrome的功能,最好的办法是安装和使用Tampermonkey -你是聪明的事,不管。 Tampermonkey完全支持Greasemonkey API和更简单的脚本管理。
Greasemonkey脚本和Tampermonkey脚本几乎总是完全兼容,对于纯Chrome用户脚本来说并非如此。
放弃Tampermonkey,唯一可行的方法是使用某种形式的脚本注入 。
以下内容现已过时:
Chrome现在为userscripts / content-scripts定义unsafeWindow
,但Chrome的unsafeWindow
仍然不允许访问目标页面创建的JS对象。
以下是如何使用特征检测 (好)与浏览器嗅探 (错误)的跨浏览器方式提供正确不安全,不安全的unsafeWindow
:
/*--- Create a proper unsafeWindow object on browsers where it doesn't exist
(Chrome, mainly).
Chrome now defines unsafeWindow, but does not give it the same access to
a page's javascript that a properly unsafe, unsafeWindow has.
This code remedies that.
*/
var bGreasemonkeyServiceDefined = false;
try {
if (typeof Components.interfaces.gmIGreasemonkeyService === "object") {
bGreasemonkeyServiceDefined = true;
}
}
catch (err) {
//Ignore.
}
if ( typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
unsafeWindow = ( function () {
var dummyElem = document.createElement('p');
dummyElem.setAttribute ('onclick', 'return window;');
return dummyElem.onclick ();
} ) ();
}
contentWindow
在Chrome 3中可用,但在Chrome 4中已删除 。 仅适用于Chrome 4的解决方案:
location.href="javascript:(function(){ alert('Hello'); })()"
如果要与页面JavaScript进行交互,则必须在页面中插入脚本。 (当然,除非你想使用本页建议的任何黑客。)我已经为我自己的脚本编写了一个功能,我会在这里发布,以防有人想要使用它。
/*
@description This function will insert the given code as a <script> or <style> block into a page.
@param The code to insert; supported types are: JavaScript Function, String (JavaScript), String (CSS).
@param2 Optional: The type of code that is inserted. If omitted, "js" is assumed. Possible values are 'js' or 'css'.
@return The HTML element that was inserted, or FALSE on failure
*/
function insert(z,t){
var j,f,x,c,i,n,d
d=document
c=d.createElement
i=d.head.appendChild
a=d.createTextNode
if(typeof z==='function') j=!0,f=!0;
if((t=='js'||!t)&&!f){j=!0,f=!1}
if(t=='css'&&!j){x=c('style');x.setAttribute('type','text/css')}
if(j){x=c('script');x.setAttribute('type','text/javascript')}
if(f) n=a('('+z+')()');else n=a(z)
x.appendChild(n)
if(x){return i(x)}else{return !1}
}
举几个例子来澄清:
//Inserting a JavaScript function
var func=function(){
stopAds();
startFileDownload();
}
insert(func);
//Inserting JavaScript as a string
var strJS="prompt(\"Copy:\",someVariableAtThePage);";
insert(strJS);
//Or with an OPTIONAL 2nd parameter:
insert(strJS,'js');
//Inserting CSS
var strCSS=".ad{display:none !important} #downloadButton{display:block}";
insert(strCSS,'css');//Specifying 2nd parameter as "css" is required.
好的,这是一个想法,您可以使用地址栏注入脚本...
javascript:var ElEm = document.createElement("script");ElEm.src='[path_to_script]';document.body.appendChild(ElEm);
那么你可以用你的javascript在窗口中运行你想要的任何东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.