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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +55 -123
index.html CHANGED
@@ -1,140 +1,72 @@
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; }
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>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>
 
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>