Skip to content

Commit 82ec52b

Browse files
committed
Refactor js, add function to close tabs
1 parent 4942660 commit 82ec52b

File tree

6 files changed

+133
-22
lines changed

6 files changed

+133
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tmp/

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Tablist
22

3-
Show all open tabs as a list of URLs.
3+
Show all open tabs in a popup. Select or export multiple tabs.
44

55
At the moment all tabs of all windows are shown.
66

77
## TODO
88

99
- [x] Add basic CSS for popup
1010
- [ ] Replace placeholder Icons (multiple sizes)
11-
- [ ] Add settings (copy all URLs or only focused window)
12-
- [ ] Add 'copy to clipboard' button?
11+
- [ ] Add 'export urls' button
12+
- [ ] Add tab favicon
13+
- [x] Add checkbox before tab name to select and close multiple tabs
14+
- [ ] Add search?
1315

1416
## Misc
1517

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"manifest_version": 2,
33
"name": "tablist",
4-
"version": "0.1",
4+
"version": "0",
55

6-
"description": "Display URLs of all open tabs in list",
6+
"description": "Delete and export your tabs",
77

88
"icons": {
99
"48": "icons/tablist-48.png"

popup/tablist.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
body {
22
font-family: monospace;
3+
max-width: 350px;
34
}
45

56
ul {
67
margin: 0;
78
padding: 0;
89
text-indent: 0;
910
list-style-type: none;
11+
max-height: 500px;
12+
overflow-y: scroll;
1013
}
1114

15+
li {
16+
margin-right: 5px;
17+
text-overflow: ellipsis;
18+
overflow-x: hidden;
19+
white-space: nowrap;
20+
}
21+
22+
.tablist-checkbox-wrapper {
23+
display: flex;
24+
align-items: center;
25+
}
26+
27+
.tablist-buttons {
28+
display: flex;
29+
justify-content: space-between;
30+
margin-top: 10px;
31+
}

popup/tablist.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
<link href="../popup/tablist.css" rel="stylesheet" type="text/css">
77
</head>
88
<body>
9-
<ul id="tablist-container">
10-
</ul>
9+
<div class="panel tablist">
10+
<div class="tablist-checkbox-wrapper">
11+
<input type="checkbox" id="selectAll" value="">
12+
<label for="selectAll">Select all</label>
13+
</div>
14+
15+
<ul class="tablist-items">
16+
</ul>
17+
<div class="tablist-buttons">
18+
<button class="tablist-delete-btn browser-style">Close selected</button>
19+
<button class="tablist-export-btn browser-style">Export selected</button>
20+
</div>
21+
</div>
1122
</body>
1223
</html>

popup/tablist.js

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,104 @@ window.onload = function() {
44
};
55
*/
66

7-
function logTabs(tabs) {
8-
const popupList = document.getElementById('tablist-container');
9-
let urlList = [];
7+
// get array of tabs call event listeners (aka main function)
8+
const logTabs = (tabs) => {
9+
const popupList = document.querySelector('.tablist-items');
1010

11-
// get all tab URLs and push to array
11+
// get all tab URLs and render list
1212
for (let tab of tabs) {
13-
// tab.url requires the `tabs` permission
14-
urlList.push(tab.url);
13+
createTabList(tab, popupList);
1514
}
1615

17-
// append URLs as li items to popup
18-
urlList.forEach(function(url) {
19-
const listItem = document.createElement('li');
20-
listItem.appendChild(document.createTextNode(url));
21-
popupList.appendChild(listItem);
22-
});
23-
}
16+
// creat eventListeners
17+
tabListEvents();
18+
};
19+
20+
// create list item and append it to popup
21+
const createTabList = (tab, popupList) => {
22+
const listItemInput = document.createElement('input');
23+
listItemInput.setAttribute('type', 'checkbox');
24+
listItemInput.setAttribute('id', tab.id);
25+
listItemInput.setAttribute('name', 'tab');
26+
listItemInput.setAttribute('value', tab.url);
27+
28+
const listItemLabel = document.createElement('label');
29+
listItemLabel.setAttribute('for', tab.id);
30+
listItemLabel.appendChild(document.createTextNode(tab.title));
31+
32+
const listItem = document.createElement('li');
33+
listItem.setAttribute('class', 'tablist-checkbox-wrapper');
34+
listItem.appendChild(listItemInput);
35+
listItem.appendChild(listItemLabel);
36+
37+
popupList.appendChild(listItem);
38+
};
39+
40+
// create all needed event listeners
41+
const tabListEvents = () => {
42+
// add event listener on wrapper and check for class?
43+
const toggleSelect = document.getElementById('selectAll'),
44+
deleteBtn = document.querySelector('.tablist-delete-btn'),
45+
exportBtn = document.querySelector('.tablist-export-btn'),
46+
listBtn = document.querySelector('.tablist-list-btn');
2447

25-
function onError(error) {
48+
// check or uncheck all items in tablist
49+
toggleSelect.addEventListener('click', (event) => {
50+
// TODO: save all previosly checked?
51+
const checkAllItems = event.target.checked;
52+
const checkboxes = Array.from(document.getElementsByName('tab'));
53+
checkboxes.map(checkbox => {
54+
if(checkAllItems) {
55+
checkbox.checked = true;
56+
} else {
57+
checkbox.checked = false;
58+
}
59+
});
60+
});
61+
62+
// collect and delete all checked items
63+
deleteBtn.addEventListener('click', () => {
64+
console.log('click delete');
65+
const checkboxes = Array.from(document.getElementsByName('tab'));
66+
let selectedTabIds = [];
67+
checkboxes.map(checkbox => {
68+
if(checkbox.checked) {
69+
console.log(checkbox);
70+
selectedTabIds.push(parseInt(checkbox.id));
71+
}
72+
});
73+
const removing = browser.tabs.remove(selectedTabIds);
74+
removing.then(closePopup, onError);
75+
});
76+
77+
exportBtn.addEventListener('click', () => {
78+
console.log('click export');
79+
const checkboxes = Array.from(document.getElementsByName('tab'));
80+
let selectedTabUrls = [];
81+
checkboxes.map(checkbox => {
82+
if(checkbox.checked) {
83+
selectedTabUrls.push(checkbox.value);
84+
}
85+
});
86+
const creating = browser.tabs.create({
87+
// data urls not possible: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/create
88+
// add array to storage and retrieve it on new site:
89+
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage
90+
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages
91+
//url:'data:text/plain;charset=utf-8;base64,' + btoa(selectedTabUrls.join(''))
92+
});
93+
//creating.then(closePopup, onError);
94+
});
95+
};
96+
97+
const closePopup = () => {
98+
console.log('done, closing');
99+
window.close();
100+
};
101+
102+
const onError = (error) => {
26103
console.log(`Error: ${error}`);
27-
}
104+
};
28105

29106
const querying = browser.tabs.query({});
30107
querying.then(logTabs, onError);

0 commit comments

Comments
 (0)