-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoptions.js
115 lines (102 loc) · 3.82 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
115
/*******************************************************************************
Measure=it - A browser extension to measure parts of page.
Copyright (C) 2017-2019 Trishul Goel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/tsl143/measure-it
*******************************************************************************/
const saveData = (key, val) => {
localStore.get('choices')
.then((res = {}) => {
const { choices } = res;
choices[key] = val;
localStore.set({ choices })
});
}
const setVal = (key, val, saveToo = true) => {
switch (key) {
case 'background': {
if(saveToo) saveData('background', val);
document.getElementById('backgroundLabel').textContent = val;
if(!saveToo) document.getElementById('background').value = val;
break;
}
case 'popup': {
if(saveToo) saveData('popup', val)
document.getElementById('popupLabel').textContent = val;
if(!saveToo) document.getElementById('popup').value = val;
break;
}
case 'popupOpacity': {
if(saveToo) saveData('popupOpacity', val);
document.getElementById('popupOpacityLabel').textContent = val;
if(!saveToo) document.getElementById('popupOpacity').value = val;
break;
}
case 'backgroundOpacity': {
if(saveToo) saveData('backgroundOpacity', val);
document.getElementById('backgroundOpacityLabel').textContent = val;
if(!saveToo) document.getElementById('backgroundOpacity').value = val;
break;
}
}
}
localStore.get('choices')
.then((res = {}) => {
const { choices = {} } = res;
setVal('background', choices.background, false);
setVal('popup', choices.popup, false);
setVal('popupOpacity', choices.popupOpacity, false);
setVal('backgroundOpacity', choices.backgroundOpacity, false);
});
document.getElementById('optionTable').addEventListener('change', e => {
const ele = e.target;
setVal(ele.getAttribute('id'), ele.value);
})
if (isFirefox) {
const shortcutInput = document.getElementById('shortcutKey');
let sKeys = 'Alt+Shift+M';
try {
if(navigator.platform.toUpperCase().includes('MAC')) sKeys = 'Command+Shift+O';
} catch(e){}
document.getElementById('shortcut').classList = "";
localStore.get().then( res => {
const { choices = {} } = res;
shortcutInput.value = choices.shortcut || sKeys;
})
const handlekeyPress = e => {
shortcutInput.value = '';
const { ctrlKey, altKey,shiftKey, metaKey, key } = e;
if (key == 'Backspace') {
shortcutInput.value = '';
} else if (!ctrlKey && !altKey && !metaKey) {
shortcutInput.value = 'Atleast 1 modifer key';
} else if (key.length > 1 || !key.match(/^[A-Za-z0-9]+$/)) {
shortcutInput.value = 'Alphabet/number only';
} else {
const finalKeys = new Array;
if(ctrlKey) finalKeys.push('Ctrl');
if(altKey) finalKeys.push('Alt');
if(shiftKey) finalKeys.push('Shift');
if(metaKey) finalKeys.push('Command');
finalKeys.push(key.toUpperCase());
sKeys = finalKeys.join('+');
shortcutInput.value = sKeys;
browser.commands.update({
name: '_execute_browser_action',
shortcut: sKeys
});
}
}
shortcutInput.addEventListener('keypress', handlekeyPress);
shortcutInput.addEventListener('click', e => { shortcutInput.value = '' })
shortcutInput.addEventListener('blur', e => { shortcutInput.value = sKeys })
}