This repository has been archived by the owner on Jan 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
userscript.js
100 lines (89 loc) · 2.61 KB
/
userscript.js
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
function D () {
/*include JSDeferred*/
/* function xhttp (opts) //=> Deferred
* Cross site version of `http`.
*/
/* function xhttp.get (url) //=> Deferred
*/
/* function xhttp.post (url, data) //=> Deferred
*/
function xhttp (opts) {
var d = Deferred();
if (opts.onload) d = d.next(opts.onload);
if (opts.onerror) d = d.error(opts.onerror);
opts.onload = function (res) {
d.call(res);
};
opts.onerror = function (res) {
d.fail(res);
};
setTimeout(function () {
GM_xmlhttpRequest(opts);
}, 0);
return d;
}
xhttp.get = function (url) { return xhttp({method:"get", url:url}) };
xhttp.post = function (url, data) { return xhttp({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}) };
/* function http (opts) //=> Deferred
* Sample:
* http.get("http://example.com/hogehoge")
* .next(function (a) {
* log(a.responseText);
* })
* .error(function (e) {
* log("error", e);
* });
*/
/* function http.get (url) //=> Deferred
*/
/* function http.post (url, data) //=> Deferred
*/
function http (opts) {
var d = Deferred();
var req = new XMLHttpRequest();
req.open(opts.method, opts.url, true);
if (opts.headers) {
for (var k in opts.headers) if (opts.headers.hasOwnProperty(k)) {
req.setRequestHeader(k, opts.headers[k]);
}
}
req.onreadystatechange = function () {
if (req.readyState == 4) d.call(req);
};
req.send(opts.data || null);
d.xhr = req;
return d;
}
http.get = function (url) { return http({method:"get", url:url}) };
http.post = function (url, data) { return http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}) };
http.jsonp = function (url, params) {
if (!params) params = {};
var Global = (function () { return this })();
var d = Deferred();
var cbname = params["callback"];
if (!cbname) do {
cbname = "callback" + String(Math.random()).slice(2);
} while (typeof(Global[cbname]) != "undefined");
params["callback"] = cbname;
url += (url.indexOf("?") == -1) ? "?" : "&";
for (var name in params) if (params.hasOwnProperty(name)) {
url = url + encodeURIComponent(name) + "=" + encodeURIComponent(params[name]) + "&";
}
alert(url);
var script = document.createElement('script');
script.type = "text/javascript";
script.charset = "utf-8";
script.src = url;
document.body.appendChild(script);
Global[cbname] = function callback (data) {
delete Global[cbname];
document.body.removeChild(script);
d.call(data);
};
return d;
};
Deferred.Deferred = Deferred;
Deferred.http = http;
Deferred.xhttp = xhttp;
return Deferred;
}