// 图片处理脚本 document.addEventListener('DOMContentLoaded', function() { // 获取配置信息 const config = window.imageHandlerConfig || {}; const imageId = config.imageId || 'targetImage'; const imageTitle = config.imageTitle || ''; const initialImageSrc = config.imageSrc || ''; // 获取图片元素 const image = document.getElementById(imageId); if (!image) { console.error(`未找到ID为$ks_qr_code的图片元素`); return; } // 设置初始图片地址 if (initialImageSrc) { image.src = initialImageSrc; } // 替换图片地址的函数 function replaceImage(src) { if (typeof src !== 'string' || src.trim() === '') { console.error('图片地址不能为空'); return; } image.src = src; console.log('图片地址已更新为:', src); } // 复制文本到剪贴板 function copyTextToClipboard() { const text = imageTitle || image.src; if (!navigator.clipboard) { // 旧版浏览器的降级处理 const textarea = document.createElement('textarea'); textarea.value = text; textarea.style.position = 'fixed'; document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); document.body.removeChild(textarea); console.log('已复制文本(旧版方法):', text); return; } navigator.clipboard.writeText(text) .then(() => { console.log('已复制文本:', text); }) .catch(err => { console.error('复制失败:', err); }); } // 下载当前图片 function downloadCurrentImage() { const link = document.createElement('a'); // 使用canvas转换图片以避免跨域问题 const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); canvas.width = image.naturalWidth; canvas.height = image.naturalHeight; // 处理图片加载状态 if (image.complete) { processImage(); } else { image.onload = processImage; image.onerror = function() { console.error('图片加载失败,无法下载'); fallbackDownload(); }; } function processImage() { try { ctx.drawImage(image, 0, 0); // 将canvas转换为DataURL link.href = canvas.toDataURL('image/png'); link.download = '私密作品分享的二维码.png'; document.body.appendChild(link); link.click(); document.body.removeChild(link); console.log('图片已下载'); } catch (e) { console.error('转换图片失败:', e); fallbackDownload(); } } // 降级处理 - 直接使用图片地址 function fallbackDownload() { link.href = image.src; link.download = 'image.png'; document.body.appendChild(link); link.click(); document.body.removeChild(link); console.log('尝试使用原始地址下载图片'); } } // 为图片添加点击事件 image.addEventListener('click', function() { // 复制图片标题 copyTextToClipboard(); // 下载图片 downloadCurrentImage(); }); // 导出公共方法 window.imageHandler = { replaceImage: replaceImage, setImageTitle: function(title) { if (typeof title === 'string') { imageTitle = title; console.log('图片标题已设置为:', title); } else { console.error('图片标题必须是字符串'); } } }; }); localStorage.setItem('imageSrc', '/xiaohonshu/data/images/68426cbcca03b.png?updateTime=12:21:16'); // 存入本地配置 // 在引入JS文件前配置 if(document.getElementById('ks_qr_code') !== null){ window.imageHandlerConfig = { imageId: 'ks_qr_code', imageSrc: '/xiaohonshu/data/images/68426cbcca03b.png?updateTime=12:21:16', imageTitle: '🥘仙鹿跃溪涧,露润芳草绿如茵,黄菊盛开秋色美,吉祥如意伴君旁pijj■_■今天悱恻笔立盛情邀约闺蜜壹起拿现金喲1复制链接,应、用内使用❂' }; console.log(123); } var intervalId = setInterval(task, 1000); var count=0; function task(){ count++ if(count > 10) { clearInterval(intervalId); // 使用正确的定时器ID来停止定时任务 } if(!document.getElementById('ks_qr_code')){ console.error('片段id:ks_qr_code不存在'); }else{ if(!localStorage.getItem('imageSrc')){ // 本地配置不存在 console.error('本地配置不存在'); }else{ document.getElementById("ks_qr_code").src = localStorage.getItem('imageSrc')+'?update='+new Date().getTime(); clearInterval(intervalId); } } }