-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions.js
154 lines (148 loc) · 3.56 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
Vue.config.productionTip = false
Vue.config.devtools = false
{
const html = String.raw
Vue.component('save-button', {
props: ['store'],
template: html`
<div class="flex items-center mb-4">
<div class="w-1/3"><slot></slot></div>
<div>
<button
@click="store.save"
class="shadow text-090807 focus:shadow-outline focus:outline-none font-bold py-2 px-4 rounded"
:class="[store.changed ? 'bg-d7fc70' : 'bg-8b8685']"
type="button"
>
Save
</button>
</div>
<div class="ml-4">
{{store.status}}
</div>
</div>
`,
})
Vue.component('boolean-flag', {
props: ['store', 'settingKey', 'title'],
data() {
return {
checked: this.getTargetValue(),
}
},
computed: {
targetValue() {
return this.getTargetValue()
},
},
methods: {
getTargetValue() {
return this.store.settings[this.settingKey] === 'on'
},
setTargetValue(flag) {
this.store.settings[this.settingKey] = flag ? 'on' : 'off'
},
},
watch: {
checked(v) {
if (this.getTargetValue() !== v) this.setTargetValue(v)
},
targetValue(v) {
if (v !== this.checked) this.checked = v
},
},
template: html`
<article>
<label class="flex items-baseline">
<input
type="checkbox"
class="form-checkbox self-center bg-090807 border-656463"
v-model="checked"
/>
<span class="ml-2">{{title}}</span>
</label>
<span class="block ml-6 text-8b8685 italic mb-2">
<slot></slot>
</span>
</article>
`,
})
}
var vm = new Vue({
el: '#app',
data: {
store: null,
grantStatus: null,
source: null,
},
mounted() {
if (location.search.match(/err=not-allowed/)) {
this.source = 'not-allowed-err'
}
if (location.search.match(/err=audio-capture/)) {
this.source = 'audio-capture-err'
}
},
methods: {
shortcuts() {
chrome.tabs.create({
url: 'chrome://extensions/shortcuts',
})
},
async grantMic() {
this.grantStatus = { ok: 'pending' }
try {
await new Promise((resolve, reject) => {
const sr = new webkitSpeechRecognition()
sr.onstart = () => {
sr.stop()
resolve()
}
sr.onerror = event => {
reject(new Error(event.error))
}
sr.start()
})
this.grantStatus = { ok: true }
} catch (e) {
this.grantStatus = { ok: false, message: String(e.message) }
}
},
},
})
function main({ defaultSettings, settings }) {
vm.store = new Vue({
data: {
savedSettings: JSON.parse(JSON.stringify(settings)),
settings,
defaultSettings,
status: '',
},
computed: {
changed() {
return (
JSON.stringify(this.settings) !== JSON.stringify(this.savedSettings)
)
},
},
watch: {
changed() {
if (this.status) this.status = ''
},
},
methods: {
save() {
const newSettings = JSON.parse(JSON.stringify(settings))
chrome.storage.sync.set(newSettings, () => {
this.savedSettings = newSettings
this.status = 'All changes saved'
})
},
},
})
}
import('./src/VXDefaultSettings.js').then(({ defaultSettings }) => {
chrome.storage.sync.get(defaultSettings, settings => {
main({ defaultSettings, settings })
})
})