-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite2.html
More file actions
170 lines (144 loc) · 5.42 KB
/
site2.html
File metadata and controls
170 lines (144 loc) · 5.42 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to site 2!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
pre {
background-color: lightgray;
padding: 10px;
}
iframe {
width: 640px;
height: 400px;
}
button {
margin-left: 5px;
}
section {
width: 600px;
padding: 10px;
display: block;
margin-bottom: 15px;
background-color: #EEEEEE;
}
label {
font-weight: bold;
}
</style>
</head>
<body>
<h1>Site 2 (site2.learning-web.rocks)</h1>
<p><a href="https://learning-web.rocks">Back to the root domain</a></p>
<!-- localStorage UI -->
<section>
<label>localStorage:</label>
<button id="refresh">Refresh</button>
<button id="clear" style="margin-right: 20px">Clear</button>
<label>postMessage:</label>
<button id="setToken">Set auth token</button>
<button id="getToken">Get auth token</button>
<pre id="localStorageOutput"></pre>
</section>
<!-- Cookies UI -->
<section>
<label>Cookies:</label>
<button id="refreshCookies">Refresh</button>
<button id="clearCookies" style="margin-right: 20px">Clear</button>
<label>postMessage:</label>
<button id="setCookieToken">Set auth token</button>
<button id="getCookieToken">Get auth token</button>
<pre id="cookiesOutput"></pre>
</section>
<iframe id="authBridge" src="https://auth-bridge.learning-web.rocks"></iframe>
<!-- Common script -->
<script>
// localStorage logic
const refresh = document.getElementById('refresh');
const clear = document.getElementById('clear');
const localStorageOutput = document.getElementById('localStorageOutput');
function displayLocalStorage() {
const output = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);
output.push(`${key}: ${value}`);
}
localStorageOutput.innerText = output.join('\n');
}
refresh.addEventListener('click', displayLocalStorage);
clear.addEventListener('click', function() {
localStorage.clear();
displayLocalStorage();
});
displayLocalStorage();
// Cookies logic
const refreshCookies = document.getElementById('refreshCookies');
const clearCookies = document.getElementById('clearCookies');
const cookiesOutput = document.getElementById('cookiesOutput');
function getCookies() {
return document.cookie.split(';')
.map(cookie => cookie.trim())
.filter(cookie => cookie !== '');
}
function displayCookies() {
const cookies = getCookies();
const output = cookies.map(cookie => {
const [key, value] = cookie.split('=');
return `${key}: ${value}`;
});
cookiesOutput.innerText = output.join('\n');
}
refreshCookies.addEventListener('click', displayCookies);
clearCookies.addEventListener('click', function() {
const cookies = getCookies();
cookies.forEach(cookie => {
const cookieName = cookie.split('=')[0];
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
});
displayCookies();
});
displayCookies();
</script>
<!-- Site script -->
<script>
const authBridgeWindow = document.getElementById('authBridge').contentWindow;
document.getElementById('setToken').addEventListener('click', function() {
const now = new Date();
const token = now.toLocaleDateString() + ' ' + now.toLocaleTimeString();
localStorage.setItem('token', token);
displayLocalStorage();
authBridgeWindow.postMessage({ type: 'SET_TOKEN', token }, '*');
});
document.getElementById('getToken').addEventListener('click', function() {
authBridgeWindow.postMessage({ type: 'GET_TOKEN' }, '*');
});
document.getElementById('setCookieToken').addEventListener('click', function() {
const now = new Date();
const token = now.toLocaleDateString() + ' ' + now.toLocaleTimeString();
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + 60); // 2 months
document.cookie = `cookieToken=${token}; path=/; expires=${expirationDate.toUTCString()}`;
displayCookies();
authBridgeWindow.postMessage({ type: 'SET_COOKIE_TOKEN', token }, '*');
});
document.getElementById('getCookieToken').addEventListener('click', function() {
authBridgeWindow.postMessage({ type: 'GET_COOKIE_TOKEN' }, '*');
});
window.addEventListener('message', function(event) {
if (event.data) {
if (event.data.type === 'TOKEN_RESPONSE') {
localStorage.setItem('token', event.data.token);
displayLocalStorage();
} else if (event.data.type === 'COOKIE_TOKEN_RESPONSE') {
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + 60); // 2 months
document.cookie = `cookieToken=${event.data.token}; path=/; expires=${expirationDate.toUTCString()}`;
displayCookies();
}
}
});
</script>
</body>
</html>