Update index.html
Browse files- index.html +96 -7
index.html
CHANGED
@@ -1,12 +1,101 @@
|
|
1 |
-
<!-- index.html -->
|
2 |
<!DOCTYPE html>
|
3 |
-
<html>
|
4 |
<head>
|
5 |
-
<
|
6 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
</head>
|
8 |
<body>
|
9 |
-
<h1
|
10 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
</body>
|
12 |
-
</html>
|
|
|
|
|
1 |
<!DOCTYPE html>
|
2 |
+
<html lang="ja">
|
3 |
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<title>通信ログ表示 (Blob Service Worker)</title>
|
6 |
+
<style>
|
7 |
+
body { font-family: sans-serif; padding: 1em; }
|
8 |
+
#log { margin-top: 1em; font-size: 14px; }
|
9 |
+
.success { color: green; margin-bottom: 1em; }
|
10 |
+
.error { color: red; margin-bottom: 1em; }
|
11 |
+
pre { background: #f9f9f9; padding: 0.5em; border: 1px solid #ccc; }
|
12 |
+
</style>
|
13 |
</head>
|
14 |
<body>
|
15 |
+
<h1>通信ログ</h1>
|
16 |
+
<div id="log">Service Worker を登録中...</div>
|
17 |
+
|
18 |
+
<script>
|
19 |
+
const swCode = `
|
20 |
+
self.addEventListener('fetch', event => {
|
21 |
+
const { request } = event;
|
22 |
+
const url = request.url;
|
23 |
+
const method = request.method;
|
24 |
+
|
25 |
+
event.respondWith(
|
26 |
+
fetch(request)
|
27 |
+
.then(response => {
|
28 |
+
const clone = response.clone();
|
29 |
+
clone.text().then(body => {
|
30 |
+
self.clients.matchAll().then(clients => {
|
31 |
+
clients.forEach(client => {
|
32 |
+
client.postMessage({
|
33 |
+
type: 'fetch-log',
|
34 |
+
method,
|
35 |
+
url,
|
36 |
+
status: response.status,
|
37 |
+
body: body.slice(0, 200)
|
38 |
+
});
|
39 |
+
});
|
40 |
+
});
|
41 |
+
});
|
42 |
+
return response;
|
43 |
+
})
|
44 |
+
.catch(error => {
|
45 |
+
self.clients.matchAll().then(clients => {
|
46 |
+
clients.forEach(client => {
|
47 |
+
client.postMessage({
|
48 |
+
type: 'fetch-error',
|
49 |
+
method,
|
50 |
+
url,
|
51 |
+
error: error.toString()
|
52 |
+
});
|
53 |
+
});
|
54 |
+
});
|
55 |
+
throw error;
|
56 |
+
})
|
57 |
+
);
|
58 |
+
});
|
59 |
+
`;
|
60 |
+
|
61 |
+
const blob = new Blob([swCode], { type: 'application/javascript' });
|
62 |
+
const swUrl = URL.createObjectURL(blob);
|
63 |
+
|
64 |
+
if ('serviceWorker' in navigator) {
|
65 |
+
navigator.serviceWorker.register(swUrl)
|
66 |
+
.then(() => {
|
67 |
+
document.getElementById('log').innerHTML = '✅ Service Worker 登録完了。通信をログします。';
|
68 |
+
})
|
69 |
+
.catch(err => {
|
70 |
+
document.getElementById('log').innerHTML = '❌ Service Worker 登録失敗: ' + err;
|
71 |
+
});
|
72 |
+
|
73 |
+
navigator.serviceWorker.addEventListener('message', (event) => {
|
74 |
+
const data = event.data;
|
75 |
+
const div = document.createElement('div');
|
76 |
+
div.className = data.type === 'fetch-log' ? 'success' : 'error';
|
77 |
+
|
78 |
+
if (data.type === 'fetch-log') {
|
79 |
+
div.innerHTML = `
|
80 |
+
✅ [${data.method}] ${data.url} — ${data.status}<br>
|
81 |
+
<pre>${data.body}</pre>
|
82 |
+
`;
|
83 |
+
} else if (data.type === 'fetch-error') {
|
84 |
+
div.innerHTML = `
|
85 |
+
❌ [${data.method}] ${data.url}<br>
|
86 |
+
エラー: ${data.error}
|
87 |
+
`;
|
88 |
+
}
|
89 |
+
|
90 |
+
document.getElementById('log').appendChild(div);
|
91 |
+
});
|
92 |
+
}
|
93 |
+
</script>
|
94 |
+
|
95 |
+
<script>
|
96 |
+
// テスト用のfetch通信を発生させる
|
97 |
+
fetch('https://jsonplaceholder.typicode.com/posts/1');
|
98 |
+
fetch('https://httpstat.us/404'); // エラーになる通信
|
99 |
+
</script>
|
100 |
</body>
|
101 |
+
</html>
|