Update index.html
Browse files- index.html +110 -71
index.html
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
-
<meta charset="UTF-8"
|
5 |
-
<title
|
6 |
<style>
|
7 |
body { font-family: sans-serif; padding: 1em; }
|
8 |
#log { margin-top: 1em; font-size: 14px; }
|
@@ -12,90 +12,129 @@
|
|
12 |
</style>
|
13 |
</head>
|
14 |
<body>
|
15 |
-
<h1
|
16 |
-
<div id="log">Service Worker
|
17 |
|
18 |
<script>
|
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 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
.catch(err => {
|
70 |
-
document.getElementById('log').innerHTML = '❌ Service Worker 登録失敗: ' + err;
|
71 |
-
});
|
72 |
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
const div = document.createElement('div');
|
76 |
-
div.className =
|
77 |
|
78 |
-
if (
|
79 |
-
div.innerHTML =
|
80 |
-
✅ [
|
81 |
-
<pre
|
82 |
-
|
83 |
-
} else if (
|
84 |
-
div.innerHTML =
|
85 |
-
❌ [
|
86 |
-
エラー:
|
87 |
-
|
88 |
}
|
89 |
|
90 |
-
|
91 |
-
}
|
92 |
-
}
|
93 |
-
</script>
|
94 |
|
95 |
-
|
96 |
-
// テスト用のfetch通信を発生させる
|
97 |
fetch('https://jsonplaceholder.typicode.com/posts/1');
|
98 |
-
fetch('https://httpstat.us/404');
|
99 |
</script>
|
100 |
</body>
|
101 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<title>1ファイル完結型 Service Worker via iframe+Blob</title>
|
6 |
<style>
|
7 |
body { font-family: sans-serif; padding: 1em; }
|
8 |
#log { margin-top: 1em; font-size: 14px; }
|
|
|
12 |
</style>
|
13 |
</head>
|
14 |
<body>
|
15 |
+
<h1>1ファイルで完結する Service Worker (iframe + Blob)</h1>
|
16 |
+
<div id="log">Service Worker の登録を試みています…</div>
|
17 |
|
18 |
<script>
|
19 |
+
// iframe内のHTMLを文字列として用意(Service Workerスクリプト含む)
|
20 |
+
const iframeHTML = `
|
21 |
+
<!DOCTYPE html>
|
22 |
+
<html>
|
23 |
+
<head><title>SW iframe</title></head>
|
24 |
+
<body>
|
25 |
+
<script type="text/javascript">
|
26 |
+
// Service Worker スクリプトを文字列で用意
|
27 |
+
const swCode = \`
|
28 |
+
self.addEventListener('fetch', event => {
|
29 |
+
const url = event.request.url;
|
30 |
+
const method = event.request.method;
|
31 |
|
32 |
+
event.respondWith(
|
33 |
+
fetch(event.request)
|
34 |
+
.then(response => {
|
35 |
+
const clone = response.clone();
|
36 |
+
clone.text().then(body => {
|
37 |
+
self.clients.matchAll().then(clients => {
|
38 |
+
clients.forEach(client => {
|
39 |
+
client.postMessage({
|
40 |
+
type: 'fetch-log',
|
41 |
+
url,
|
42 |
+
method,
|
43 |
+
status: response.status,
|
44 |
+
body: body.slice(0, 200)
|
45 |
+
});
|
46 |
+
});
|
47 |
+
});
|
48 |
});
|
49 |
+
return response;
|
50 |
+
})
|
51 |
+
.catch(error => {
|
52 |
+
self.clients.matchAll().then(clients => {
|
53 |
+
clients.forEach(client => {
|
54 |
+
client.postMessage({
|
55 |
+
type: 'fetch-error',
|
56 |
+
url,
|
57 |
+
method,
|
58 |
+
error: error.toString()
|
59 |
+
});
|
60 |
+
});
|
61 |
+
});
|
62 |
+
throw error;
|
63 |
+
})
|
64 |
+
);
|
65 |
+
});
|
66 |
+
\`;
|
67 |
+
|
68 |
+
// Blob URLとして作成
|
69 |
+
const blob = new Blob([swCode], { type: 'application/javascript' });
|
70 |
+
const swBlobUrl = URL.createObjectURL(blob);
|
71 |
+
|
72 |
+
// Service Worker 登録
|
73 |
+
if ('serviceWorker' in navigator) {
|
74 |
+
navigator.serviceWorker.register(swBlobUrl)
|
75 |
+
.then(() => {
|
76 |
+
parent.postMessage({ type: 'sw-registered' }, '*');
|
77 |
+
})
|
78 |
+
.catch(err => {
|
79 |
+
parent.postMessage({ type: 'sw-error', error: err.toString() }, '*');
|
80 |
});
|
81 |
+
|
82 |
+
// Service Worker からのメッセージを受け取り親に中継
|
83 |
+
navigator.serviceWorker.addEventListener('message', event => {
|
84 |
+
parent.postMessage({ type: 'sw-message', data: event.data }, '*');
|
85 |
+
});
|
86 |
+
}
|
87 |
+
<\/script>
|
88 |
+
</body>
|
89 |
+
</html>
|
90 |
`;
|
91 |
|
92 |
+
// Blob URLとして iframe のページを作る
|
93 |
+
const iframeBlob = new Blob([iframeHTML], { type: 'text/html' });
|
94 |
+
const iframeUrl = URL.createObjectURL(iframeBlob);
|
95 |
+
|
96 |
+
// iframe要素作成
|
97 |
+
const iframe = document.createElement('iframe');
|
98 |
+
iframe.src = iframeUrl;
|
99 |
+
iframe.style.display = 'none'; // 表示不要
|
100 |
+
document.body.appendChild(iframe);
|
101 |
|
102 |
+
// メッセージ受け取り処理
|
103 |
+
const logDiv = document.getElementById('log');
|
104 |
+
window.addEventListener('message', event => {
|
105 |
+
const data = event.data;
|
106 |
+
if (!data || !data.type) return;
|
|
|
|
|
|
|
107 |
|
108 |
+
if (data.type === 'sw-registered') {
|
109 |
+
logDiv.textContent = '✅ Service Worker 登録完了 (iframe 内)';
|
110 |
+
}
|
111 |
+
else if (data.type === 'sw-error') {
|
112 |
+
logDiv.textContent = '❌ Service Worker 登録失敗: ' + data.error;
|
113 |
+
}
|
114 |
+
else if (data.type === 'sw-message') {
|
115 |
+
const msg = data.data;
|
116 |
const div = document.createElement('div');
|
117 |
+
div.className = msg.type === 'fetch-log' ? 'success' : 'error';
|
118 |
|
119 |
+
if (msg.type === 'fetch-log') {
|
120 |
+
div.innerHTML = \`
|
121 |
+
✅ [\${msg.method}] \${msg.url} — \${msg.status}<br>
|
122 |
+
<pre>\${msg.body}</pre>
|
123 |
+
\`;
|
124 |
+
} else if (msg.type === 'fetch-error') {
|
125 |
+
div.innerHTML = \`
|
126 |
+
❌ [\${msg.method}] \${msg.url}<br>
|
127 |
+
エラー: \${msg.error}
|
128 |
+
\`;
|
129 |
}
|
130 |
|
131 |
+
logDiv.appendChild(div);
|
132 |
+
}
|
133 |
+
});
|
|
|
134 |
|
135 |
+
// 動作確認用fetchを親ページから実行
|
|
|
136 |
fetch('https://jsonplaceholder.typicode.com/posts/1');
|
137 |
+
fetch('https://httpstat.us/404');
|
138 |
</script>
|
139 |
</body>
|
140 |
</html>
|