-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
73 lines (67 loc) · 2.08 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
63
64
65
66
67
68
69
70
71
72
73
document.onclick = function click(event) {
if(event) {
if(event.target.matches('.pin-toggle')) {
togglePin(event.target, event.target.innerHTML)
}
}
}
let optionsLink = document.getElementById('options-link');
optionsLink.addEventListener('click', () => {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
document.addEventListener('DOMContentLoaded', function () {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
(function () {
var ln = links[i];
var location = ln.href;
ln.onclick = function () {
chrome.tabs.create({active: true, url: location});
};
})();
}
});
const options = {};
chrome.storage.sync.get('options', (data) => {
Object.assign(options, data.options);
let grid = document.getElementById('pinned-grid');
if(options.pinnedKeywords.length > 0) {
options.pinnedKeywords.forEach(keyword => {
grid.appendChild(newPinToggle(keyword));
});
}
});
/**
* create new pin toggle element
* @param value corresponding keyword
* @return new pin toggle element
*/
function newPinToggle(value) {
let pinToggle = document.createElement('div');
pinToggle.classList.add('pin-toggle');
if(options.disabledPinnedKeywords.includes(value)) {
pinToggle.classList.add('disabled');
}
pinToggle.innerHTML = value;
return pinToggle;
}
/**
* toggle whether keyword should be included in keyword filters
* @param el html element
* @param value keyword string
*/
function togglePin(el, value) {
if(el.classList.contains('disabled')) {
options.disabledPinnedKeywords = options.disabledPinnedKeywords.filter(e => e !== value)
chrome.storage.sync.set({options});
el.classList.remove('disabled');
} else {
options.disabledPinnedKeywords.push(value);
chrome.storage.sync.set({options});
el.classList.add('disabled');
}
}