Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Tab Dedup - 重复标签页检测",
"version": "4.1.0",
"version": "4.3.1",
"description": "检测并关闭重复标签页,支持批量操作、预览和智能分组",
"permissions": [
"tabs",
Expand Down
22 changes: 14 additions & 8 deletions firefox/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '完全相同的页面' },
Expand Down Expand Up @@ -44,17 +42,20 @@ function loadTabs() {
document.getElementById('tl').innerHTML = '<div class=empty-state><div class=icon>⚠️</div><div class=msg>连接 background 超时,请重新加载插件</div></div>';
}, 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 = '<div class=empty-state><div class=icon>⚠️</div><div class=msg>获取失败: ' + (browser.runtime.lastError ? browser.runtime.lastError.message : '无响应') + '</div></div>';
console.log('[Tab Dedup] 收到响应:', resp);
if (!resp || !resp.tabs) {
document.getElementById('tl').innerHTML = '<div class=empty-state><div class=icon>⚠️</div><div class=msg>获取失败: 无响应</div></div>';
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 = '<div class=empty-state><div class=icon>⚠️</div><div class=msg>获取失败: ' + esc(err.message) + '</div></div>';
});
}

Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 || '无法预览';
});
}

Expand Down
85 changes: 85 additions & 0 deletions tools/validate-extension.js
Original file line number Diff line number Diff line change
@@ -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');
}
Loading