forked from soscripted/sox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsox.common.js
338 lines (317 loc) · 13.1 KB
/
sox.common.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
(function(sox, $, undefined) {
'use strict';
var SOX_SETTINGS = 'SOXSETTINGS';
var commonInfo = JSON.parse(GM_getResourceText('common'));
sox.info = {
version: (typeof GM_info !== 'undefined' ? GM_info.script.version : 'unknown'),
handler: (typeof GM_info !== 'undefined' ? GM_info.scriptHandler : 'unknown'),
apikey: 'lL1S1jr2m*DRwOvXMPp26g((',
debugging: GM_getValue('SOX-debug', false)
};
sox.debug = function() {
if(!sox.info.debugging) return;
for (var arg = 0; arg < arguments.length; ++arg) {
console.debug('SOX: ', arguments[arg]);
}
};
sox.log = function() {
for (var arg = 0; arg < arguments.length; ++arg) {
console.log('SOX: ', arguments[arg]);
}
};
sox.warn = function() {
for (var arg = 0; arg < arguments.length; ++arg) {
console.warn('SOX: ', arguments[arg]);
}
};
sox.error = function() {
for (var arg = 0; arg < arguments.length; ++arg) {
console.error('SOX: ', arguments[arg]);
}
};
sox.loginfo = function() {
for (var arg = 0; arg < arguments.length; ++arg) {
console.info('SOX: ', arguments[arg]);
}
};
//var Stack = (typeof StackExchange === "undefined" ? window.eval('if (typeof StackExchange != "undefined") StackExchange') : StackExchange) | undefined;
var Chat = (typeof CHAT === "undefined" ? window.eval("typeof CHAT != 'undefined' ? CHAT : undefined") : CHAT);
sox.debug(Chat);
var Stack = (typeof Chat === "undefined" ? (typeof StackExchange === "undefined" ? window.eval('if (typeof StackExchange != "undefined") StackExchange') : StackExchange) : undefined);
sox.debug(Stack);
sox.exists = function(path) {
if(!Stack) return false;
var toCheck = path.split('.'),
cont = true,
o = Stack,
i;
for (i = 0; i < toCheck.length; i++) {
if (!cont) return false;
if (!(toCheck[i] in o)) cont = false;
o = o[toCheck[i]];
}
return cont;
};
sox.ready = function(func) {
$(function() {
if (Stack) {
if (Stack.ready) {
Stack.ready(func());
} else {
func();
}
} else {
func();
}
});
};
sox.settings = {
available: GM_getValue(SOX_SETTINGS, -1) != -1,
load: function() {
var settings = GM_getValue(SOX_SETTINGS);
return settings === undefined ? undefined : JSON.parse(settings);
},
save: function(settings) {
GM_setValue(SOX_SETTINGS, JSON.stringify(settings));
},
reset: function() {
var keys = GM_listValues();
sox.debug(keys);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
GM_deleteValue(key);
}
},
get accessToken() {
sox.debug('SOX Access Token: ' + (GM_getValue('SOX-accessToken', false) === false ? 'NOT SET' : 'SET'));
return GM_getValue('SOX-accessToken', false);
},
writeToConsole: function(hideAccessToken) {
sox.loginfo('logging sox stored values --- ');
var keys = GM_listValues();
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if(hideAccessToken && key == 'SOX-accessToken') {
sox.loginfo('access token set');
} else {
sox.loginfo(key, GM_getValue(key));
}
}
}
};
sox.helpers = {
getFromAPI: function(type, id, sitename, callback, sortby, asyncYesNo) {
sox.debug('Getting From API with URL: https://api.stackexchange.com/2.2/' + type + '/' + id + '?order=desc&sort=' + (sortby || 'creation') + '&site=' + sitename + '&key=' + sox.info.apikey + '&access_token=' + sox.settings.accessToken);
$.ajax({
type: 'get',
async: (typeof asyncYesNo === 'undefined' ? true : false),
url: 'https://api.stackexchange.com/2.2/' + type + '/' + id + '?order=desc&sort=' + (sortby || 'creation') + '&site=' + sitename + '&key=' + sox.info.apikey + '&access_token=' + sox.settings.accessToken,
success: function(d) {
if (d.backoff) {
sox.error('SOX Error: BACKOFF: ' + d.backoff);
} else {
callback(d);
}
},
error: function(a, b, c) {
sox.error('SOX Error: ' + b + ' ' + c);
}
});
},
observe: function(elements, callback, toObserve) {
sox.debug('observe: ' + elements);
sox.debug(toObserve);
var observer = new MutationObserver(function(mutations, observer) {
for (var i = 0; i < mutations.length; i++) {
for (var a = 0; a < mutations[i].addedNodes.length; a++) {
var $a = $(mutations[i].addedNodes[a]);
if ($a && $a.is((Array.isArray(elements) ? elements.join(',') : elements))) {
callback(mutations[i].addedNodes[a]);
sox.debug('fire (added): ' + elements);
}
}
for (var r = 0; r < mutations[i].removedNodes.length; r++) {
var $r = $(mutations[i].removedNodes[r]);
if ($r && $r.is((Array.isArray(elements) ? elements.join(',') : elements))) {
callback(mutations[i].addedNodes[r]);
sox.debug('fire (removed): ' + elements);
}
}
}
});
if (toObserve) {
for (var i = 0; i < toObserve.length; i++) { //could be multiple elements with querySelectorAll
observer.observe(toObserve[i], {
attributes: true,
childList: true,
characterData: true,
subtree: true
});
}
} else {
observer.observe(document.body, {
attributes: true,
childList: true,
characterData: true,
subtree: true
});
}
},
newElement: function(type, elementDetails) {
var extras = {},
allowed = ['text', 'checkbox', 'radio', 'textarea', 'span'];
if (allowed.indexOf(type) != -1) {
if (type == 'text') {
type = 'input';
extras.type = 'input';
} else if (type == 'checkbox') {
type = 'input';
extras.type = 'checkbox';
} else if (type == 'radio') {
type = 'input';
extras.type = 'radio';
} else if (type == 'textarea') {
if (!elementDetails.text) {
elementDetails.text = elementDetails.value;
}
}
$.each(elementDetails, function(k, v) {
extras[k] = v;
});
return $('<' + type + '/>', extras);
} else {
return false;
}
}
};
sox.site = {
types: {
main: 'main',
meta: 'meta',
chat: 'chat',
beta: 'beta'
},
id: (sox.exists('options.site.id') ? Stack.options.site.id : undefined),
get name() {
if (Chat) {
return $('#footer-logo a').attr('title');
} else { //using StackExchange object doesn't give correct name (eg. `Biology` is called `Biology Stack Exchange` in the object)
return $('.js-topbar-dialog-corral .modal-content.current-site-container .current-site-link div').attr('title');
}
return undefined;
},
get type() {
if (Chat) {
return this.types.chat;
} else if (Stack) {
if (sox.exists('options.site') && Stack.options.site.isMetaSite) {
return this.types.meta;
} else {
// check if site is in beta or graduated
if ($('.beta-title').length > 0) {
return this.types.beta;
} else {
return this.types.main;
}
}
}
},
apiParameter: function(siteName) {
if (commonInfo.apiParameters.hasOwnProperty(siteName)) {
return commonInfo.apiParameters[siteName];
} else if(sox.location.on('area51')) {
return 'area51';
}
},
metaApiParameter: function(siteName) {
if (Chat || this.apiParameter(siteName)) {
return 'meta.' + this.apiParameter(siteName);
}
},
get currentApiParameter() {
return this.apiParameter(this.name);
},
get icon() {
return "favicon-" + $(".current-site a:not([href*='meta']) .site-icon").attr('class').split('favicon-')[1];
},
url: location.hostname,
href: location.href
};
sox.location = {
// location helpers
on: function(location) {
return window.location.href.indexOf(location) > -1 ? true : false;
},
get onUserProfile() {
return this.on('/users/');
},
get onQuestion() {
return this.on('/questions/');
},
matchWithPattern: function(pattern, urlToMatchWith) { //commented version @ https://jsfiddle.net/shub01/t90kx2dv/
if(pattern == 'SE1.0') { //SE.com && Area51.SE.com special checking
if(urlToMatchWith) {
if(urlToMatchWith.match(/https?:\/\/stackexchange\.com\/?/) || sox.location.matchWithPattern('*://area51.stackexchange.com/*')) return true;
} else {
if(location.href.match(/https?:\/\/stackexchange\.com\/?/) || sox.location.matchWithPattern('*://area51.stackexchange.com/*')) return true;
}
return false;
}
var currentSiteScheme, currentSiteHost, currentSitePath;
if (urlToMatchWith) {
var split = urlToMatchWith.split('/');
currentSiteScheme = split[0];
currentSiteHost = split[2];
currentSitePath = '/' + split.slice(-(split.length - 3)).join('/');
} else {
currentSiteScheme = location.protocol;
currentSiteHost = location.hostname;
currentSitePath = location.pathname;
}
var matchSplit = pattern.split('/'),
matchScheme = matchSplit[0],
matchHost = matchSplit[2],
matchPath = matchSplit.slice(-(matchSplit.length - 3)).join('/');
matchScheme = matchScheme.replace(/\*/g, ".*");
matchHost = matchHost.replace(/\./g, "\\.").replace(/\*\\\./g, ".*.?").replace(/\\\.\*/g, ".*").replace(/\*$/g, ".*");
matchPath = '^\/' + matchPath.replace(/\//g, "\\/").replace(/\*/g, ".*");
if (currentSiteScheme.match(new RegExp(matchScheme)) && currentSiteHost.match(new RegExp(matchHost)) && currentSitePath.match(new RegExp(matchPath))) {
return true;
}
return false;
}
};
sox.user = {
get id() {
if (sox.site.type == sox.site.types.chat) {
return Chat ? Chat.RoomUsers.current().id : undefined;
} else {
return sox.exists('options.user.userId') ? Stack.options.user.userId : undefined;
}
},
get rep() {
if (sox.site.type == sox.site.types.chat) {
return Chat.RoomUsers.current().reputation;
} else {
return sox.exists('options.user.rep') ? Stack.options.user.rep : undefined;
}
},
get name() {
if (sox.site.type == sox.site.types.chat) {
return Chat.RoomUsers.current().name;
} else {
return Stack && this.loggedIn ? decodeURI(Stack.options.user.profileUrl.split('/')[5]) : undefined;
}
},
get loggedIn() {
return sox.exists('options.user.isRegistered') ? Stack.options.user.isRegistered : undefined;
},
hasPrivilege: function(privilege) {
if (this.loggedIn) {
var rep = (sox.site.type == 'beta' ? commonInfo.privileges.beta[privilege] : commonInfo.privileges.graduated[privilege]);
return this.rep > rep;
}
return false;
}
};
})(window.sox = window.sox || {}, jQuery);