forked from greasemonkey/greasemonkey
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api-provider-source.js
175 lines (146 loc) · 4.41 KB
/
api-provider-source.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
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
/*
Generate the string source of the script-side API providers for a given
user script. This source is concatenated with the script itself for injection.
This will be an anonymous and immediately called function which exports objects
to the global scope (the `this` object). It ...
*/
const SUPPORTED_APIS = new Set([
'GM.getResourceUrl',
'GM.deleteValue', 'GM.getValue', 'GM.listValues', 'GM.setValue',
'GM.xmlHttpRequest',
]);
(function() {
function apiProviderSource(userScript) {
const grants = userScript.grants;
if (!grants || grants.length == 0
|| (grants.length == 1 && grants[0] == 'none')
) {
return '/* No grants, no APIs. */';
}
let source = '(function() {\n';
// A private copy of the script UUID which cannot be tampered with.
source += 'const _uuid = "' + userScript.uuid + '";\n\n';
if (grants.includes('GM.getResourceUrl')) {
source += 'GM.getResourceUrl = ' + GM_getResourceUrl.toString() + ';\n\n';
}
if (grants.includes('GM.deleteValue')) {
source += 'GM.deleteValue = ' + GM_deleteValue.toString() + ';\n\n';
}
if (grants.includes('GM.getValue')) {
source += 'GM.getValue = ' + GM_getValue.toString() + ';\n\n';
}
if (grants.includes('GM.listValues')) {
source += 'GM.listValues = ' + GM_listValues.toString() + ';\n\n';
}
if (grants.includes('GM.setValue')) {
source += 'GM.setValue = ' + GM_setValue.toString() + ';\n\n';
}
if (grants.includes('GM.xmlHttpRequest')) {
source += 'GM.xmlHttpRequest = ' + GM_xmlHttpRequest.toString() + ';\n\n';
}
// TODO: GM_registerMenuCommand -- maybe.
// TODO: GM_getResourceText -- maybe.
source += '})();';
return source;
}
window.apiProviderSource = apiProviderSource;
function GM_getResourceUrl(name) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
'name': 'ApiGetResourceBlob',
'resourceName': name,
'uuid': _uuid,
}, result => {
if (result) {
resolve(URL.createObjectURL(result.blob))
} else {
reject(`No resource named "${name}"`);
}
});
});
}
function GM_deleteValue(key) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
'key': key,
'name': 'ApiDeleteValue',
'uuid': _uuid,
}, result => result ? resolve() : reject());
});
}
function GM_getValue(key, defaultValue) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
'key': key,
'name': 'ApiGetValue',
'uuid': _uuid,
}, result => {
if (result !== undefined) {
resolve(result)
} else {
resolve(defaultValue);
}
});
});
}
function GM_listValues() {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
'name': 'ApiListValues',
'uuid': _uuid,
}, result => resolve(result));
});
}
function GM_setValue(key, value) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({
'key': key,
'name': 'ApiSetValue',
'uuid': _uuid,
'value': value,
}, result => {
if (result !== undefined) {
resolve(result);
} else {
console.warn('set value failed:', chrome.runtime.lastError);
reject();
}
});
});
}
function GM_xmlHttpRequest(d) {
if (!d) throw new Error('GM.xmlHttpRequest: Received no details.');
if (!d.url) throw new Error('GM.xmlHttpRequest: Received no URL.');
let url;
try {
url = new URL(d.url, location.href);
} catch (e) {
throw new Error(
'GM.xmlHttpRequest: Could not understand the URL: ' + d.url
+ '\n' + e);
}
if (url.protocol != 'http:'
&& url.protocol != 'https:'
&& url.protocol != 'ftp:'
) {
throw new Error('GM.xmlHttpRequest: Passed URL has bad protocol: ' + d.url);
}
let port = chrome.runtime.connect({name: 'UserScriptXhr'});
port.onMessage.addListener(function(msg) {
let o = msg.src == 'up' ? d.upload : d;
let cb = o['on' + msg.type];
if (cb) cb(msg.responseState);
});
let noCallbackDetails = {};
Object.keys(d).forEach(k => {
let v = d[k];
noCallbackDetails[k] = v;
if ('function' == typeof v) noCallbackDetails[k] = true;
});
noCallbackDetails.upload = {};
d.upload && Object.keys(k => noCallbackDetails.upload[k] = true);
noCallbackDetails.url = url.href;
port.postMessage({name: 'open', details: noCallbackDetails});
// TODO: Return an object which can be `.abort()`ed.
}
})();