forked from jedediah/wetbanana
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathoptions.js
More file actions
130 lines (105 loc) · 3.26 KB
/
Copy pathoptions.js
File metadata and controls
130 lines (105 loc) · 3.26 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var KEYS = ['shift', 'ctrl', 'alt', 'meta']
function $(id) {
return document.getElementById(id)
}
function error(msg) {
$('message').innerHTML += '<div style="color:red">' + msg + '</div>'
}
function clearMessage() {
$('message').innerHTML = ''
}
function save() {
var x
var o = {}
clearMessage()
x = $('button').selectedIndex
if (x < 0 || x > 2) {
error('Somehow, you broke the button field')
} else o.button = x
x = $('scaling').value - 0
if (isNaN(x)) {
error('Scaling must be a number')
} else o.scaling = x / 100
x = $('speed').value - 0
if (isNaN(x) || x < 0) {
error('Top speed must be a positive number or zero')
} else o.speed = x
x = $('friction').value - 0
if (isNaN(x) || x < 0) {
error('Friction must be a positive number')
} else o.friction = x
for (var i = 0; i < KEYS.length; i++) {
o['key_' + KEYS[i]] = $('key_' + KEYS[i]).checked
}
x = $('blocklist').value
var hosts = x.split('\n')
for (var i = hosts.length - 1; i >= 0; i--) {
var host = hosts[i].trim()
if (!host.match(/^[a-z0-9-.]*$/)) {
error('The blocklisted domain name "' + host + '" is not valid')
}
}
o.blocklist = x
o.cursor = $('cursor').checked
o.notext = $('notext').checked
o.grab_and_drag = $('grab_and_drag').checked
o.debug = $('debug').checked
console.log('Saving options:', o)
chrome.storage.local.set(o)
}
function load(o) {
$('button').selectedIndex = o.button
for (var i = 0; i < KEYS.length; i++) {
$('key_' + KEYS[i]).checked = o['key_' + KEYS[i]] + '' == 'true'
}
$('scaling').value = o.scaling * 100
$('speed').value = o.speed
$('friction').value = o.friction
$('blocklist').value = o.blocklist || o.blacklist || ''
$('cursor').checked = isTrue(o.cursor)
$('notext').checked = isTrue(o.notext)
$('grab_and_drag').checked = isTrue(o.grab_and_drag)
$('debug').checked = isTrue(o.debug)
console.log('Options loaded:', o)
}
function isTrue(value) {
return value == true || value == 'true'
}
var updateTimeoutId
function onUpdate(ev) {
if (updateTimeoutId != null) clearTimeout(updateTimeoutId)
updateTimeoutId = setTimeout(save, 200)
$('windows_middle_warning').style.display =
$('button').selectedIndex == 1 &&
navigator.userAgent.search(/Windows/) != -1 &&
navigator.userAgent.search(/Chrome\/[012345]\./) != -1
? 'block'
: 'none'
}
document.addEventListener(
'DOMContentLoaded',
function (ev) {
chrome.storage.local.get(null, function (loadedOptions) {
console.log('Loaded stored options:', loadedOptions)
if (Object.keys(loadedOptions).length > 0) {
load(loadedOptions)
}
})
;['button', 'cursor', 'notext', 'debug', 'grab_and_drag'].forEach(
function (id) {
$(id).addEventListener('change', onUpdate, false)
},
)
KEYS.forEach(function (key) {
$('key_' + key).addEventListener('change', onUpdate, false)
})
;['scaling', 'speed', 'friction', 'blocklist'].forEach(function (id) {
$(id).addEventListener('change', onUpdate, true)
$(id).addEventListener('keydown', onUpdate, true)
$(id).addEventListener('mousedown', onUpdate, true)
$(id).addEventListener('blur', onUpdate, true)
})
},
true,
)
document.addEventListener('unload', save, true)