嵌套XMLHttpRequests有多个闭包是个好主意吗? [英] Is nested XMLHttpRequests with multiple closures a good idea?
问题描述
我有一个Greasemonkey脚本,它在视频网站的搜索结果页上运行。脚本的功能是采取一个javascript链接,打开一个新的窗口与闪存播放器,跳过一些重定向圈,并插入一个常规链接到所需的FLV文件。
I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through some redirection hoops, and insert a regular link to the desired FLV file.
我已经改变了脚本做愚蠢,但结构上等同于en.wikipedia.org。我的问题是,3个嵌套的闭包和嵌套的xmlhttprequests是否是最好的解决方法。
I have changed the script to do silly but structurally equivalent things to en.wikipedia.org. My question is whether the 3 nested closures and the nested xmlhttprequests is the best way to go about this.
// ==UserScript==
// @name wiki mod example
// @namespace http://
// @description example script
// @include *wikipedia.org*
// ==/UserScript==
var candidates = document.getElementsByTagName("a");
for (var cand = null, i = 0; (cand = candidates[i]); i++) {
if (cand.href.match(/\/wiki\/W/)) { // for all articles starting with 'W'
var progress = document.createElement('span');
progress.appendChild(document.createTextNode(" Start"));
cand.parentNode.insertBefore(progress, cand.nextSibling);
progress.addEventListener("click",
function(link1) { return function() { // link1 is cand.href
this.innerHTML = " finding...";
GM_xmlhttpRequest({method:"GET",url:link1,
onload:function(p) { return function(responseDetails) {
// p is is the current progress element
// the first linked article starting with 'S' is *special*
var link2 = responseDetails.responseText.match(/\/wiki\/S[^"]+/);
if(!link2) { p.innerHTML = "failed in request 1"; return;}
GM_xmlhttpRequest({method:"GET",url:"http://en.wikipedia.org"+link2[0],
onload:function(p2) { return function(responseDetails) {
// p2 is p, ie. progress
// link3 would contain the URL to the FLV in the real script
var link3 = responseDetails.responseHeaders.match(/Content-Length.+/);
if(!link3) { p2.innerHTML = "failed in request 2"; return;}
var elmNewContent = document.createElement('p');
elmNewContent.appendChild(document.createTextNode(link3));
p2.parentNode.insertBefore(elmNewContent, p2.nextSibling);
p2.innerHTML = " <em>Done</em>";
}}(p) // 3rd closure
}); // end of second xmlhttprequest
}}(this) // 2nd closure
}); // end of first xmlhttprequest
}}(cand.href), true); // 1st closure and end of addeventlistener
}
}
推荐答案
好吧,你可以通过为每个阶段创建单独的函数,然后调用阶段1调用阶段2等来提高可读性。所以,而不是
Well, you could improve readability by creating separate functions for each stage, then having stage 1 call stage 2, etc. So, instead of
request({onload: function(response) {
request({onload: function(response) {
request({onload: function(response) {
alert("psych!");
}});
}});
}});
您会有
request({onload: doTheNextThing});
function doTheNextThing(responseObject) {
request({onload: doTheRightThing});
}
function doTheRightThing(responseObject) {
request({onload: doTheLastThing});
}
function doTheLastThing(responseObject) {
alert("psych!");
}
这篇关于嵌套XMLHttpRequests有多个闭包是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!