-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjQuery-and-common-function-shortcuts-everywhere.user.js
74 lines (69 loc) · 2.35 KB
/
jQuery-and-common-function-shortcuts-everywhere.user.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
// ==UserScript==
// @name jQuery and common function shortcuts everywhere
// @namespace https://github.com/Alistair1231/my-userscripts/
// @version 0.5.1
// @description injects jquery if not exists and adds some common function shortcuts to the window object. See al.help() for details.
// @author Alistair1231
// @match *://*/*
// @grant GM_xmlhttpRequest
// @license MIT
// ==/UserScript==
// https://greasyfork.org/en/scripts/439017-jquery-and-common-function-shortcuts-everywhere
(function () {
'use strict';
const helpString = `
'\
al: jQuery and Method shortcuts everywhere\\n\
------------------------------------------\\n\
al.cl(str) - console.log(str)\\n\
al.js(obj) - JSON.stringify(obj)\\n\
al.jsp(obj) - JSON.stringify(obj, null, 2)\\n\
al.jp(str) - JSON.parse(str)\\n\
al.qs(selector) - document.querySelector(selector)\\n\
al.qsa(selector) - document.querySelectorAll(selector)\\n\
al.gid(id) - document.getElementById(id)\\n\
al.print() - Prints the object definition\\n\
al.eurl(str) - document.encodeURIComponent(str)\\n\
al.durl(str) - document.decodeURIComponent(str)\\n\
------------------------------------------\\n\
'
`;
const shortcuts = `
const al = {
help: () => console.log(${helpString}),
cl: (str) => console.log(str),
js: (obj) => JSON.stringify(obj),
jsp: (obj) => JSON.stringify(obj, null, 2),
jp: (str) => JSON.parse(str),
qs: (selector) => document.querySelector(selector),
qsa: (selector) => document.querySelectorAll(selector),
gid: (id) => document.getElementById(id),
print: () => {
const { help, ...rest } = al;
console.log(rest);
},
eurl: (str) => document.encodeURIComponent(str),
durl: (str) => document.decodeURIComponent(str)
};
`;
const e = document.createElement('script');
e.id = 'injectedScript';
e.innerText = shortcuts;
document.head.appendChild(e);
if (typeof $ != 'function') {
window.$ = function (selector, target) {
if (typeof target == 'undefined') {
target = document;
}
return target.querySelector(selector);
}
}
if (typeof $$ != 'function') {
window.$$ = function (selector, target) {
if (typeof target == 'undefined') {
target = document;
}
return Array.from(target.querySelectorAll(selector));
}
}
})();