forked from chromium/permission.site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
280 lines (266 loc) · 9.04 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// - Information about clearing settings in Chrome (can't link to chrome:// URLs)
// - Indicate if permissions are already granted, if the relevant API allows it.
window.addEventListener("load", function() {
var toggle = document.querySelector("#toggle");
toggle.classList.add("instant");
if (window.location.protocol == "https:") {
toggle.classList.add("https");
toggle.protocol = "http:";
} else if (window.location.protocol == "http:") {
toggle.classList.add("http");
toggle.protocol = "https:";
}
setTimeout(function() {
toggle.classList.remove("instant");
}, 10);
function displayOutcome(type, outcome) {
return function() {
var argList = [outcome, type].concat([].slice.call(arguments));
switch(outcome) {
case "success":
console.info.apply(console, argList);
break;
case "error":
console.error.apply(console, argList);
break;
default:
console.log.apply(console, argList);
}
document.getElementById(type).classList.add(outcome);
};
};
function displayOutcomeForNotifications(outcome) {
switch(outcome) {
case "granted":
console.info(outcome, "notifications");
document.getElementById("notifications").classList.add("success");
break;
case "denied":
case "default":
// "default" is supposed to be like "denied", except the user hasn't made an explicit decision yet.
// https://notifications.spec.whatwg.org/#permission-model
console.error(outcome, "notifications");
document.getElementById("notifications").classList.add("error");
break;
default:
throw "Unknown notification API response.";
}
};
function triggerDownload() {
// Based on http://stackoverflow.com/a/27280611
var a = document.createElement('a');
a.download = "test-image.png";
a.href = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAABC0lEQVQYlTXPPUsCYQDA8b/e04tdQR5ZBpE3NAR6S0SDVDZKDQ2BY9TUy1foE0TQ1Edo6hOEkyUG0QuBRtQgl0hnenVdnZD5eLbU7xv8Avy5X16KhrQBg47EtpziXO6qBhAEeNEm0qr7VdBcLxt2mlnNbhVu0NMAgdj1wvjOoX2xdSt0L7MGgx2GGid8yLrJvJMUkbKfOF8N68bUIqcz2wQR7GUcYvJIr1dFQijvkh89xGV6BPPMwvMF/nQXJMgWiM+KLPX2tc0HNa/HUxDv2owpx7xV+023Hiwpdt7yhmcjj9/NdrIhn8LrPVmotctWVd01Nt27wH9T3YhHU5O+sT/6SuVZKa4cNGoAv/ZMas7pC/KaAAAAAElFTkSuQmCC";
a.click();
}
navigator.getUserMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia
);
navigator.requestFullscreen = (
navigator.requestFullscreen ||
navigator.webkitRequestFullscreen ||
navigator.mozRequestFullscreen ||
navigator.msRequestFullscreen
);
navigator.requestMIDIAccess = (
navigator.requestMIDIAccess ||
navigator.webkitRequestMIDIAccess ||
navigator.mozRequestMIDIAccess ||
navigator.msRequestMIDIAccess
);
document.body.requestFullScreen = (
document.body.requestFullScreen ||
document.body.webkitRequestFullScreen ||
document.body.mozRequestFullScreen ||
document.body.msRequestFullScreen
);
document.body.requestPointerLock = (
document.body.requestPointerLock ||
document.body.webkitRequestPointerLock ||
document.body.mozRequestPointerLock ||
document.body.msRequestPointerLock
);
var register = {
"notifications": function () {
Notification.requestPermission(
displayOutcomeForNotifications
);
},
"location": function() {
navigator.geolocation.getCurrentPosition(
displayOutcome("location", "success"),
displayOutcome("location", "error")
);
},
"camera": function() {
navigator.getUserMedia(
{video: true},
displayOutcome("camera", "success"),
displayOutcome("camera", "error")
);
},
"microphone": function() {
navigator.getUserMedia(
{audio: true},
displayOutcome("microphone", "success"),
displayOutcome("microphone", "error")
);
},
"camera+microphone": function() {
navigator.getUserMedia(
{audio: true, video: true},
displayOutcome("camera+microphone", "success"),
displayOutcome("camera+microphone", "error")
);
},
"midi": function() {
navigator.requestMIDIAccess({
sysex: true
}).then(
displayOutcome("midi", "success"),
displayOutcome("midi", "error")
);
},
"bluetooth": function() {
navigator.bluetooth.requestDevice({
filters: [{services: ['battery_service']}]
}).then(
displayOutcome("bluetooth", "success"),
displayOutcome("bluetooth", "error")
);
},
"usb": function() {
navigator.usb.requestDevice({filters: [{}]}).then(
displayOutcome("usb", "success"),
displayOutcome("usb", "error")
);
},
"eme": function() {
// https://w3c.github.io/encrypted-media/#requestMediaKeySystemAccess
// Tries multiple configuration per key system. The configurations are in
// descending order of privileges such that a supported permission-requiring
// configuration should be attempted before a configuration that does not
// require permissions.
var knownKeySystems = [
"com.example.somesystem", // Ensure no real system is the first tried.
"com.widevine.alpha",
"com.microsoft.playready",
"com.adobe.primetime",
"com.apple.fps.2_0",
"com.apple.fps",
"com.apple.fps.1_0",
"com.example.somesystem" // Ensure no real system is the last tried.
];
var tryKeySystem = function(keySystem) {
navigator.requestMediaKeySystemAccess(
keySystem,
[
{ distinctiveIdentifier: "required",
persistentState: "required",
label: "'distinctiveIdentifier' and 'persistentState' required"
},
{ distinctiveIdentifier: "required",
label: "'distinctiveIdentifier' required"
},
{ persistentState: "required",
label: "'persistentState' required"
},
{ label: "empty" }
]
).then(
function (mediaKeySystemAccess) {
displayOutcome("eme", "success")(
"Key System: " + keySystem,
"Configuration: " + mediaKeySystemAccess.getConfiguration().label,
mediaKeySystemAccess);
},
function (error) {
if (knownKeySystems.length > 0)
return tryKeySystem(knownKeySystems.shift());
displayOutcome("eme", "error")(
error,
error.name == "NotSupportedError" ? "No known key systems supported or permitted." : "");
}
);
};
tryKeySystem(knownKeySystems.shift());
},
"copy": (function() {
var interceptCopy = false;
document.addEventListener("copy", function(e){
if (interceptCopy) {
// From http://www.w3.org/TR/clipboard-apis/#h4_the-copy-action
e.clipboardData.setData("text/plain",
"This text was copied from the permission.site clipboard example."
);
e.clipboardData.setData("text/html",
"This text was copied from the " +
"<a href='https://permission.site/'>" +
"permission.site</a> clipboard example."
);
e.preventDefault();
}
});
return function() {
interceptCopy = true;
document.execCommand("copy");
interceptCopy = false;
};
}()),
"popup": function() {
var w = window.open(
location.href,
"Popup",
"resizable,scrollbars,status"
)
displayOutcome("popup", w ? "success" : "error")(w);
},
"popup-delayed": function() {
setTimeout(function() {
var w = window.open(
location.href,
"Popup",
"resizable,scrollbars,status"
)
displayOutcome("popup-delayed", w ? "success" : "error")(w);
}, 2000);
},
"fullscreen": function() {
// Note: As of 2014-12-16, fullscreen only allows "ask" and "allow" in Chrome.
document.body.requestFullScreen(
/* no callback */
);
},
"pointerlock": function() {
document.body.requestPointerLock(
/* no callback */
);
},
"download": function() {
// Two downloads at the same time trigger a permission prompt in Chrome.
triggerDownload();
triggerDownload();
},
"keygen": function() {
var keygen = document.createElement("keygen");
document.getElementById("keygen-container").appendChild(keygen);
},
"persistent-storage": function() {
// https://storage.spec.whatwg.org
navigator.storage.persist().then(
function(persisted) {
displayOutcome("persistent-storage", persisted ? "success" : "default")(persisted);
},
displayOutcome("persistent-storage", "error")
)
}
};
for (var type in register) {
document.getElementById(type).addEventListener('click',
register[type]
);
}
});