From e6a803ca1a1d59a453d03ea22309037325e05506 Mon Sep 17 00:00:00 2001 From: Mby159 Date: Mon, 15 Jun 2026 01:10:40 +0800 Subject: [PATCH] fix: clean Firefox popup messaging and add static validation --- .github/workflows/ci.yml | 31 ++++++++++++++ chrome/manifest.json | 2 +- firefox/popup.js | 22 ++++++---- tools/validate-extension.js | 85 +++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 tools/validate-extension.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a8ff515 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,31 @@ +name: ci + +on: + push: + paths: + - ".github/workflows/ci.yml" + - "chrome/**" + - "firefox/**" + - "tools/**" + - "test.js" + pull_request: + paths: + - ".github/workflows/ci.yml" + - "chrome/**" + - "firefox/**" + - "tools/**" + - "test.js" + +jobs: + static-validation: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: "22" + + - name: Validate extension files + run: node tools/validate-extension.js diff --git a/chrome/manifest.json b/chrome/manifest.json index 42b126b..9f7c27a 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Tab Dedup - 重复标签页检测", - "version": "4.1.0", + "version": "4.3.1", "description": "检测并关闭重复标签页,支持批量操作、预览和智能分组", "permissions": [ "tabs", diff --git a/firefox/popup.js b/firefox/popup.js index 3e8aca2..0780903 100644 --- a/firefox/popup.js +++ b/firefox/popup.js @@ -2,8 +2,6 @@ (function() { 'use strict'; -alert('[Tab Dedup] popup JS 已启动'); - var allTabs = [], tabGroups = [], dupGroups = [], currentGroupBy = 'full'; var GROUPS = [ { id: 'full', label: '完整 URL', desc: '完全相同的页面' }, @@ -44,17 +42,20 @@ function loadTabs() { document.getElementById('tl').innerHTML = '
⚠️
连接 background 超时,请重新加载插件
'; }, 5000); - browser.runtime.sendMessage({ type: 'refresh' }, function(resp) { + browser.runtime.sendMessage({ type: 'refresh' }).then(function(resp) { clearTimeout(timeout); - console.log('[Tab Dedup] 收到响应:', resp, 'lastError:', browser.runtime.lastError); - if (browser.runtime.lastError || !resp || !resp.tabs) { - document.getElementById('tl').innerHTML = '
⚠️
获取失败: ' + (browser.runtime.lastError ? browser.runtime.lastError.message : '无响应') + '
'; + console.log('[Tab Dedup] 收到响应:', resp); + if (!resp || !resp.tabs) { + document.getElementById('tl').innerHTML = '
⚠️
获取失败: 无响应
'; return; } currentGroupBy = resp.groupBy || 'full'; allTabs = resp.tabs.filter(function(t) { return t.url && !t.url.startsWith('about:') && !t.url.startsWith('moz-extension://'); }); buildGroups(); render(); + }).catch(function(err) { + clearTimeout(timeout); + document.getElementById('tl').innerHTML = '
⚠️
获取失败: ' + esc(err.message) + '
'; }); } @@ -72,7 +73,7 @@ function buildGroups() { function changeGroupBy(gb) { currentGroupBy = gb; - browser.runtime.sendMessage({ type: 'setGroupBy', groupBy: gb }, function() {}); + browser.runtime.sendMessage({ type: 'setGroupBy', groupBy: gb }).catch(function() {}); buildGroups(); render(); } @@ -109,7 +110,7 @@ function showPreview(tabId, title, url, rect) { p.style.cssText = 'display:block;opacity:1;top:' + top + 'px;left:' + left + 'px'; var done = false; var timer = setTimeout(function() { if (!done) { done = true; se.textContent = '预览超时'; } }, 6000); - browser.runtime.sendMessage({ type: 'captureTab', tabId: tabId }, function(resp) { + browser.runtime.sendMessage({ type: 'captureTab', tabId: tabId }).then(function(resp) { clearTimeout(timer); if (done) return; done = true; @@ -120,6 +121,11 @@ function showPreview(tabId, title, url, rect) { } else { se.textContent = (resp && resp.error) || '无法预览'; } + }).catch(function(err) { + clearTimeout(timer); + if (done) return; + done = true; + se.textContent = err.message || '无法预览'; }); } diff --git a/tools/validate-extension.js b/tools/validate-extension.js new file mode 100644 index 0000000..c2393b7 --- /dev/null +++ b/tools/validate-extension.js @@ -0,0 +1,85 @@ +#!/usr/bin/env node +const fs = require('fs'); +const path = require('path'); +const { spawnSync } = require('child_process'); + +const root = path.resolve(__dirname, '..'); +const jsFiles = [ + 'chrome/background.js', + 'chrome/popup.js', + 'firefox/background.js', + 'firefox/popup.js', + 'test.js', +]; +const manifestFiles = ['chrome/manifest.json', 'firefox/manifest.json']; + +function fail(message) { + console.error(`FAIL ${message}`); + process.exitCode = 1; +} + +function read(file) { + return fs.readFileSync(path.join(root, file), 'utf8'); +} + +function readJson(file) { + try { + return JSON.parse(read(file)); + } catch (error) { + fail(`${file}: invalid JSON: ${error.message}`); + return null; + } +} + +for (const file of jsFiles) { + const result = spawnSync(process.execPath, ['--check', path.join(root, file)], { + cwd: root, + encoding: 'utf8', + }); + if (result.status !== 0) { + fail(`${file}: JavaScript syntax check failed\n${result.stderr || result.stdout}`); + } +} + +const chromeManifest = readJson('chrome/manifest.json'); +const firefoxManifest = readJson('firefox/manifest.json'); + +for (const file of manifestFiles) { + const manifest = readJson(file); + if (!manifest) continue; + const base = path.dirname(file); + for (const [size, iconPath] of Object.entries(manifest.icons || {})) { + if (!fs.existsSync(path.join(root, base, iconPath))) { + fail(`${file}: missing icon ${size}: ${iconPath}`); + } + } +} + +if (chromeManifest && chromeManifest.manifest_version !== 3) { + fail('chrome/manifest.json should be Manifest V3'); +} +if (firefoxManifest && firefoxManifest.manifest_version !== 2) { + fail('firefox/manifest.json should be Manifest V2'); +} +if (chromeManifest && firefoxManifest && chromeManifest.version !== firefoxManifest.version) { + fail(`manifest versions differ: Chrome=${chromeManifest.version}, Firefox=${firefoxManifest.version}`); +} + +const firefoxPopup = read('firefox/popup.js'); +if (firefoxPopup.includes('alert(')) { + fail('firefox/popup.js should not contain alert() debug popups'); +} +if (/browser\.runtime\.sendMessage\([^\n]+,\s*function/.test(firefoxPopup)) { + fail('firefox/popup.js should use Promise-style browser.runtime.sendMessage'); +} + +const result = spawnSync(process.execPath, ['test.js'], { cwd: root, encoding: 'utf8' }); +process.stdout.write(result.stdout || ''); +process.stderr.write(result.stderr || ''); +if (result.status !== 0) { + fail('test.js failed'); +} + +if (!process.exitCode) { + console.log('extension static validation passed'); +}