-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
148 lines (130 loc) · 4.63 KB
/
utils.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
/*
Copyright 2023 Jonathan Kamens.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
var utils = {
perItemLimit: 8192,
debugging: false,
debug: function(...args) {
if (utils.debugging)
console.log.apply(null, ["LIJF"].concat(args));
},
valuesAreEqual: function(value1, value2) {
if (Array.isArray(value1)) {
if (!Array.isArray(value2)) return false;
if (value1.length != value2.length) return false;
for (var i = 0; i < value1.length; i++)
if (! utils.valuesAreEqual(value1[i], value2[i])) return false;
return true;
}
if (typeof(value1) == "object") {
if (typeof(value2) != "object") return false;
if (Object.keys(value1).length != Object.keys(value2).length)
return false;
for (var [key, value] of Object.entries(value1))
if (! utils.valuesAreEqual(value, value2[key])) return false;
return true;
}
return value1 == value2;
},
saveListToStorage: async function(name, items) {
var oldOptions = await chrome.storage.sync.get();
var perItemSize = utils.perItemLimit * 0.9; // 10% slack
var totalLength = JSON.stringify(items).length;
var numberOfFragments = Math.ceil(totalLength / perItemSize);
var itemsPerFragment = Math.ceil(items.length / numberOfFragments);
var options = {};
for (var i = 0; i < numberOfFragments; i++) {
var key = `${name}${i}`;
var start = itemsPerFragment * i;
var end = start + itemsPerFragment;
options[key] = items.slice(start, end);
}
await chrome.storage.sync.set(options);
var toRemove = [];
if (oldOptions[name] != undefined)
toRemove.push(name);
while (oldOptions[`${name}${i}`] != undefined) {
toRemove.push(`${name}${i}`);
i++;
}
if (toRemove.length)
await chrome.storage.sync.remove(toRemove);
},
readListFromStorage: async function(name) {
var options = await chrome.storage.sync.get();
// Backward compatibility 2023-03-22
if (options[name] != undefined)
return options[name];
var items = [];
for (var i = 0; options[`${name}${i}`] != undefined; i++)
items = items.concat(options[`${name}${i}`])
return items;
},
id: undefined,
regReg: /^\/(.*)\/([i]*)$/,
compileRegexps: function(regexps) {
utils.debug(`compileRegexps(${regexps})`);
if (! regexps) return [];
var compiled = [];
for (var regexp of regexps) {
var flags;
var match = utils.regReg.exec(regexp);
if (match) {
regexp = match[1];
flags = match[2];
}
else {
flags = "";
}
try {
regexp = new RegExp(regexp, flags);
}
catch (ex) {
console.log(ex.message);
continue;
}
compiled.push(regexp);
}
return compiled;
},
escapeHTML: function(unsafeText) {
let div = document.createElement('div');
div.innerText = unsafeText;
return div.innerHTML;
},
unparseJobFilters: function(filters) {
filters = filters.map(f => {
var filter = `${f.title} // ${f.company} // ${f.location}`;
if (f.private) {
filter += " // private";
}
return filter;
});
return filters;
},
};
// Borrowed from https://stackoverflow.com/a/1997811/937306
(function() {
if ( typeof utils.id != "undefined" ) return;
var _id = 0;
utils.id = function(o) {
if ( typeof o.__uniqueid != "undefined" ) {
return o.__uniqueid;
}
Object.defineProperty(o, "__uniqueid", {
value: `id${++_id}`,
enumerable: false,
writable: false
});
return o.__uniqueid;
};
})();