-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
95 lines (79 loc) · 2.59 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function _getExtension(callBack) {
let client
try {
client = browser
} catch (e) {
try {
client = chrome
} catch(f) {
client = null
}
}
return client.runtime.getBackgroundPage(callBack)
}
function clearHistory() {
_getExtension(ext => {
ext._noNewTabsExtension.clearHistory()
displayHistory()
})
}
function removeHistoryItem(item) {
_getExtension(ext => {
ext._noNewTabsExtension.removeFromHistory(item)
displayHistory()
})
}
function openHistoryItem(item) {
_getExtension(ext => ext._noNewTabsExtension.openNewTab(item))
}
function createHistoryItem(url, count) {
var itemContainer = document.createElement('div')
var itemContent = document.createElement('div')
var controlsContainer = document.createElement('div')
var removeButton = document.createElement('button')
var openButton = document.createElement('button')
var text = document.createTextNode('[' + count + '] ' + url)
itemContainer.className = 'historyItemContainer'
itemContainer.appendChild(itemContent)
itemContainer.appendChild(controlsContainer)
itemContent.className = 'historyItemContent'
itemContent.appendChild(text)
removeButton.innerHTML = 'remove'
removeButton.addEventListener('click', ()=>{removeHistoryItem(url)})
openButton.innerHTML = 'open'
openButton.addEventListener('click', ()=>{openHistoryItem(url)})
controlsContainer.className = 'historyItemControls'
controlsContainer.appendChild(removeButton)
controlsContainer.appendChild(openButton)
return itemContainer
}
function displayHistory() {
var display = document.querySelector('#historyDisplay')
display.innerHTML = ''
_getExtension(ext => Object.keys(ext._noNewTabsExtension.history).forEach((key) => {
var historyItem = createHistoryItem(key, ext._noNewTabsExtension.history[key])
display.appendChild(historyItem)
}))
}
function _setupOptionControl(option) {
var control = document.querySelector('#' + option)
_getExtension(ext => {
control.checked = ext._noNewTabsExtension[option]
control.addEventListener('click', (e) => {
ext._noNewTabsExtension[option] = e.target.checked
})
})
}
document.addEventListener('DOMContentLoaded', function() {
_setupOptionControl('allowAll')
_setupOptionControl('allowBlankTabs')
_setupOptionControl('allowHistory')
document.querySelector('#clearHistoryButton')
.addEventListener('click', clearHistory)
displayHistory()
document.querySelector('#sourceLink')
.addEventListener('click', (e) => {
openHistoryItem(e.target.href)
e.preventDefault()
})
})