-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildpage.js
169 lines (142 loc) · 4.09 KB
/
buildpage.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
158
159
160
161
162
163
164
165
166
167
168
169
let tabs_cache;
let star = "\u2606";
let cross = "\u2A2F";
browser.tabs.query({currentWindow: true}).then(tabs => {
tabs_cache = tabs;
let download_link = document.getElementById('download_json');
let serialized_tabs = JSON.stringify(tabs, ["title", "url"], 2);
download_link.setAttribute(
'href',
'data:application/json;charset=utf-8,' + encodeURIComponent(serialized_tabs)
);
download_link.setAttribute(
'download',
`list_tabs_${(new Date()).toISOString()}.json`
);
createTable(tabs);
});
document.getElementById('close_duplicates').onclick = closeDuplicates;
function createTable(tabs) {
var table = document.createElement("table");
var table_headers = ["#", star, cross, "", "Title & URL"];
createHeaderRow(table, table_headers);
var pinned_tabs = [];
var not_pinned_tabs = [];
for (let tab of tabs) {
if (tab.pinned) {
pinned_tabs.push(tab);
} else {
not_pinned_tabs.push(tab);
}
}
if (pinned_tabs.length > 0) {
createSeparatorRow(table, "Pinned:", table_headers.length);
createBunchOfTabRows(table, pinned_tabs);
createSeparatorRow(table, "Not pinned:", table_headers.length);
}
createBunchOfTabRows(table, not_pinned_tabs);
document.body.appendChild(table);
}
function createHeaderRow(table, headers) {
let row = table.insertRow(0);
for (let i = 0; i < headers.length; i++) {
let cell = row.insertCell(-1);
cell.classList.add("header");
cell.textContent = headers[i];
}
}
function createSeparatorRow(table, text, cols) {
let row = table.insertRow(-1);
row.classList.add("separator");
let cell = row.insertCell(-1);
cell.classList.add("separator");
cell.colSpan = cols;
cell.textContent = text;
}
function createBunchOfTabRows(table, tabs) {
for (let i = 0; i < tabs.length; i++) {
let tab = tabs[i];
createTabRow(table, tab.title, tab.url, i, tab.id, tab.favIconUrl);
}
}
function createTabRow(table, title, url, index, id, favIconUrl) {
var row = table.insertRow(-1);
row.classList.add("tab");
getNewTabCell(row).appendChild(createActivateButton(index, id));
getNewTabCell(row).appendChild(createBookmarkButton(id));
getNewTabCell(row).appendChild(createCloseButton(id));
getNewTabCell(row).appendChild(createFavicon(favIconUrl));
cell = getNewTabCell(row);
cell.appendChild(createTitle(title));
cell.appendChild(document.createElement("br"));
cell.appendChild(createURL(url));
}
function getNewTabCell(row) {
cell = row.insertCell(-1);
cell.classList.add("tab");
return cell;
}
function onActivateButtonAction() {
browser.tabs.update(parseInt(this.name), {active: true});
}
function onBookmarkButtonAction() {
browser.tabs.get(
parseInt(this.name),
(tab) => {
browser.bookmarks.create({
title: tab.title,
url: tab.url
}).then(
() => {this.disabled = true}
);
}
);
}
function onCloseButtonAction() {
browser.tabs.remove(parseInt(this.name)).then(() => {this.disabled = true});
}
function createButton(name, text, handler) {
var button = document.createElement("button");
button.name = name;
button.textContent = text;
button.onclick = handler;
button.classList.add("cellButton");
return button;
}
function createActivateButton(index, id) {
return createButton("" + id, "" + index, onActivateButtonAction);
}
function createBookmarkButton(id) {
return createButton("" + id, star, onBookmarkButtonAction);
}
function createCloseButton(id) {
return createButton("" + id, cross, onCloseButtonAction);
}
function createTitle(title) {
return document.createTextNode(title);
}
function createURL(url) {
var a = document.createElement("a");
a.classList.add("tabURL");
a.text = decodeURI(url);
a.href = url;
return a;
}
function createFavicon(url) {
var img = document.createElement("img");
img.src = url;
img.height = 16;
img.width = 16;
return img;
}
function closeDuplicates() {
let urls = new Set();
for (let tab of tabs_cache) {
if (urls.has(tab.url)) {
browser.tabs.remove(tab.id);
} else {
urls.add(tab.url);
}
}
document.location.reload(true);
}