-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabulence.js
99 lines (68 loc) · 2.04 KB
/
tabulence.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
// tabulence.js
var tabsInfo = document.getElementById('tabs-info');
var btnText = document.querySelector('.btn-text');
btnText.addEventListener('click', tabsInfoAsText);
var btnMd = document.querySelector('.btn-md');
btnMd.addEventListener('click', tabsInfoAsMd);
var btnLi = document.querySelector('.btn-li');
btnLi.addEventListener('click', tabsInfoAsLi);
var btnText = document.querySelector('.btn-select');
btnText.addEventListener('click', selectInfoText);
function tabsInfoAsText() {
showTabsInfo(fmtInfoAsText);
}
function tabsInfoAsMd() {
showTabsInfo(fmtInfoAsMd);
}
function tabsInfoAsLi() {
showTabsInfo(fmtInfoAsLi);
}
function selectInfoText() {
if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(tabsInfo);
selection.removeAllRanges();
selection.addRange(range);
}
}
function onError(error) {
console.log(`ERROR: ${error}`);
}
function showTabsInfo(fmtFunc) {
function listTabs(tabs) {
var tabsText = "";
for (let tab of tabs) {
var p = document.createElement('pre');
p.textContent = fmtFunc(tab);
tabsInfo.appendChild(p);
}
}
clearInfo();
let qry = browser.tabs.query({currentWindow: true});
qry.then(listTabs, onError);
}
function fmtInfoAsText(tab) {
return `Title: ${escHtml(tab.title)}\n URL: ${escHtml(tab.url)}`
}
function fmtInfoAsMd(tab) {
return `[${escHtml(tab.title)}](${escHtml(tab.url)})`
}
function fmtInfoAsLi(tab) {
return `<li><a target="_blank" href="${escHtml(tab.url)}">${escHtml(tab.title)}</a></li>`
}
function clearInfo() {
while (tabsInfo.firstChild) {
tabsInfo.removeChild(tabsInfo.firstChild);
}
}
function escHtml(s) {
return String(s)
.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/</g, "<")
.replace(/>/g, ">")
}
// Start with tabs as text for initial load.
tabsInfoAsText();