-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomfetch.js
More file actions
117 lines (96 loc) · 3.06 KB
/
Copy pathdomfetch.js
File metadata and controls
117 lines (96 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*global XMLHttpRequest,console,ActiveXObject*/
var DOMFetch = (function () {
"use strict";
/*
params = {
url: "http://somethingtofetch",
selector: "#something ul li",
isCached: false,
callback: function(data) {
something();
}
}
*/
var DOMFetchURL = "http://domfetcher.cloudapp.net/get";
var slug = null;
var hasCORS = (function () {
if ("withCredentials" in new XMLHttpRequest()) {
return true;
} else if (window.XDomainRequest) {
return true;
} else {
return false;
}
}());
// helper - $.AJAX
if (typeof XMLHttpRequest === "undefined") {
XMLHttpRequest = function () {
try {
return new ActiveXObject('Msxml2.XMLHTTP.6.0');
} catch (e) { }
try {
return new ActiveXObject('Msxml2.XMLHTTP.3.0');
} catch (e) { }
try {
return new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) { }
throw new Error('This browser does not support XMLHttpRequest.');
};
}
// helper - $.AJAX
var getCORS = function (url, callback) {
var xmlresponse;
var POSTString = "fetchURL=" + url;
POSTString += (slug !== null) ? "&slug=" + slug : "";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', DOMFetchURL);
xmlhttp.send("fetchURL=" + url);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
if (xmlhttp.responseXML) {
xmlresponse = xmlhttp.responseXML;
} else if (xmlhttp.responseText) {
xmlresponse = xmlhttp.responseText;
}
callback(xmlresponse);
}
};
};
// helper - $.getJSON
var getJSONP = function (params) {
var callbackid = Math.floor(Math.random() * 100000);
DOMFetch["callback" + callbackid] = params.callback;
DOMFetch["error" + callbackid] = params.error;
var getURL = DOMFetchURL + "?url=" + encodeURIComponent(params.url);
getURL += "&selector=" + encodeURIComponent(params.selector);
getURL += "&uniqueid=" + callbackid;
if (params.type) {
getURL += "&type=" + params.type;
}
if (params.runon) {
getURL += "&runon=" + params.runon;
}
if (params.timeout) {
getURL += "&timeout=" + params.timeout;
}
console.log(getURL);
var fileref = document.createElement("script");
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", getURL);
document.body.appendChild(fileref);
};
var get = function (params) {
//if (hasCORS) {
// $.get(params.url)
//getCORS(params.url,params.callback);
//} else {
// $.getJSON(params.url)
getJSONP(params);
//}
};
return {
get: get,
hasCORS: hasCORS
};
}());