这是我的 manifest.json 档案
{
"manifest_version": 3,
"name": "上传类型提醒",
"version": "1.0",
"permissions": ["activeTab", "tabs", "webRequest",
"declarativeNetRequest"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["background.js"]
}
]
}
这是我的 background.js 档案
document.addEventListener('change', function(event) {
const input = event.target;
if (input.type === 'file') {
const files = input.files;
for (let i = 0; i < files.length; i++) {
const fileName = files[i].name.toLowerCase();
if (fileName.endsWith('.bmp') ||
fileName.endsWith('.png') ||
fileName.endsWith('.gif') ||
fileName.endsWith('.doc') ||
fileName.endsWith('.docx') ||
fileName.endsWith('.xls') ||
fileName.endsWith('.xlsx')) {
alert('此文件类型不允许上传:' + files[i].name);
input.value = ''; // 清空档案输入
// 使用 setTimeout 在 0.001 秒后关闭目前标签页
setTimeout(() => {
window.location.href = 'about:blank'; // 跳到一个空白页
}, 1); // 1 毫秒 = 0.001 秒
break;
}
}
}
});
我想再加入
如果我浏览到某个指定网站时
如果浏览页面有出现 .doc 或 .xlx 或 .bmp 或 .png 这些文字的时候
就像我的 background.js 档案 里面一样
出现阻挡画面,然后再0.001秒 后自动跳到另一个空白页