`; saveWindow.document.write(htmlContent); saveWindow.document.close(); } function takeScreenshot() { var button = document.getElementById('sst-btn'); var tip = document.getElementById('sst-tip'); tip.style.display = 'flex'; button.innerHTML = '截圖中...'; button.disabled = true; button.style.background = '#666'; button.style.color = 'white'; // 準確截取當前可視區域 html2canvas(document.body, { useCORS: true, scale: 2, logging: false, backgroundColor: '#ffffff', scrollX: 0, scrollY: -window.scrollY, windowWidth: document.documentElement.scrollWidth, windowHeight: document.documentElement.scrollHeight, x: 0, y: window.scrollY, width: window.innerWidth, height: window.innerHeight, foreignObjectRendering: false }).then(canvas => { var imgData = canvas.toDataURL('image/png'); var previewContainer = document.getElementById('sst-preview'); previewContainer.innerHTML = `截圖預覽`; document.getElementById('sst-modal').style.display = 'flex'; // 爲蘋果手機優化:添加點擊保存按鈕 var img = previewContainer.querySelector('img'); // 綁定圖片點擊事件(備用) img.addEventListener('click', function() { saveToAlbum(this); }); }).catch(error => { console.error('截圖失敗:', error); alert('截圖失敗,請重試'); resetScreenshotButton(); }); } function resetScreenshotButton() { var button = document.getElementById('sst-btn'); var tip = document.getElementById('sst-tip'); button.innerHTML = '截圖保存'; button.disabled = false; button.style.background = 'red'; button.style.color = 'yellow'; tip.style.display = 'none'; } // 綁定事件 document.getElementById('sst-btn').addEventListener('click', takeScreenshot); // 綁定保存按鈕事件 document.getElementById('sst-save-btn').addEventListener('click', function() { var img = document.querySelector('#sst-preview img'); if (img) { saveToAlbum(img); } }); document.getElementById('sst-close').addEventListener('click', function() { document.getElementById('sst-modal').style.display = 'none'; resetScreenshotButton(); }); // 點擊模態框外部關閉 document.getElementById('sst-modal').addEventListener('click', function(e) { if (e.target === this) { this.style.display = 'none'; resetScreenshotButton(); } }); // 防止內容區域點擊事件冒泡 document.querySelector('.sst-content').addEventListener('click', function(e) { e.stopPropagation(); }); } })();