0
你怎麼能叫GM_xmlhttpRequest
的eval
裏面,你是eval
荷蘭國際集團一些複雜的代碼,其中一些要求GM_xmlhttpRequest
。的Greasemonkey:執行GM_xmlhttpRequest()從EVAL(跟進)
這是一個後續行動Perform GM_xmlhttpRequest() from eval
下面是一些示例代碼:
// ==UserScript==
// @name Test GM AJAX
// ==/UserScript==
console = unsafeWindow.console;
function fetch(msg) {
console.log('fetching: '+msg);
GM_xmlhttpRequest({
method: 'GET',
url: 'http://google.com',
onload: function(responseDetails) {
console.log(msg);
}
});
}
function complicated(arg1, arg2) {
fetch(arg1 + arg2);
}
console.log('trying');
var code = 'complicated("Ya", "y!")';
function myEval(code) {
eval(code);
eval('setTimeout(function(){'+code+'},0)');
eval('setTimeout(fetch,0)');
eval('setTimeout(function(){console.log("here");fetch("cool")},0)');
fetch("BOO");
}
myEval(code);
,輸出:
trying
fetching: Yay!
fetching: BOO
fetching: Yay!
fetching: 30
here
fetching: cool
BOO
30
所以,唯一的訪存的工作是在setTimeout(fetch,0)
但我需要以實際執行code
其中包括複雜的代碼。
任何想法?
你可以證明你使用'eval'的理由嗎?從經驗中我可以看出,有99.7%的時間有替代選擇。 「我有複雜的代碼」不是理由。 – 2010-04-13 08:44:01
我正在構建一個插件系統,其中所有的插件都是存儲在服務器上的一小段javascript,併發送到客戶端執行。每個插件都可以調用客戶端庫來執行一些操作。有沒有比'eval()'更適合插件系統的方法? – 2010-04-13 08:51:36
我想這可能會落入其他0.3%。 – 2010-04-13 08:55:30