Chrome未打包扩展中获取本地图片描述的问题求助
解决方案:用配套文本文件存储图片描述
既然Exif路径走不通,最简单的办法是给每张图片配一个同名的文本文件,比如(1).jpg对应(1).txt,把图片描述写在txt里。这样不用碰Exif,也能避开CSP问题。
修改你的JavaScript代码如下:
document.getElementById("btn").addEventListener("click", myFunction); function myFunction() { var imgElement = document.getElementById('img'); // 生成随机数字 var randomNum = Math.floor(Math.random() * 552); var imgPath = `Images/Photoes/(${randomNum}).jpg`; var descPath = `Images/Photoes/(${randomNum}).txt`; // 更新图片 imgElement.src = imgPath; // 加载对应的描述文本 var textElement = document.getElementById('imgD'); fetch(descPath) .then(response => response.text()) .then(description => { textElement.textContent = description; }) .catch(error => { // 如果找不到对应txt文件,显示默认文本 textElement.textContent = "暂无描述"; }); } window.onload = function () { document.getElementById("btn").click(); }
操作步骤:
- 在
Images/Photoes文件夹里,给每一张(num).jpg创建对应的(num).txt文件,把图片描述复制进去 - 确保你的manifest.json里的
content_security_policy未禁止访问本地资源(扩展默认允许访问自身包内的资源,无需额外配置)
为什么不用Exif?
你提到Exif拿不到需要的标题/描述,大概率是因为你的图片本身没有在Exif的对应字段(比如ImageDescription)里存储这些信息,或者调用exif.js的方式不对。相比手动给每张图片编辑Exif,用配套txt文件更直观,操作起来也更适合你当前的JS水平。
内容的提问来源于stack exchange,提问作者BM-Code
相关产品推荐
相关产品推荐

