-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.js
112 lines (94 loc) · 3.14 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
'use strict';
/* global chrome */
var permissions = [];
var options = {};
// ID for each checkbox
const supported = ['disableresume', 'pauseoninactive', 'multipletabs', 'ignoretabchange', 'muteonpause', 'ignoreother', 'nopermission', 'permediapause', 'checkidle','resumelimit', 'allowactive', 'ask'];
var userinput = document.getElementById('userinput');
// User presses enter
window.addEventListener('keyup', event => {
if (event.key === 'Enter') {
event.preventDefault();
permissionUpdate();
}
});
chrome.permissions.onAdded.addListener(getPermissions);
chrome.permissions.onRemoved.addListener(getPermissions);
chrome.storage.sync.get('options', result => {
if (typeof result.options === 'object' && result.options !== null) {
options = result.options;
applyChanges();
}
});
chrome.storage.onChanged.addListener(result => {
if (typeof result.options === 'object' && result.options !== null) {
options = result.options.newValue;
applyChanges();
}
});
function applyChanges() {
supported.forEach(id => {
var state = hasProperty(options, id);
document.getElementById(id).checked = state;
});
}
supported.forEach(id => {
document.getElementById(id).onclick = () => {
toggleOption(id);
}
});
function hasProperty(value, key) {
return Object.prototype.hasOwnProperty.call(value, key);
}
function toggleOption(o) {
if (hasProperty(options, o)) {
delete options[o];
} else {
options[o] = true;
}
return new Promise(resolve => {
chrome.storage.sync.set({
options
}, function (result) {
resolve(result);
});
});
}
function getPermissions() {
chrome.permissions.getAll(resp => {
permissions = resp.origins;
userinput.value = permissions.join(' ');
});
}
getPermissions();
const common = new Map([['youtube', 'https://www.youtube.com/*'], ['soundcloud', 'https://soundcloud.com/*'], ['twitch', 'https://www.twitch.tv/*'], ['pandora', 'https://*.pandora.com/*'], ['wrif', 'https://wrif.com/*'], ['ustvgo', 'https://ustvgo.tv/*'], ['picarto', 'https://picarto.tv/*']]);
userinput.oninput = () => {
let result = userinput.value.split(' ');
for (let [index, value] of result.entries()) {
const key = value.toLowerCase();
if (common.has(key)) {
result[index] = common.get(key);
}
}
userinput.value = result.join(' ');
};
async function permissionUpdate() {
const domains = userinput.value.split(' ');
const regex = /^(https?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$/;
const add = domains.filter(domain => domain === '<all_urls>' || regex.test(domain));
const remove = permissions.filter(permission => !domains.includes(permission));
if (remove.length > 0) {
chrome.permissions.remove({
origins: remove
}, () => {
getPermissions();
});
}
if (add.length > 0) {
chrome.permissions.request({
origins: add
}, () => {
getPermissions();
});
}
}