Update index.html
Browse files- index.html +55 -123
index.html
CHANGED
@@ -1,140 +1,72 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
-
<meta charset="
|
5 |
-
<title>
|
6 |
-
<style>
|
7 |
-
|
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><script src="/dev-tools.js"></script>
|
14 |
<body>
|
15 |
-
<h1
|
16 |
-
<div id="log"
|
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 |
-
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 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
}
|
87 |
-
<\/script>
|
88 |
-
</body>
|
89 |
-
</html>
|
90 |
-
`;
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
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 |
-
//
|
136 |
-
fetch('https://jsonplaceholder.typicode.com/
|
137 |
-
fetch('https://httpstat.us/
|
138 |
</script>
|
139 |
</body>
|
140 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="ja">
|
3 |
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<title>Blob + iframe で Service Worker</title>
|
6 |
+
<style>pre{background:#f0f0f0;padding:4px;margin:4px 0;}</style>
|
7 |
+
</head>
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
<body>
|
9 |
+
<h1>通信ログ</h1>
|
10 |
+
<div id="log"></div>
|
11 |
|
12 |
+
<!-- ① SW コード埋め込み -->
|
13 |
+
<script id="sw-code" type="text/plain">
|
14 |
+
self.addEventListener('fetch', event => {
|
15 |
+
const { request } = event;
|
16 |
+
event.respondWith(
|
17 |
+
fetch(request).then(res => {
|
18 |
+
res.clone().text().then(body => {
|
19 |
+
self.clients.matchAll().then(clients =>
|
20 |
+
clients.forEach(c =>
|
21 |
+
c.postMessage({ type:'log', url:request.url, status:res.status, snippet:body.slice(0,100) })
|
22 |
+
)
|
23 |
+
);
|
24 |
+
});
|
25 |
+
return res;
|
26 |
+
}).catch(err => {
|
27 |
+
self.clients.matchAll().then(clients =>
|
28 |
+
clients.forEach(c =>
|
29 |
+
c.postMessage({ type:'error', url:request.url, error:err.toString() })
|
30 |
+
)
|
31 |
+
);
|
32 |
+
throw err;
|
33 |
+
})
|
34 |
+
);
|
35 |
+
});
|
36 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
<!-- ②–⑤ メインスクリプト -->
|
39 |
+
<script>
|
40 |
+
const swSource = document.getElementById('sw-code').textContent;
|
41 |
+
const swBlobUrl = URL.createObjectURL(new Blob([swSource], { type:'application/javascript' }));
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
const iframeHtml = `
|
44 |
+
<!DOCTYPE html>
|
45 |
+
<html><head><meta charset="utf-8"></head><body>
|
46 |
+
<script>
|
47 |
+
navigator.serviceWorker.register('${swBlobUrl}')
|
48 |
+
.catch(e=>console.error('SW登録失敗',e));
|
49 |
+
</script>
|
50 |
+
</body></html>`;
|
51 |
+
const iframeUrl = URL.createObjectURL(new Blob([iframeHtml], { type:'text/html' }));
|
52 |
|
|
|
53 |
const iframe = document.createElement('iframe');
|
54 |
+
iframe.style.display = 'none';
|
55 |
iframe.src = iframeUrl;
|
|
|
56 |
document.body.appendChild(iframe);
|
57 |
|
58 |
+
navigator.serviceWorker.addEventListener('message', e => {
|
59 |
+
const d = e.data;
|
60 |
+
const pre = document.createElement('pre');
|
61 |
+
pre.textContent = d.type==='log'
|
62 |
+
? `[${d.status}] ${d.url}\n…${d.snippet}`
|
63 |
+
: `⚠️ ERROR: ${d.url}\n→ ${d.error}`;
|
64 |
+
document.getElementById('log').appendChild(pre);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
});
|
66 |
|
67 |
+
// テスト fetch
|
68 |
+
fetch('https://jsonplaceholder.typicode.com/todos/1');
|
69 |
+
fetch('https://httpstat.us/500');
|
70 |
</script>
|
71 |
</body>
|
72 |
</html>
|