-
Notifications
You must be signed in to change notification settings - Fork 22
/
options.js
114 lines (99 loc) · 3.46 KB
/
options.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
window.addEventListener('load', onLoad, false)
/**
* Short form for getting elements by id.
* @param {string} id The id.
*/
function $(id) {
return document.getElementById(id);
}
/**
* When the options window has been loaded.
*/
function onLoad() {
onRestore();
$('button-close').addEventListener('click', onClose, false)
$('button-extension').addEventListener('click', onExtension, false)
$('keyboardShortcutUpdate').addEventListener('click', onKeyboardShortcut, false)
}
/**
* When the options window is closed;
*/
function onClose() {
window.close()
}
/**
* Opens the extensions page. The reason why we didn't do a simple link,
* is because you are not allowed to load that local resource via renderer.
*/
function onExtension() {
chrome.tabs.create({ url: 'chrome://extensions/' })
return false
}
function setupCheckbox(id, storedValue, defaultValue = false) {
const element = $(id)
element.checked = (typeof storedValue == 'undefined') ? defaultValue : (storedValue == true)
element.addEventListener('change', (e) => {
const stopFlashing = flashMessage(e.target)
chrome.storage.sync.set({ [id]: e.target.checked }, () => stopFlashing())
})
}
function setupDropdown(id, storedValue, defaultValue = false) {
const element = $(id)
element.value = (typeof storedValue == 'undefined') ? defaultValue : storedValue
element.addEventListener('change', (e) => {
const stopFlashing = flashMessage(e.target)
chrome.storage.sync.set({ [id]: e.target.value }, () => stopFlashing())
})
}
function flashMessage(element) {
const rect = element.getBoundingClientRect()
const info = $('info-message')
info.style.top = rect.top + (rect.height / 2) - (info.clientHeight / 2) + 'px'
info.style.left = rect.x + rect.width + 'px'
info.style.opacity = 1
return () => setTimeout(() => info.style.opacity = 0.0, 1000)
}
/**
* Restore all options.
*/
function onRestore() {
const settingsToFetch = [
'reloadWindow',
'reloadAllWindows',
'reloadPinnedOnly',
'reloadUnpinnedOnly',
'reloadAllLeft',
'reloadAllRight',
'closeAllLeft',
'closeAllRight',
'bypassCache',
'buttonDefaultAction',
'version'
]
chrome.storage.sync.get(settingsToFetch, settings => {
$('version').innerText = ' (v' + settings.version + ')'
setupCheckbox('reloadWindow', settings.reloadWindow, true /* default if not exists */)
setupCheckbox('reloadAllWindows', settings.reloadAllWindows)
setupCheckbox('reloadPinnedOnly', settings.reloadPinnedOnly)
setupCheckbox('reloadUnpinnedOnly', settings.reloadUnpinnedOnly)
setupCheckbox('reloadAllLeft', settings.reloadAllLeft)
setupCheckbox('reloadAllRight', settings.reloadAllRight)
setupCheckbox('closeAllLeft', settings.closeAllLeft)
setupCheckbox('closeAllRight', settings.closeAllRight)
setupCheckbox('bypassCache', settings.bypassCache)
setupDropdown('buttonDefaultAction', settings.buttonDefaultAction, 'window')
})
chrome.commands.getAll(callback => {
$('keyboardShortcut').innerText = callback[0].shortcut || 'Not Set'
})
}
function onKeyboardShortcut(e) {
const selection = window.getSelection()
const range = document.createRange()
range.selectNodeContents(e.target)
selection.removeAllRanges()
selection.addRange(range)
document.execCommand('copy')
selection.removeAllRanges()
alert(`Copied the following link '${e.target.innerText}' to clipboard. You can change its defaults there. Due to Chrome security, you need to visit it manually.`)
}