-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcore.js
More file actions
451 lines (370 loc) · 12.9 KB
/
core.js
File metadata and controls
451 lines (370 loc) · 12.9 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
var pluginSettings = {
randomOption: "default",
tabOption: "default",
tabSetActive: true,
disableAutomaticRefresh: false,
randomizeHistory: false,
maxHistory: 10,
showContextMenu: false,
showContextOpenCountMenu: false,
contextMenuName: "randombookmarkContext",
loadingGroups: false,
loadingBookmarks: false,
initialLoading: true,
selectedGroup: "default",
isDebugging: false,
showActionNotice: false,
};
var sessionInfo = {
currentTabId: 0,
loadingDateTimeStarted: null,
};
async function loadUserSettings() {
logToDebugConsole("loadUserSettings");
var resSync = await browser.storage.sync.get();
var randomOpt = "default";
if (resSync.randomOption === "bybookmark") {
randomOpt = "bybookmark";
} else if (resSync.randomOption === "alphabetical") {
randomOpt = "alphabetical";
}
pluginSettings.randomOption = randomOpt;
pluginSettings.tabSetActive = typeof resSync.setActive !== "undefined" ? resSync.setActive : true;
pluginSettings.disableAutomaticRefresh = typeof resSync.disableAutomaticRefresh !== "undefined" ? resSync.disableAutomaticRefresh : false;
pluginSettings.randomizeHistory = typeof resSync.randomizeHistory !== "undefined" ? resSync.randomizeHistory : false;
pluginSettings.showContextMenu = typeof resSync.showContextMenu !== "undefined" ? resSync.showContextMenu : false;
pluginSettings.showContextOpenCountMenu = typeof resSync.showContextOpenCountMenu !== "undefined" ? resSync.showContextOpenCountMenu : false;
pluginSettings.showActionNotice = typeof resSync.showActionNotice !== "undefined" ? resSync.showActionNotice : false;
if (resSync.tabOption === "newTab" || resSync.tabOption === "currentTab") {
pluginSettings.tabOption = resSync.tabOption;
} else {
pluginSettings.tabOption = "default";
}
if (resSync.selectedFolders) {
// Updating from an older install version
changeToGroups(resSync.selectedFolders);
} else if (typeof resSync.groups === "undefined") {
// New install, set the default group to everything
changeToGroups([]);
}
}
function changeToGroups(selectedFolders) {
logToDebugConsole("changeToGroups");
var defaultBookmarks = [
{
name: "Default",
id: "default",
selected: selectedFolders,
reload: true,
index: 0,
},
];
browser.storage.sync.set({
groups: defaultBookmarks,
});
browser.storage.sync.remove("selectedFolders");
browser.storage.sync.remove("reloadBookmarks");
return defaultBookmarks;
}
async function loadContextMenus() {
logToDebugConsole("loadContextMenus");
removeContextOption(pluginSettings.contextMenuName);
if (pluginSettings.showContextMenu) {
await browser.menus.create({
id: pluginSettings.contextMenuName,
title: "Load Random Bookmark",
contexts: ["bookmark"],
});
}
for (var i = 2; i <= 10; i++) {
removeContextOption(`open-random-${i}`);
}
if (pluginSettings.showContextOpenCountMenu) {
for (var i = 2; i <= 10; i++) {
await browser.menus.create({
id: `open-random-${i}`,
title: "Load " + i + " Random Bookmarks",
contexts: ["bookmark"],
});
}
}
}
/// Set up the interactive icon action button at the top of the browser
/// When there's more than 5 bookmark groups, it'll group them into it's own dropdown for the right click action
async function loadBrowserActionGroups() {
logToDebugConsole("loadBrowserActionGroups");
const theme = await browser.theme.getCurrent();
const userAgent = navigator.userAgent.toLowerCase();
const isDarkTheme = (theme?.properties?.color_scheme || userAgent.includes("ubuntu")) ?? false;
var userLocalStorage = await browser.storage.local.get();
var userSyncOptions = await browser.storage.sync.get();
if (userSyncOptions.groups) {
var bookmarkGroupSettings = userSyncOptions.groups;
bookmarkGroupSettings.sort(compareBookmarkGroup);
var groupExists = bookmarkGroupSettings.find((obj) => {
return obj.id === pluginSettings.selectedGroup;
});
if (!groupExists) {
// Group no longer exists, set it back to default
pluginSettings.selectedGroup = "default";
browser.storage.local.set({
activeGroup: "default",
});
}
var parentId;
if (bookmarkGroupSettings.length > 5) {
removeContextOption("options-groupparent");
// Add the bookmark groups menu option
await browser.menus.create({
id: "options-groupparent",
type: "normal",
title: "Bookmark Groups",
contexts: ["browser_action"],
});
parentId = "options-groupparent";
}
// Add the default group
createContextOption("default", "Default", parentId);
// Add the rest of the groups
for (var i = 0; i < bookmarkGroupSettings.length; i++) {
if (bookmarkGroupSettings[i].id !== "default") {
createContextOption(bookmarkGroupSettings[i].id, bookmarkGroupSettings[i].name, parentId);
}
}
} else {
createContextOption("default", "Default");
}
removeContextOption("random-bookmark-options");
await browser.menus.create({
id: "random-bookmark-options",
type: "normal",
title: "Help",
contexts: ["browser_action"],
icons: {
16: `icons/bookmark-star${isDarkTheme ? "-white" : ""}-16.png`,
32: `icons/bookmark-star${isDarkTheme ? "-white" : ""}-32.png`,
},
});
// Add a shortcut to the options page
removeContextOption("options-page");
await browser.menus.create({
id: "options-page",
type: "normal",
title: "Random Bookmark Options",
contexts: ["browser_action"],
icons: {
16: `icons/gear${isDarkTheme ? "-white" : ""}-16.png`,
32: `icons/gear${isDarkTheme ? "-white" : ""}-32.png`,
},
parentId: "random-bookmark-options",
});
// Force cache reload of current group
removeContextOption("refresh-cache");
await browser.menus.create({
id: "refresh-cache",
type: "normal",
title: "Force Group Cache Refresh",
contexts: ["browser_action"],
icons: {
16: `icons/refresh${isDarkTheme ? "-white" : ""}-16.png`,
32: `icons/refresh${isDarkTheme ? "-white" : ""}-32.png`,
},
parentId: "random-bookmark-options",
});
// Will be used to update and show the last path
removeContextOption("last-bookmark-path");
await browser.menus.create({
id: "last-bookmark-path",
type: "normal",
title: "[Last Bookmark Path]",
contexts: ["browser_action"],
visible: false,
icons: {
16: `icons/asterisk${isDarkTheme ? "-white" : ""}-16.png`,
32: `icons/asterisk${isDarkTheme ? "-white" : ""}-32.png`,
},
parentId: "random-bookmark-options",
});
// Add shortcut to plugin page on firefox add-ons
removeContextOption("plugin-page");
await browser.menus.create({
id: "plugin-page",
type: "normal",
title: "Random Bookmark Add-on Info",
contexts: ["browser_action"],
icons: {
16: `icons/globe${isDarkTheme ? "-white" : ""}-16.png`,
32: `icons/globe${isDarkTheme ? "-white" : ""}-32.png`,
},
parentId: "random-bookmark-options",
});
// Check/preload the currently selected menu
preloadBookmarksIntoLocalStorage("loadBrowserActionGroups");
pluginSettings.loadingGroups = false;
if (userLocalStorage.activeGroup) {
pluginSettings.selectedGroup = userLocalStorage.activeGroup;
}
}
async function removeContextOption(id) {
logToDebugConsole("removeContextOption");
try {
await browser.menus.remove(id);
} catch (err) {}
}
async function createContextOption(id, name, parent) {
logToDebugConsole("createContextOption");
removeContextOption(id);
var menuItem = {
id: id,
type: "radio",
title: name,
checked: id === pluginSettings.selectedGroup ? true : false,
contexts: ["browser_action"],
};
if (typeof parent != "undefined" && parent !== "") {
menuItem.parentId = parent;
}
await browser.menus.create(menuItem);
}
async function preloadBookmarksIntoLocalStorage(source) {
logToDebugConsole("preloadBookmarksIntoLocalStorage", { source: source, pluginSettings: pluginSettings });
if (pluginSettings.loadingBookmarks === false) {
sessionInfo.loadingDateTimeStarted = Date.now();
pluginSettings.loadingBookmarks = true;
// Preload only the selected group.
var userSyncOptions = await browser.storage.sync.get();
var found = userSyncOptions.groups.filter((obj) => {
return obj.id === pluginSettings.selectedGroup;
});
if (found.length) {
var group = found[0];
if (group.reload) {
group.reload = false;
loadBookmarksIntoLocalStorage(group.id, group.selected);
const index = userSyncOptions.groups.findIndex((obj) => obj.id === pluginSettings.selectedGroup);
userSyncOptions.groups[index] = group;
browser.storage.sync.set({
groups: userSyncOptions.groups,
});
} else {
setTimeout(function () {
finishedLoading();
}, 250);
sessionInfo.loadingDateTimeStarted = null;
}
} else {
setTimeout(function () {
finishedLoading();
}, 250);
sessionInfo.loadingDateTimeStarted = null;
}
}
}
function loadBookmarksIntoLocalStorage(id, folders) {
logToDebugConsole("loadBookmarksIntoLocalStorage", { id: id, folders: folders });
if (folders.length > 0) {
var selectedPromises = [];
// Selected Bookmarks
for (var i = 0; i < folders.length; i++) {
selectedPromises.push(browser.bookmarks.getChildren(folders[i]));
}
processBookmarkPromises(id, selectedPromises);
} else {
// All Bookmarks
var allBookmarks = browser.bookmarks.getTree();
var allPromises = [allBookmarks];
processBookmarkPromises(id, allPromises);
}
}
function processBookmarkPromises(id, promises) {
logToDebugConsole("processBookmarkPromises", { id: id, promises: promises });
settlePromises(promises).then((results) => {
var bookmarksToSave = [];
results.forEach((result) => {
logToDebugConsole("processBookmarkPromises Result", result);
if (result.state === "fulfilled") {
for (var i = 0; i < result.value.length; i++) {
var r = processBookmarks(result.value[i], result.value[i].id === "root________");
bookmarksToSave = bookmarksToSave.concat(r);
}
}
});
var uniqueBookmarks = bookmarksToSave.filter(function (elem, index, self) {
return index === self.indexOf(elem);
});
if (pluginSettings.randomOption === "alphabetical") {
uniqueBookmarks.sort((a, b) => a.title.localeCompare(b.title, undefined, { sensitivity: "base" }));
} else {
Shuffle(uniqueBookmarks);
}
logToDebugConsole("Bookmarks to store", uniqueBookmarks);
browser.storage.local.set({
[id]: uniqueBookmarks.map((bookmark) => bookmark.id),
});
setTimeout(function () {
finishedLoading();
}, 250);
});
}
function processBookmarks(bookmarkItem, goDeeper) {
//logToDebugConsole('processBookmarks', { 'bookmarkItem': bookmarkItem, 'goDeeper': goDeeper });
var bookmarksCollection = [];
if (bookmarkItem.type === "folder") {
var result = getBookmarks(bookmarkItem.children);
bookmarksCollection = bookmarksCollection.concat(result);
} else if (bookmarkItem.type === "bookmark") {
bookmarksCollection.push({ id: bookmarkItem.id, title: bookmarkItem.title });
}
if (bookmarkItem.children && goDeeper) {
for (child of bookmarkItem.children) {
var result = processBookmarks(child, goDeeper);
bookmarksCollection = bookmarksCollection.concat(result);
}
}
return bookmarksCollection;
}
function getBookmarks(bookmarkFolder) {
logToDebugConsole("getBookmarks", { bookmarkFolder: bookmarkFolder });
var bookmarksCollection = [];
if (typeof bookmarkFolder !== "undefined" && bookmarkFolder !== null)
for (var i = 0; i < bookmarkFolder.length; i++) {
if (bookmarkFolder[i].type === "bookmark") {
bookmarksCollection.push({ id: bookmarkFolder[i].id, title: bookmarkFolder[i].title });
}
}
return bookmarksCollection;
}
function finishedLoading() {
pluginSettings.loadingBookmarks = false;
pluginSettings.initialLoading = false;
}
function onError(e) {
console.error(e);
}
function logToDebugConsole(what, data) {
if (pluginSettings.isDebugging) {
console.log(what, data);
}
}
// https://stackoverflow.com/a/32979111/13690517
function settlePromises(arr) {
return Promise.all(
arr.map((promise) => {
return promise.then(
(value) => ({ state: "fulfilled", value }),
(value) => ({ state: "rejected", value })
);
})
);
}
async function getBookmarkPath(bookmarkId) {
let path = [];
let currentId = bookmarkId;
while (currentId) {
// Get the current bookmark or folder
let [currentBookmark] = await browser.bookmarks.get(currentId);
path.unshift(currentBookmark.title); // Add the title to the path
currentId = currentBookmark.parentId; // Move to the parent
}
return path.slice(0, -1).filter(Boolean).join(" > ");
}