forked from Tylian/XKit
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathbridge.js
287 lines (224 loc) · 7.79 KB
/
bridge.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Bridge2 for XKit
Version 2.2.1
(c) 2011 - 2014 STUDIOXENIX
(c) 2015 - 2018 the New XKit Team and Contributors (https://github.com/new-xkit/XKit/contributors)
*/
/* globals msBrowser */
if (typeof(browser) === 'undefined') {
if (typeof(chrome) !== 'undefined') {
browser = chrome; // eslint-disable-line no-global-assign
} else if (typeof(msBrowser) !== 'undefined') {
browser = msBrowser; // eslint-disable-line no-global-assign
}
}
var bridge_error = false;
var bridge_error_object;
var xkit_storage = {};
var bridge_ver = "2.2.1";
try {
var storage = browser.storage.local;
var storage_loaded = false;
var framework_version = getVersion(); // eslint-disable-line no-redeclare
var storage_used = 0; // eslint-disable-line no-redeclare
var storage_max = -1; // eslint-disable-line no-redeclare
init_bridge();
} catch (e) {
console.log("[XKIT] Caught bridge error: " + e.message);
bridge_error_object = e;
bridge_error = true;
try {
call_xkit();
} catch (em) {
alert("Fatal XKit Error:\n" + em.message + "\n\nPlease go to new-xkit-extension.tumblr.com for support.");
console.log("[XKIT] Caught bridge error: " + em.message);
}
}
function getBridgeError() { // eslint-disable-line no-redeclare
var m_object = {};
m_object.errors = bridge_error;
m_object.error = bridge_error_object;
return m_object;
}
function getVersion() {
var xhr = new XMLHttpRequest();
xhr.open('GET', browser.extension.getURL('manifest.json'), false);
xhr.send(null);
var manifest = JSON.parse(xhr.responseText);
return manifest.version;
}
function call_xkit() {
if (typeof XKit !== "undefined") {
XKit.init();
} else {
setTimeout(function() { call_xkit(); }, 1);
}
}
function init_bridge() {
var last_error = "";
console.log("[XKit Bridge] Hello from Bridge " + bridge_ver);
console.log("[XKit Bridge] Retrieving storage..");
try {
storage.get(function(items) {
if (browser.runtime.lastError) {
last_error = browser.runtime.lastError.message;
console.log("storage.get error: " + last_error);
}
if (last_error !== "") {
XKit.window.show("Corrupt storage", "XKit noticed that your browser's storage area allocated for XKit is corrupt and will now reset itself and clear the storage area so it can save it data and function properly.<br/><br/><b>Your browser returned the following error message:</b><br/>\"" + last_error + "\"<br/><br/>If you keep seeing this message, it means your browser's profile file is corrupt, please try uninstalling and reinstalling New XKit", "error", "<div class=\"xkit-button default\" id=\"xkit-bridge-reset-and-continue\">OK</div>");
$("#xkit-bridge-reset-and-continue").click(function() {
GM_flushStorage(function() {
init_bridge();
});
});
return;
}
if (typeof(chrome) === 'undefined') {
if (!items.isProperlyMigrated) {
XKit.window.show("Storage migration in progress", "XKit is still busy migrating your preferences from the old storage system to the new one. Please check back in a couple seconds by refreshing the page.", "warning", "<div class=\"xkit-button default\" id=\"xkit-bridge-refresh\">Refresh</div>");
$("#xkit-bridge-refresh").click(function() {
window.location.reload();
});
return;
}
}
for (var key in items) {
xkit_storage[key] = items[key];
}
storage_loaded = true;
console.log("[XKit Bridge] Storage loaded, calling XKit.. bye!");
if (storage.getBytesInUse) {
storage.getBytesInUse(function(bytes) {
storage_used = bytes;
storage_max = -1;
call_xkit();
});
} else {
call_xkit();
}
});
} catch (e) {
XKit.window.show("Corrupt storage", "XKit noticed that your browser's storage area allocated for XKit is corrupt and will now reset itself and clear the storage area so it can save it data and function properly.<br/><br/><b>Your browser returned the following error message:</b><br/>\"" + last_error + "\"<br/><br/>If you keep seeing this message, it means your browser's profile file is corrupt, please try uninstalling and reinstalling New XKit", "error", "<div class=\"xkit-button default\" id=\"xkit-bridge-reset-and-continue\">OK</div>");
$("#xkit-bridge-reset-and-continue").click(function() {
GM_flushStorage(function() {
init_bridge();
});
});
return;
}
}
function GM_flushStorage(callback) { // eslint-disable-line no-redeclare
storage.remove("xkit_something", function() {
storage.clear(function(items) {
var last_error = 'unknown error';
if (browser.runtime.lastError) {
last_error = browser.runtime.lastError.message;
}
console.log("storage.clear error: " + last_error);
callback();
});
});
}
function GM_deleteAllValues(callback) { // eslint-disable-line no-redeclare
storage.get(function(items) {
for (var key in items) {
GM_deleteValue(key);
}
storage.clear();
callback();
});
}
function GM_getValue(name, defaultValue) { // eslint-disable-line no-redeclare
//console.log("Bridge : GM_getValue for " + name);
var value = xkit_storage[name];
if (!value) {
// console.log(" --- Returning default value.");
return defaultValue;
} else {
// console.log(" --- Returning " + value.substring(0,60) + "...");
return value;
}
}
function GM_deleteValue(name) { // eslint-disable-line no-redeclare
//console.log("Bridge : GM_deleteValue for " + name);
storage.remove(name);
delete xkit_storage[name];
}
function GM_setValue(name, value) { // eslint-disable-line no-redeclare
var m_object = {};
var m_name = name;
m_object[ m_name ] = value;
xkit_storage[name] = value;
storage.set(m_object);
return true;
}
function GM_log(message) { // eslint-disable-line no-redeclare
console.log(message);
}
function GM_openInTab(url) { // eslint-disable-line no-redeclare
return window.open(url, "_blank");
}
function GM_xmlhttpRequest(settings) { // eslint-disable-line no-redeclare
try {
var request = new XMLHttpRequest();
var timeout = 1;
if (settings.url.indexOf("http://") != -1 && settings.url.indexOf("tumblr.com/svc/") != -1) {
try {
console.log(" -- Bridge forwarding to HTTPS!");
settings.url = settings.url.replace("http://", "https://");
timeout = 1;
} catch (e) {
console.log(" -- Bridge forwarding to HTTPS FAIL..!");
}
}
settings.url = settings.url.replace("http://api.tumblr.com", "https://api.tumblr.com");
if (settings.url.indexOf("http://www.tumblr.com/") === 0) {
try {
console.log(" -- Bridge forwarding to HTTPS! (Dashboard)");
settings.url = settings.url.replace("http://", "https://");
timeout = 1;
} catch (e) {
console.log(" -- Bridge forwarding to HTTPS FAIL..!");
}
}
setTimeout(function() {
if (settings.method === "POST") {
request.open('POST', settings.url, true);
} else {
request.open('GET', settings.url, true);
}
request.onreadystatechange = function(oEvent) {
if (request.readyState === 4) {
if (request.status === 200) {
if (typeof settings.onload !== "undefined") {
settings.onload.call(request, request);
}
} else {
if (typeof settings.onerror !== "undefined") {
settings.onerror.call(request, request);
}
}
}
};
if (typeof settings.headers !== "undefined") {
for (var obj in settings.headers) {
request.setRequestHeader(obj, settings.headers[obj]);
}
}
if (settings.method === "POST") {
if (settings.json === true) {
request.setRequestHeader('Content-Type', "application/json");
console.log(" -- Bridge requesting post with json mode on");
} else {
request.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
console.log(" -- Bridge requesting post with json mode off");
}
request.send(settings.data);
} else {
request.send(null);
}
}, timeout);
} catch (e) {
console.log("Bridge can not make request: " + e.message);
}
}