Update index.html
Browse files- index.html +37 -85
index.html
CHANGED
@@ -1,102 +1,54 @@
|
|
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; }
|
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>Service Worker
|
16 |
-
<
|
17 |
|
18 |
-
<!-- Service Worker Script as text -->
|
19 |
-
<script id="sw-script" type="javascript/worker">
|
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 |
-
</script>
|
60 |
-
|
61 |
-
<!-- Register Service Worker from inline script -->
|
62 |
<script>
|
63 |
-
|
64 |
-
const
|
65 |
-
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
}).catch(error => {
|
71 |
-
document.getElementById('log').innerHTML = '❌ 登録失敗: ' + error.message;
|
72 |
});
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
<pre>${data.body}</pre>
|
83 |
-
`;
|
84 |
-
} else if (data.type === 'fetch-error') {
|
85 |
-
div.innerHTML = `
|
86 |
-
❌ [${data.method}] ${data.url}<br>
|
87 |
-
エラー: ${data.error}
|
88 |
-
`;
|
89 |
-
}
|
90 |
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
}
|
94 |
-
</script>
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
100 |
</script>
|
101 |
</body>
|
102 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<title>Blob Service Worker サンプル</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
</head>
|
7 |
<body>
|
8 |
+
<h1>Blob Service Worker デモ</h1>
|
9 |
+
<button id="fetch-button">データ取得</button>
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
<script>
|
12 |
+
// 1. Service Worker のコードを文字列として用意
|
13 |
+
const swCode = `
|
14 |
+
self.addEventListener('install', event => {
|
15 |
+
console.log('[SW] Installed');
|
16 |
+
self.skipWaiting(); // 即時有効化(開発用)
|
17 |
+
});
|
18 |
|
19 |
+
self.addEventListener('activate', event => {
|
20 |
+
console.log('[SW] Activated');
|
21 |
+
event.waitUntil(self.clients.claim());
|
|
|
|
|
22 |
});
|
23 |
|
24 |
+
self.addEventListener('fetch', event => {
|
25 |
+
console.log('[SW] Fetch intercepted:', event.request.url);
|
26 |
+
});
|
27 |
+
`;
|
28 |
|
29 |
+
// 2. Blob を作成し、オブジェクトURLに変換
|
30 |
+
const blob = new Blob([swCode], { type: 'application/javascript' });
|
31 |
+
const blobUrl = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
// 3. Service Worker を登録
|
34 |
+
if ('serviceWorker' in navigator) {
|
35 |
+
navigator.serviceWorker.register(blobUrl)
|
36 |
+
.then(reg => {
|
37 |
+
console.log('[Main] Service Worker registered from Blob:', reg.scope);
|
38 |
+
})
|
39 |
+
.catch(err => {
|
40 |
+
console.error('[Main] Registration failed:', err);
|
41 |
+
});
|
42 |
}
|
|
|
43 |
|
44 |
+
// 4. デモ用:ボタンクリックで fetch 通信
|
45 |
+
document.getElementById('fetch-button').addEventListener('click', () => {
|
46 |
+
fetch('https://jsonplaceholder.typicode.com/posts/1')
|
47 |
+
.then(res => res.json())
|
48 |
+
.then(data => {
|
49 |
+
console.log('[Main] Fetched data:', data);
|
50 |
+
});
|
51 |
+
});
|
52 |
</script>
|
53 |
</body>
|
54 |
</html>
|