-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalways-ask.js
45 lines (43 loc) · 1.34 KB
/
always-ask.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
async function shimAlwaysAsk(name) {
try {
const status = await window.navigator.permissions.query({name});
const lastState = status.state;
status.onchange = () => {
switch (status.state) {
case "granted":
window.localStorage[`queryShim_${name}_alwaysAsk`] = true;
break;
case "denied":
delete window.localStorage[`queryShim_${name}_alwaysAsk`];
break;
case "prompt":
if (lastState == "granted") {
delete window.localStorage[`queryShim_${name}_alwaysAsk`];
}
break;
}
lastState = status.state;
};
status.onchange();
} catch (e) {}
}
shimAlwaysAsk("camera");
shimAlwaysAsk("microphone");
shimPermissionNameMap = {
camera: "camera",
microphone: "microphone",
video_capture: "camera",
audio_capture: "microphone",
};
const originalGetter = Object.getOwnPropertyDescriptor(PermissionStatus.prototype, 'state').get;
Object.defineProperty(PermissionStatus.prototype, 'state', {
get: function() {
const name = shimPermissionNameMap[this.name];
if (name != "camera" && name != "microphone") {
return originalGetter.call(this);
}
const state = originalGetter.call(this);
return (state == "prompt" &&
window.localStorage[`queryShim_${name}_alwaysAsk`])? "always-ask" : state;
}
});