forked from mozilla-mobile/testapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanti-tracker-test_set_storage_with_sa_api.html
200 lines (174 loc) · 7.77 KB
/
anti-tracker-test_set_storage_with_sa_api.html
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
<!DOCTYPE HTML>
<html lang="en-US" dir="ltr">
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/idb-keyval@3/dist/idb-keyval-iife.min.js"></script>
</head>
<body>
<!-- Storage access API -->
<h4>Storage Access API</h4>
<button onclick='requestSA()'>requestStorageAccess()</button><br />
<p>Return value of requestStorageAccess():</p><pre id='request_storage_access'>not yet called</pre><br />
<button onclick='hasSA()'>hasStorageAccess()</button><br />
<p>Return value of hasStorageAccess():</p><pre id='has_storage_access'>not yet called</pre><br />
<h4>cookies</h4>
<p id="cookie_message"></p>
<div id="set_cookie_message"><p>Trying to set cookie with value:</p><pre id='cookie_set'></pre></div>
<p>Your current non-HTTPOnly cookies are:</p><pre id='cookie_read'></pre>
<h4>localStorage</h4>
<p id="ls_message"></p>
<div id="set_ls_message"><p>Trying to set foo in localStorage to:</p><pre id='ls_set'></pre></div>
<p>Current localStorage</p><pre id='ls_read'></pre>
<h4>sessionStorage</h4>
<p id="ss_message"></p>
<div id="set_ss_message"><p>Trying to set foo in sessionStorage to:</p><pre id='ss_set'></pre></div>
<p>Current sessionStorage</p><pre id='ss_read'></pre>
<h4>Indexed DB</h4>
<p id="idb_message"></p>
<div id="set_idb_message"><p>Trying to set foo in indexedDB to:</p><pre id='idb_set'></pre></div>
<p>Current IndexedDB</p><pre id='idb_read'></pre>
<button onclick="setAllStorageLocations(false);">rerun</button>
<button onclick="setAllStorageLocations(true);">reset</button>
<!-- Storage access API -->
<br /><br />
<h4>Broadcast Channel</h4>
<button onclick='sendMessageToChannel()'>sendMessageToChannel()</button><br />
<p>Message sent to channel:</p><pre id='channel_sent'>none</pre><br />
<p>Messages received from channel:</p><pre id='channel_received'>none</pre><br />
<script>
var newRandVal = Math.random();
function setCookies(reset=false) {
var cookies = document.cookie;
var msg = document.getElementById('cookie_message');
if (cookies === "" || reset === true) {
msg.innerHTML = 'Cookies not yet set, setting a new cookie...';
var set_message_div = document.getElementById('set_cookie_message');
set_message_div.style = "";
// Try to set a cookie
var set = document.getElementById('cookie_set');
set.innerHTML = newRandVal;
document.cookie = 'foo='+newRandVal+'; SameSite=None; Secure';
// Get a cookie
cookies = document.cookie;
var get = document.getElementById('cookie_read');
get.innerHTML = cookies;
} else {
msg.innerHTML = 'Cookies already set, NOT overwriting...';
var set_message_div = document.getElementById('set_cookie_message');
set_message_div.style = "display: none;";
var get = document.getElementById('cookie_read');
get.innerHTML = cookies;
}
}
function setLocalStorage(reset=false) {
var foo = window.localStorage.getItem('foo');
var msg = document.getElementById('ls_message');
if (foo === null || reset === true) {
msg.innerHTML = 'LocalStorage key foo not yet set...';
var set_message_div = document.getElementById('set_ls_message');
set_message_div.style = "";
// Set a new value for foo
var set = document.getElementById('ls_set');
set.innerHTML = newRandVal;
window.localStorage.setItem('foo', newRandVal);
// Get value of foo
foo = window.localStorage.getItem('foo');
var get = document.getElementById('ls_read');
get.innerHTML = 'foo='+foo;
} else {
msg.innerHTML = 'localStorage key foo already written, NOT overwriting...';
var set_message_div = document.getElementById('set_ls_message');
set_message_div.style = "display: none;";
var get = document.getElementById('ls_read');
get.innerHTML = 'foo='+foo;
}
}
function setSessionStorage(reset=false) {
var foo = window.sessionStorage.getItem('foo');
var msg = document.getElementById('ss_message');
if (foo === null || reset === true) {
msg.innerHTML = 'SessionStorage key foo not yet set...';
var set_message_div = document.getElementById('set_ss_message');
set_message_div.style = "";
// Set a new value for foo
var set = document.getElementById('ss_set');
set.innerHTML = newRandVal;
window.sessionStorage.setItem('foo', newRandVal);
// Get value of foo
foo = window.sessionStorage.getItem('foo');
var get = document.getElementById('ss_read');
get.innerHTML = 'foo='+foo;
} else {
msg.innerHTML = 'sessionStorage key foo already written, NOT overwriting...';
var set_message_div = document.getElementById('set_ss_message');
set_message_div.style = "display: none;";
var get = document.getElementById('ss_read');
get.innerHTML = 'foo='+foo;
}
}
async function setIndexedDB(reset) {
var foo = await idbKeyval.get('foo');
var msg = document.getElementById('idb_message');
if (foo === undefined || reset === true) {
msg.innerHTML = 'IndexedDB key foo not yet set...';
var set_message_div = document.getElementById('set_idb_message');
set_message_div.style = "";
// Set a new value for foo
var set = document.getElementById('idb_set');
set.innerHTML = newRandVal;
await idbKeyval.set('foo', newRandVal);
// Get value of foo
foo = await idbKeyval.get('foo');
var get = document.getElementById('idb_read');
get.innerHTML = 'foo='+foo;
} else {
msg.innerHTML = 'IndexedDB key foo already written, NOT overwriting...';
var set_message_div = document.getElementById('set_idb_message');
set_message_div.style = "display: none;";
var get = document.getElementById('idb_read');
get.innerHTML = 'foo='+foo;
}
}
function setAllStorageLocations(reset=false) {
setCookies(reset);
setLocalStorage(reset);
setSessionStorage(reset);
setIndexedDB(reset);
}
setAllStorageLocations(false);
// Storage Access API
function requestSA() {
var result = document.getElementById('request_storage_access');
document.requestStorageAccess().then(
() => {result.innerHTML = 'access granted'},
() => {result.innerHTML = 'access denied'}
);
}
function hasSA() {
var result = document.getElementById('has_storage_access');
document.hasStorageAccess().then(
(hasAccess) => {result.innerHTML = hasAccess},
(reason) => {result.innerHTML = 'promise rejected for reason' + reason}
);
}
hasSA();
// BroadcastChannel
function sendMessageToChannel() {
const bc = new BroadcastChannel('test_channel');
bc.postMessage(newRandVal);
let update = document.getElementById('channel_sent');
update.innerHTML = newRandVal;
bc.close();
}
sendMessageToChannel();
function startListening() {
const bc = new BroadcastChannel('test_channel');
bc.onmessage = function (ev) {
let msg = document.getElementById('channel_received');
msg.innerText = msg.innerText + '\n' + ev.data;
}
}
startListening();
</script>
</body>
</html>