-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
62 lines (51 loc) · 1.64 KB
/
popup.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
function getCurrentTabUrl(callback) {
let queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
let tab = tabs[0];
let url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
callback(url);
});
}
function renderStatus(statusText) {
document.getElementById('status').textContent = statusText;
}
document.addEventListener('DOMContentLoaded', function() {
const ul = document.getElementById('items');
let li;
/* add each item in storage an an <li> in ul#items */
chrome.storage.sync.get('items', function (s) {
const items = s.items;
let i; let item; let text;
for (i = 0; i < items.length; ++i) {
item = items[i];
li = document.createElement("li");
let name = document.createElement("span");
name.className = "name";
let port = document.createElement("span");
port.className = "port";
let nameText = document.createTextNode(item.name);
let portText = document.createTextNode(item.port);
name.appendChild(nameText);
port.appendChild(portText);
li.className = "button";
li.dataset.url = item.url;
li.appendChild(name);
li.appendChild(port);
ul.appendChild(li);
}
/* Add click event to each item */
const buttons = document.querySelectorAll('.button');
for (i = 0; i < buttons.length; ++i) {
const button = buttons[i];
button.addEventListener('click', function(e) {
getCurrentTabUrl(function(url) {
chrome.tabs.create({active: true, url: button.dataset.url});
});
});
}
});
});