forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreasemonkey-tests.ts
220 lines (183 loc) · 6.47 KB
/
greasemonkey-tests.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/// <reference path="greasemonkey.d.ts" />
////////////////
// Global variable
////////////////
var title: string = unsafeWindow.document.title;
var scriptDescription: string = GM_info.script.description;
var scriptExcludes: string[] = GM_info.script.excludes;
var scriptIncludes: string[] = GM_info.script.includes;
var scriptMatches: string[] = GM_info.script.matches;
var scriptName: string = GM_info.script.name;
var scriptNamespace: string = GM_info.script.namespace;
var scriptResouces: Object = GM_info.script.resources;
var scriptRunAt: string = GM_info.script['run-at'];
var scriptUnwrap: boolean = GM_info.script.unwrap;
var scriptVersion: string = GM_info.script.version;
var scriptMetsStr: string = GM_info.scriptMetaStr;
var scriptWillUpdate: boolean = GM_info.scriptWillUpdate;
var gmVersion: string = GM_info.version;
////////////////
// Values
////////////////
GM_setValue('a', 'foobar');
GM_setValue('b', 123);
GM_setValue('c', true);
GM_setValue('d', null);
// NG: GM_setValue('x', new Date());
var a: string = GM_getValue('a', 'foobar');
var b: number = GM_getValue('b', 123);
var c: boolean = GM_getValue('c', true);
var d: any = GM_getValue('d', null);
var e: string = GM_getValue('e');
var f: number = GM_getValue('f');
var g: boolean = GM_getValue('g');
// NG: var x: string = GM_getValue('x', 123);
GM_deleteValue('d');
GM_listValues().forEach((name: string) => {
console.log(name + ":", GM_getValue(name));
});
////////////////
// Resources
////////////////
var prototypeSource: string = GM_getResourceText('prototype');
var prototypeURL: string = GM_getResourceURL('prototype');
////////////////
// Utilities
////////////////
GM_addStyle("body { color: white; background-color: black; } img { border: 0; }");
GM_log("Hello, World!");
GM_openInTab("http://www.example.com/");
GM_registerMenuCommand("Hello, world (simple)", helloSimple);
GM_registerMenuCommand("Hello, world!", hello, "h");
// NG (Old Style): GM_registerMenuCommand("Hello, world! (again)", hello2, "e", "shift alt", "w");
function helloSimple() {}
function hello() {}
GM_setClipboard('http://www.example.com/short-url-code');
////////////////
// XMLHttpRequest
////////////////
//// Examples from Greasemonkey Wiki
// Bare Minimum
GM_xmlhttpRequest({
method: "GET",
url: "http://www.example.com/",
onload: function(response) {
alert(response.responseText);
}
});
// GET request
GM_xmlhttpRequest({
method: "GET",
url: "http://www.example.net/",
headers: {
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
onload: function(response) {
var responseXML = (<any>response).responseXML;
// Inject responseXML into existing Object (only appropriate for XML content).
if (!responseXML) {
responseXML = new DOMParser()
.parseFromString(response.responseText, "text/xml");
}
GM_log([
response.status,
response.statusText,
response.readyState,
response.responseHeaders,
response.responseText,
response.finalUrl,
responseXML
].join("\n"));
}
});
// POST request
GM_xmlhttpRequest({
method: "POST",
url: "http://www.example.net/login",
data: "username=johndoe&password=xyz123",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
if (response.responseText.indexOf("Logged in as") > -1) {
location.href = "http://www.example.net/dashboard";
}
}
});
// HEAD request
GM_xmlhttpRequest({
url: "http://www.example.com",
method: "HEAD",
onload: function(response) {
GM_log(response.responseHeaders);
}
});
//// Checkk all options
var result = GM_xmlhttpRequest({
binary: false,
context: {},
data: 'foo=1&bar=2',
headers: { 'User-Agent': 'greasemonkey' },
method: 'POST',
onabort: (response: GMXMLHttpRequestResponse) => { },
onerror: (response: GMXMLHttpRequestResponse) => { },
onload: (response: GMXMLHttpRequestResponse) => { },
onprogress: (response: GMXMLHttpRequestProgressResponse) => { },
onreadystatechange: (response: GMXMLHttpRequestResponse) => { },
ontimeout: (response: GMXMLHttpRequestResponse) => { },
overrideMimeType: 'text/plain',
password: 'abc123',
synchronous: false,
timeout: 10,
upload: {
onabort: (response: GMXMLHttpRequestResponse) => { },
onerror: (response: GMXMLHttpRequestResponse) => { },
onload: (response: GMXMLHttpRequestResponse) => { },
onprogress: (response: GMXMLHttpRequestProgressResponse) => { }
},
url: 'http://example.com/',
user: 'guest'
});
//// Check responses
GM_xmlhttpRequest({
method: 'GET',
url: 'http://example.com/',
onload: (response: GMXMLHttpRequestResponse) => {
var readyState: number = response.readyState;
var responseHeaders: string = response.responseHeaders;
var responseText: string = response.responseText;
var status: number = response.status;
var statusText: string = response.statusText;
var context: any = response.context;
var finalUrl: string = response.finalUrl;
// NG: var loaded: number = response.loaded;
},
onprogress: (response: GMXMLHttpRequestProgressResponse) => {
var status: number = response.status;
var lengthComputable: boolean = response.lengthComputable;
var loaded: number = response.loaded;
var total: number = response.total;
}
});
//// Synchronous
var syncResult: GMXMLHttpRequestSyncResult = GM_xmlhttpRequest({
method: 'GET',
url: 'http://example.com/',
synchronous: true
});
syncResult.abort();
var finalUrl: string = syncResult.finalUrl;
var readyState: number = syncResult.readyState;
var responseHeaders: string = syncResult.responseHeaders;
var responseText: string = syncResult.responseText;
(function() { var status: number = syncResult.status; })(); // conflict with state defined in lib.d.ts
var statusText: string = syncResult.statusText;
//// Asynchronous
var asyncResult: GMXMLHttpRequestAsyncResult = GM_xmlhttpRequest({
method: 'GET',
url: 'http://example.com/',
synchronous: false
});
asyncResult.abort();
// NG: var status: number = asyncResult.status;