soiz1 commited on
Commit
c7c1e55
·
verified ·
1 Parent(s): bbd3ce4

Update 2.html

Browse files
Files changed (1) hide show
  1. 2.html +12 -4
2.html CHANGED
@@ -89,14 +89,17 @@
89
  </li>
90
  </ol>
91
  <script>
 
 
92
  document.addEventListener('visibilitychange', async () => {
93
  if (document.visibilityState === 'hidden') {
 
94
  if ('documentPictureInPicture' in window) {
95
  try {
96
- const pipWindow = await documentPictureInPicture.requestWindow();
97
  pipWindow.document.body.innerHTML = document.body.innerHTML;
98
 
99
- // 必要に応じてスタイルもコピー
100
  const styles = document.querySelectorAll('style, link[rel="stylesheet"]');
101
  styles.forEach(style => {
102
  pipWindow.document.head.appendChild(style.cloneNode(true));
@@ -105,12 +108,17 @@ document.addEventListener('visibilitychange', async () => {
105
  } catch (error) {
106
  console.error('PiPウィンドウ起動エラー:', error);
107
  }
108
- } else {
109
- console.warn('documentPictureInPictureがサポートされていません');
 
 
 
 
110
  }
111
  }
112
  });
113
  </script>
114
 
 
115
  </body>
116
  </html>
 
89
  </li>
90
  </ol>
91
  <script>
92
+ let pipWindow = null; // PiPウィンドウを保存する変数
93
+
94
  document.addEventListener('visibilitychange', async () => {
95
  if (document.visibilityState === 'hidden') {
96
+ // タブから離れたとき
97
  if ('documentPictureInPicture' in window) {
98
  try {
99
+ pipWindow = await documentPictureInPicture.requestWindow();
100
  pipWindow.document.body.innerHTML = document.body.innerHTML;
101
 
102
+ // スタイルもコピーする
103
  const styles = document.querySelectorAll('style, link[rel="stylesheet"]');
104
  styles.forEach(style => {
105
  pipWindow.document.head.appendChild(style.cloneNode(true));
 
108
  } catch (error) {
109
  console.error('PiPウィンドウ起動エラー:', error);
110
  }
111
+ }
112
+ } else if (document.visibilityState === 'visible') {
113
+ // タブに戻ったとき
114
+ if (pipWindow && !pipWindow.closed) {
115
+ pipWindow.close();
116
+ pipWindow = null;
117
  }
118
  }
119
  });
120
  </script>
121
 
122
+
123
  </body>
124
  </html>