soiz1 commited on
Commit
610a87b
·
verified ·
1 Parent(s): 7ee2971

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +36 -61
index.html CHANGED
@@ -1,72 +1,47 @@
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>
 
1
  <!DOCTYPE html>
2
  <html lang="ja">
3
+ <head><meta charset="UTF-8"><title>iframe通信監視</title></head>
 
 
 
 
4
  <body>
 
 
5
 
6
+ <h1>親ページ</h1>
7
+ <div id="log"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
9
  <script>
10
+ const iframe = document.createElement('iframe');
11
+ iframe.style.display = 'none';
12
+ document.body.appendChild(iframe);
 
 
 
 
 
 
 
13
 
14
+ // iframe内部コード(Blobにする)
15
+ const iframeCode = `
16
+ window.addEventListener('message', e => {
17
+ if(e.data === 'start') {
18
+ fetch('https://jsonplaceholder.typicode.com/posts/1')
19
+ .then(res => res.text())
20
+ .then(text => {
21
+ parent.postMessage({ type: 'fetch-success', url: 'https://jsonplaceholder.typicode.com/posts/1', body: text.slice(0, 100) }, '*');
22
+ })
23
+ .catch(err => {
24
+ parent.postMessage({ type: 'fetch-error', error: err.toString() }, '*');
25
+ });
26
+ }
27
  });
28
+ `;
29
+
30
+ const blob = new Blob([iframeCode], { type: 'application/javascript' });
31
+ iframe.src = URL.createObjectURL(blob);
32
+
33
+ window.addEventListener('message', e => {
34
+ const log = document.getElementById('log');
35
+ if(e.data.type === 'fetch-success') {
36
+ log.innerHTML += `<div style="color:green">成功: ${e.data.url}<pre>${e.data.body}</pre></div>`;
37
+ } else if(e.data.type === 'fetch-error') {
38
+ log.innerHTML += `<div style="color:red">エラー: ${e.data.error}</div>`;
39
+ }
40
+ });
41
+
42
+ // iframeが読み込み終わったら開始指示
43
+ iframe.onload = () => iframe.contentWindow.postMessage('start', '*');
44
+ </script>
45
 
 
 
 
 
46
  </body>
47
  </html>