是否存在进阶版AdCloseGold类Chrome插件?求开发教程
Hey there! 我完全懂你被自动弹出的广告标签烦到炸的感觉——之前用AdCloseGold踩的那些坑,我也多少遇见过。先给你推荐几个现成的进阶插件,要是能直接解决问题最好,不行的话再给你一步步讲怎么自己开发一个专属的插件:
现成进阶插件推荐
这些插件基本能覆盖你所有需求,不用自己折腾:
- Auto Close Tabs:支持通配符匹配URL(比如
*.example.com/ad*这种写法),没有URL数量限制,还能设置自动关闭空白页、404页面的规则,自带一键把当前页面加入黑名单的按钮,甚至有预设的广告域名列表可以直接导入,省得你一个个加。 - Tab Manager Plus:它的URL匹配支持正则表达式,比通配符更灵活,也能处理空白页和错误页面,而且数据默认存在本地(可选同步到谷歌账户),完全不用担心存储限制的问题。
如果这些插件还是不完全贴合你的使用习惯,那自己开发一个确实是个好主意——毕竟你有基础的Chrome插件开发知识,下面是我整理的详细教程,一步步来就能搞定:
自定义Chrome广告标签自动关闭插件开发教程
1. 先搭好插件的基础结构
创建一个叫AutoAdTabKiller的文件夹,里面放这几个核心文件就行:
manifest.json:插件的配置核心,必须要有background.js:后台运行的脚本,用来监听标签创建/更新事件popup.html:可选的弹窗页面,用来让你添加规则、设置开关popup.js:弹窗的交互脚本,处理按钮点击、规则保存这些逻辑
manifest.json 基础配置
先写好配置,要申请必要的权限才能访问标签和存储:
{ "manifest_version": 3, "name": "Auto Ad Tab Killer", "version": "1.0", "description": "自动关闭广告标签、空白页和404页面,支持通配符和正则匹配", "permissions": ["tabs", "storage", "scripting"], "background": { "service_worker": "background.js" }, "action": { "default_popup": "popup.html", "default_icon": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" } }, "icons": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" } }
(注:图标可以自己找免费的或者用在线工具生成,暂时没有的话也可以先留空,不影响测试)
2. 后台脚本核心逻辑(background.js)
这个脚本是插件的大脑,负责监听新标签,判断要不要关闭它:
监听标签的创建和更新
// 监听新标签打开 chrome.tabs.onCreated.addListener(async (tab) => { await checkAndCloseTab(tab); }); // 监听标签加载完成(比如页面加载后变成404) chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { if (changeInfo.status === 'complete') { await checkAndCloseTab(tab); } });
判断是否关闭标签的核心函数
async function checkAndCloseTab(tab) { // 从本地存储获取我们设置的规则和开关 const { blockRules = [], closeBlank = true, close404 = true } = await chrome.storage.local.get(['blockRules', 'closeBlank', 'close404']); // 处理空白页 if (closeBlank && tab.url === 'chrome://newtab/') { chrome.tabs.remove(tab.id); return; } // 处理404页面(需要注入脚本检测页面内容) if (close404) { try { const is404 = await chrome.scripting.executeScript({ target: { tabId: tab.id }, func: () => { // 简单检测404的方式,你可以根据实际情况优化,比如匹配更多关键词 return document.title.includes('404') || (document.querySelector('h1')?.textContent || '').includes('404'); } }); if (is404[0].result) { chrome.tabs.remove(tab.id); return; } } catch (e) { // 像chrome://开头的页面没法注入脚本,直接跳过就行 return; } } // 处理URL规则匹配(把通配符转成正则表达式来匹配) for (const rule of blockRules) { // 把*换成.*,?换成.,这样就能支持通配符了 const regexPattern = rule.replace(/\*/g, '.*').replace(/\?/g, '.'); const regex = new RegExp(`^${regexPattern}$`, 'i'); // i表示忽略大小写 if (regex.test(tab.url)) { chrome.tabs.remove(tab.id); return; } } }
3. 弹窗页面(popup.html)
做个简单的界面,方便你添加规则和设置开关:
<!DOCTYPE html> <html> <head> <style> body { width: 300px; padding: 15px; font-family: Arial, sans-serif; } .rule-item { display: flex; align-items: center; gap: 8px; margin: 10px 0; } button { padding: 5px 10px; cursor: pointer; border: none; border-radius: 4px; background: #007bff; color: white; } button.delete-btn { background: #dc3545; } input[type="text"] { flex: 1; padding: 6px; border: 1px solid #ddd; border-radius: 4px; } </style> </head> <body> <h3>自动关闭标签设置</h3> <div style="margin-bottom: 10px;"> <label><input type="checkbox" id="closeBlank"> 自动关闭空白页</label> </div> <div style="margin-bottom: 15px;"> <label><input type="checkbox" id="close404"> 自动关闭404页面</label> </div> <hr> <h4>添加拦截规则(支持通配符,比如*.example.com/ad*)</h4> <div class="rule-item"> <input type="text" id="ruleInput" placeholder="输入URL规则"> <button id="addRule">添加</button> </div> <button id="addCurrentTab" style="margin-bottom: 15px;">添加当前页面</button> <div id="rulesList"></div> <script src="popup.js"></script> </body> </html>
4. 弹窗交互脚本(popup.js)
实现规则的添加、删除和设置的保存:
// 页面加载时初始化所有内容 document.addEventListener('DOMContentLoaded', async () => { // 加载开关设置 const { closeBlank = true, close404 = true } = await chrome.storage.local.get(['closeBlank', 'close404']); document.getElementById('closeBlank').checked = closeBlank; document.getElementById('close404').checked = close404; // 保存开关状态到本地存储 document.getElementById('closeBlank').addEventListener('change', async (e) => { await chrome.storage.local.set({ closeBlank: e.target.checked }); }); document.getElementById('close404').addEventListener('change', async (e) => { await chrome.storage.local.set({ close404: e.target.checked }); }); // 加载已有的规则列表 await loadRules(); // 点击添加规则按钮 document.getElementById('addRule').addEventListener('click', async () => { const input = document.getElementById('ruleInput'); const rule = input.value.trim(); if (rule) { const { blockRules = [] } = await chrome.storage.local.get('blockRules'); // 避免重复添加相同规则 if (!blockRules.includes(rule)) { blockRules.push(rule); await chrome.storage.local.set({ blockRules }); input.value = ''; await loadRules(); } } }); // 点击添加当前页面按钮 document.getElementById('addCurrentTab').addEventListener('click', async () => { // 获取当前激活的标签页 const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); if (tab && tab.url && !tab.url.startsWith('chrome://')) { const { blockRules = [] } = await chrome.storage.local.get('blockRules'); if (!blockRules.includes(tab.url)) { blockRules.push(tab.url); await chrome.storage.local.set({ blockRules }); await loadRules(); } } }); }); // 加载并显示规则列表,带删除按钮 async function loadRules() { const { blockRules = [] } = await chrome.storage.local.get('blockRules'); const list = document.getElementById('rulesList'); list.innerHTML = ''; blockRules.forEach((rule, index) => { const div = document.createElement('div'); div.className = 'rule-item'; div.textContent = rule; const deleteBtn = document.createElement('button'); deleteBtn.className = 'delete-btn'; deleteBtn.textContent = '删除'; deleteBtn.addEventListener('click', async () => { blockRules.splice(index, 1); await chrome.storage.local.set({ blockRules }); await loadRules(); }); div.appendChild(deleteBtn); list.appendChild(div); }); }
5. 测试和打包插件
- 打开Chrome,输入
chrome://extensions/进入扩展程序页面 - 开启右上角的「开发者模式」
- 点击「加载已解压的扩展程序」,选择你创建的
AutoAdTabKiller文件夹 - 测试功能:打开广告标签、空白页、404页面,看是否自动关闭;用弹窗添加规则、添加当前页面,验证是否生效
- 要是想打包成crx文件发布,点击「打包扩展程序」,按提示操作就行
6. 可选优化方向
- 支持正则表达式:可以在弹窗里加个选项,让用户选择用通配符还是正则规则
- 预设规则列表:在
background.js里预设一些常见的广告域名,比如['*.adnetwork.com/*', '*.doubleclick.net/*'],首次安装时自动导入到本地存储 - 延迟关闭:给用户设置延迟时间(比如3秒),避免误关有用的页面
- 白名单功能:添加白名单规则,匹配到的页面不会被关闭
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

