如何在 Chrome 中模仿 Greasemonkey/Firefox 的 unsafeWindow 功能?
-
06-07-2019 - |
题
我现在只是在 chrome 中摆弄用户脚本,所以请容忍我潜在的无知/白痴。
在我正在编写脚本的页面中,有一个 <script>
声明变量的元素 x
。这是否意味着,在我的用户脚本中,我可以访问 x
来自全局命名空间?
例如,如果我的用户脚本中的唯一行是 alert(x);
, ,是否可以按预期工作(假设 x
是一个字符串)?我知道 chrome 不支持 unsafewindow,但由于某种原因,我发现不可能弄清楚如何模仿该功能。有可能吗?
解决方案
contentWindow
可在Chrome 3中使用,但已在Chrome中删除4 。仅适用于Chrome 4的解决方案:
location.href="javascript:(function(){ alert('Hello'); })()"
其他提示
这将为您提供对窗口对象的引用(如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版本。
要在Chrome中获得unsafeWindow
功能,最好的办法是安装和使用 Tampermonkey - 无论如何,你都会聪明地做。 Tampermonkey完全支持Greasemonkey API和更简单的脚本管理。
Greasemonkey脚本和Tampermonkey脚本几乎总是完全兼容,对于普通的Chrome用户脚本来说并非如此。
放弃Tampermonkey,唯一可行的方法是使用某种形式的脚本注入。
结果
以下内容现已过时:
Chrome现在定义<=> for userscripts / content-scripts ,但Chrome的<=>仍然不允许访问到目标页面创建的JS对象。
以下是如何以跨浏览器的方式提供正确不安全的<=> - 使用 功能检测(好)与浏览器嗅探(错误):
/*--- 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 ();
} ) ();
}
如果您想与页面 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
在窗口中运行你想要的任何内容