/
tuck_it.js
109 lines (95 loc) · 3.39 KB
/
tuck_it.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
let gTabs;
function zero_pad_left(num, digits) {
return ("0".repeat(digits) + num.toString()).slice(-1 * digits);
}
function month_str(date) {
return date.getFullYear().toString().slice(2, 4)
+ "_"
+ zero_pad_left(date.getMonth() + 1, 2);
}
function entry_prefix(date) {
return zero_pad_left(date.getDate(), 2)
+ "."
+ zero_pad_left(date.getHours(), 2)
+ ":"
+ zero_pad_left(date.getMinutes(), 2);
}
function add_tabs(node, date) {
let prefix = entry_prefix(date);
let promiseList = [];
let counter = 1;
for (let tab of gTabs) {
if (gTabs.length == 1) {
promiseList.push(browser.bookmarks.create({
parentId: node.id,
title: prefix + " " + tab.title, url: tab.url
}));
} else {
let count_str = zero_pad_left(counter, gTabs.length.toString().length);
promiseList.push(browser.bookmarks.create({
parentId: node.id,
title: prefix
+ " "
+ (gTabs.length > 9 ? count_str : count_str.slice(-1))
+ " "
+ tab.title,
url: tab.url
}));
counter++;
}
}
return Promise.all(promiseList);
}
function error_print(error) {
console.log(`An error: ${error}`);
}
async function print_tabs(tabs) {
gTabs = tabs;
let date = new Date();
let root_search_list = await browser.bookmarks.search({title: "tuck_it"});
console.log('tuck_it = ', root_search_list);
let tuck_it_node = 0;
let month_node = 0;
if (root_search_list.length == 0) {
// Need to create the folder structure
console.log("Creating tuck_it folder at bookmarks toolbar...")
tuck_it_node = await browser.bookmarks.create({parentId: "toolbar_____", title: "tuck_it"});
month_node = await browser.bookmarks.create({parentId: tuck_it_node.id, title: month_str(date)});
} else {
for (const item of root_search_list) {
if (item.type === "folder" && item.title === "tuck_it") {
tuck_it_node = item;
break;
}
}
}
if (tuck_it_node == 0) {
console.log("Failed to find folder tuck_it");
return;
} else {
let month_result = await browser.bookmarks.search(month_str(date));
if (month_result == 0) {
// create month folder
month_node = await browser.bookmarks.create({parentId: tuck_it_node.id, title: month_str(date)});
} else {
for (const item of month_result) {
if (item.type === "folder" && item.title === month_str(date) && item.parentId == tuck_it_node.id)
month_node = item;
}
}
}
if (month_node == 0) {
console.log("Failed to find month folder");
return;
}
console.log("tuck_it_node = ", tuck_it_node);
console.log("month_node = ", month_node);
add_tabs(month_node, date).catch(error_print);
}
function tuck_it_action() {
function onError(error) {
console.log(`Error: ${error}`);
}
browser.tabs.query({currentWindow: true}).then(print_tabs, onError);
}
browser.action.onClicked.addListener(tuck_it_action);