我使用的Greasemonkey腳本與功能GM_xmlhttpRequest
它similair到xmlhttpRequest
,但允許跨站點腳本等如何在JavaScript中使用響應來組織http請求?
我做了一堆HTTP相同常,requsts各種網頁,然後使用的onload閱讀來自這些網頁的一些數據。根據結果,我製作了新的http-requst。這是一個例子,代碼可能不起作用,更多的是說明我正在使用的東西。
function calleniro(nicerows, attempt){
if(attempt === 1){
var who = nicerows.contact.firstname+' '+nicerows.contact.lastname;
var where = ''
}else if(attempt === 2){
var who = nicerows.contact.firstname+' '+nicerows.contact.lastname;
var where = nicerows.contact.postal;
}else if(attempt === 3){
var who = nicerows.contact.firstname+' '+nicerows.contact.lastname;
var where = nicerows.contact.adress;
}
var url = 'http://personer.eniro.se/resultat/'+who+'/'+where;
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(data) {
data = $.parseHTML(data.response);
var phone = $(data).find('.tel.row a').map(function(){
return $(this).text();
}).get();
//one company, save the data
if(vCard.length = 1){
//adding the company if we find a phonenumber or more.
if (phone.length > 0){
nicerows.contact.phone = phone;
}
more than one company.
}else if(vCard > 1){
attempt++;
calleniro(nicerows, attempt)
}
}
})
}
這一個bubushka娃娃水螅與不斷分支的onload功能非常快的圈。跟蹤發生的事情非常困難。我想更多的分隔功能,這樣的事情,例如:
var contact = callenrio(foo,bar)
//the next thing should happen after onload only.
if(contact.tel){
save(contact);
}
else{
callenrio(foobar,barfoo)
}
這太棒了!我之前沒有真正使用過面向對象的JavaScript。*現在閱讀它*如何構造類等等。我目前的想法是爲每種類型的http請求創建一個類。併爲每個http請求創建一個新對象。另一個問題是$ .ajax不能跨站點工作。 – Himmators