/
Utils.user.js
157 lines (151 loc) · 3.33 KB
/
Utils.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
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
// ==UserScript==
// @name Common.Utils
// @description Classes for your scripts
// @author Anton Shevchuk
// @license MIT License
// @version 0.0.7
// @match *://*/*
// @grant none
// @namespace https://greasyfork.org/users/227648
// ==/UserScript==
/**
* Object with wrapper with getters and setters
*/
class Container {
constructor() {
this.container = {};
}
/**
* @param {String[]} keys
* @param {String} value
*/
set(keys, value) {
this._set(this.container, keys, value);
}
_set(elements, keys, value) {
let key = keys.shift();
if (typeof elements[key] === 'undefined') {
elements[key] = {};
}
if (keys.length === 0) {
elements[key] = value;
} else {
this._set(elements[key], keys, value);
}
}
/**
* @param {String} keys
* @return {null|*}
*/
get(...keys) {
if (keys.length === 0) {
return this.container
}
if (this.has(...keys)) {
return this._get(this.container, ...keys);
} else {
return null;
}
}
_get(elements, ...keys) {
let key = keys.shift();
if (typeof elements[key] === 'undefined') {
return null;
}
if (keys.length === 0) {
return elements[key];
} else {
return this._get(elements[key], ...keys);
}
}
/**
* @param {String} keys
* @return {boolean}
*/
has(...keys) {
return this._has(this.container, ...keys);
}
_has(elements, ...keys) {
let key = keys.shift();
if (typeof elements[key] === 'undefined') {
return false;
}
if (keys.length === 0) {
return true;
} else {
return this._has(elements[key], ...keys);
}
}
}
/**
* Simple cache object with getters and setters
*/
class SimpleCache extends Container {
/**
* @param {String} key
* @param {*} value
*/
set(key, value) {
super.set([key], value);
}
}
/**
* Settings object with localStorage as storage
*/
class Settings extends Container {
constructor(uid, def = {}) {
super();
this.uid = uid;
this.default = def;
this.load();
}
load() {
let settings = localStorage.getItem(this.uid);
if (settings) {
settings = JSON.parse(settings);
this.container = Tools.mergeDeep({}, this.default, settings);
} else {
this.container = this.default;
}
}
/**
* With jQuery:
* $(window).on('beforeunload', () => SettingsInstance.save() );
*/
save() {
localStorage.setItem(this.uid, JSON.stringify(this.container));
}
}
/**
* Static functions
*/
class Tools {
/**
* Simple object check
* @param {Object} item
* @returns {Boolean}
*/
static isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* Deep merge objects
* @param {Object} target
* @param {Array} sources
*/
static mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (Tools.isObject(target) && Tools.isObject(source)) {
for (const key in source) {
if (Tools.isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
Tools.mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return Tools.mergeDeep(target, ...sources);
}
}