请求修改Tampermonkey脚本以支持多关键词检测及逻辑调整
多关键词检测的Tampermonkey脚本修改方案
完整修改代码
window.addEventListener('load', function() { console.log("Page loaded."); // 自定义多关键词数组,按需修改 var lookForKeywords = ["cheese", "milk", "bread"]; var content = document.body.textContent || document.body.innerText; // 遍历数组检测是否存在匹配关键词 var foundKeyword = lookForKeywords.find(keyword => content.indexOf(keyword) !== -1); if(foundKeyword) { console.log(`Text found! Alerting: ${foundKeyword}`); alert(`Found: ${foundKeyword}`); } else { console.log("None of the keywords found - reloading."); location.reload(); } });
核心改动点
- 将单个关键词变量替换为关键词数组
lookForKeywords,可直接添加/删除需要检测的内容 - 使用
Array.find()方法快速定位第一个匹配的关键词,无匹配则返回undefined - 补全原脚本缺失的
location.reload()语句,确保未检测到关键词时正常刷新页面 - 弹窗内容会显示实际检测到的具体关键词,更直观
自定义提示
- 支持中文关键词,比如把数组改成
["优惠", "秒杀", "补货"]即可检测中文内容 - 若需要区分大小写的精确匹配,可将
content.indexOf(keyword) !== -1替换为content.includes(keyword)(效果一致,但更直观);如果需要忽略大小写,改用content.toLowerCase().includes(keyword.toLowerCase())
内容的提问来源于stack exchange,提问作者Haza jir
相关产品推荐
相关产品推荐

