Update index.html
Browse files- index.html +99 -35
index.html
CHANGED
@@ -1,47 +1,111 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
-
<head
|
|
|
|
|
|
|
4 |
<body>
|
|
|
|
|
5 |
|
6 |
-
<
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
.then(
|
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 |
</body>
|
47 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<title>iframe + BlobでService Worker登録</title>
|
6 |
+
</head>
|
7 |
<body>
|
8 |
+
<h1>通信ログ(親ページ)</h1>
|
9 |
+
<div id="log"></div>
|
10 |
|
11 |
+
<script>
|
12 |
+
// iframe内で動くHTML(Service Worker登録用)
|
13 |
+
const iframeHtml = `
|
14 |
+
<!DOCTYPE html>
|
15 |
+
<html lang="ja">
|
16 |
+
<head><meta charset="UTF-8"></head>
|
17 |
+
<body>
|
18 |
+
<script>
|
19 |
+
// Service Workerのコード(文字列)
|
20 |
+
const swCode = \`
|
21 |
+
self.addEventListener('fetch', event => {
|
22 |
+
const { request } = event;
|
23 |
+
event.respondWith(
|
24 |
+
fetch(request).then(response => {
|
25 |
+
// クライアントにログを送信
|
26 |
+
self.clients.matchAll().then(clients => {
|
27 |
+
clients.forEach(client => {
|
28 |
+
client.postMessage({
|
29 |
+
type: 'log',
|
30 |
+
url: request.url,
|
31 |
+
status: response.status,
|
32 |
+
method: request.method
|
33 |
+
});
|
34 |
+
});
|
35 |
+
});
|
36 |
+
return response;
|
37 |
+
}).catch(error => {
|
38 |
+
self.clients.matchAll().then(clients => {
|
39 |
+
clients.forEach(client => {
|
40 |
+
client.postMessage({
|
41 |
+
type: 'error',
|
42 |
+
url: request.url,
|
43 |
+
message: error.message
|
44 |
+
});
|
45 |
+
});
|
46 |
+
});
|
47 |
+
throw error;
|
48 |
})
|
49 |
+
);
|
50 |
+
});
|
51 |
+
\`;
|
|
|
|
|
|
|
52 |
|
53 |
+
// BlobでSWスクリプト作成
|
54 |
+
const blob = new Blob([swCode], {type: 'application/javascript'});
|
55 |
+
const swUrl = URL.createObjectURL(blob);
|
56 |
|
57 |
+
if ('serviceWorker' in navigator) {
|
58 |
+
navigator.serviceWorker.register(swUrl).then(() => {
|
59 |
+
parent.postMessage({type: 'sw-registered'}, '*');
|
60 |
+
}).catch(e => {
|
61 |
+
parent.postMessage({type: 'sw-error', message: e.message}, '*');
|
62 |
+
});
|
63 |
+
|
64 |
+
// SWからのメッセージを親iframeへ中継
|
65 |
+
navigator.serviceWorker.addEventListener('message', event => {
|
66 |
+
parent.postMessage({type: 'sw-message', data: event.data}, '*');
|
67 |
+
});
|
68 |
}
|
69 |
+
<\/script>
|
70 |
+
</body>
|
71 |
+
</html>
|
72 |
+
`;
|
73 |
+
|
74 |
+
// iframe用Blob URL作成
|
75 |
+
const iframeBlob = new Blob([iframeHtml], {type: 'text/html'});
|
76 |
+
const iframeUrl = URL.createObjectURL(iframeBlob);
|
77 |
+
|
78 |
+
// iframe作成してDOMに追加
|
79 |
+
const iframe = document.createElement('iframe');
|
80 |
+
iframe.src = iframeUrl;
|
81 |
+
iframe.style.width = '100%';
|
82 |
+
iframe.style.height = '200px';
|
83 |
+
document.body.appendChild(iframe);
|
84 |
+
|
85 |
+
// ログ表示領域
|
86 |
+
const logDiv = document.getElementById('log');
|
87 |
|
88 |
+
// iframeからのpostMessage受信
|
89 |
+
window.addEventListener('message', e => {
|
90 |
+
if (!e.data) return;
|
91 |
+
|
92 |
+
if (e.data.type === 'sw-registered') {
|
93 |
+
logDiv.textContent += '✅ Service Worker登録完了\n';
|
94 |
+
} else if (e.data.type === 'sw-error') {
|
95 |
+
logDiv.textContent += '❌ Service Worker登録エラー: ' + e.data.message + '\n';
|
96 |
+
} else if (e.data.type === 'sw-message') {
|
97 |
+
const d = e.data.data;
|
98 |
+
if (d.type === 'log') {
|
99 |
+
logDiv.textContent += `🔵 [${d.method}] ${d.url} → ${d.status}\n`;
|
100 |
+
} else if (d.type === 'error') {
|
101 |
+
logDiv.textContent += `🔴 エラー: ${d.message} (${d.url})\n`;
|
102 |
+
}
|
103 |
+
}
|
104 |
+
});
|
105 |
|
106 |
+
// テストfetch(親ページから)
|
107 |
+
fetch('https://jsonplaceholder.typicode.com/posts/1');
|
108 |
+
fetch('https://httpstat.us/404');
|
109 |
+
</script>
|
110 |
</body>
|
111 |
</html>
|