如何使Greasemonkey的同步AJAX调用? [英] How to make synchronous AJAX calls in greasemonkey?

查看:392
本文介绍了如何使Greasemonkey的同步AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址列表,需要加载每个页面,此起彼伏。照片 这是我的主要功能,我在我的脑海里。

  mainFunction(){
循环{//循环网址列表
oPage = func1的(URL); //获取页面内容
综合因素的产物= FUNC2(oPage); //分析的内容
FUNC3(综合因素的产物); //完成当前页面的修改
}
}
 

FUNC1 使用GM_xmlhtt prequest,这是异步的,因为函数结束之前可以检索页面中的内容underfined所以​​oPage结果。
FUNC2 还采用GM_xmlhtt prequest,所以即使不管oPage是不确定的,综合因素的产物将不确定了。

如何使所有这些工作的任何想法?

FUNC1 FUNC2 FUNC3 应在整个可重复使用脚本,每个这些功能可以独立地或一起使用的脚本的不同部分。

解决方案

  VAR的网址= [];

(递归函数(名单)
{
    如果(名单[0])//列表不为空
    GM_xmlhtt prequest({//这将是FUNC1
        URL:列表[0],//第一个URL列表
        的onload:功能(XHR)
        {
            VAR oPage = xhr.responseText,//页面内容
            综合因素的产物= FUNC2(oPage); //分析内容
            FUNC3(综合因素的产物); //做当前页面修改

            list.shift(); //删除列表的第一连杆
            递归(清单); //转到下一个网址列表
        }
    });
    其他
    (端列表)警报;
})(网址);
 

还没有测试它,但你有这个想法

I have a list of URLs and need to load each page, one after another.
This is my main function that i have in my Mind.

mainFunction() {  
loop {  // Loop through URL list
oPage = func1(URL); //Get page contents
aResult = func2(oPage); //Analyse the contents
func3(aResult); //Do current page modifications
}  
}

func1 uses GM_xmlhttprequest, which is asynchronous, so oPage results in 'underfined' as function ends BEFORE the contents of a page could be retrieved.
func2 also uses GM_xmlhttprequest, so even no matter if oPage was undefined, aResult will be undefined too.

Any ideas on how to make all of this work?

func1 func2 and func3 should be reusable throughout the script, each of these functions may be used independently or together in different parts of script.

解决方案

var urls = [];

(function recursive(list)
{
    if (list[0])    // the list is not empty
    GM_xmlhttpRequest({ // that would be "func1"
        "url" : list[0],    // first url in the list
        "onload" : function(xhr)
        {
            var oPage = xhr.responseText,   // page contents
            aResult = func2(oPage); // analyse the contents
            func3(aResult); // do current page modifications

            list.shift();   // remove the first link of the list
            recursive(list);    // go to the next url in the list
        }
    });
    else
    alert("end of list");
})(urls);

haven't tested it but you got the idea

这篇关于如何使Greasemonkey的同步AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆