Skip to content

Commit dec5f9b

Browse files
committed
Handle service worker sleep errors in messaging
Silently ignore 'Could not establish connection' errors caused by Manifest V3 service worker sleep in runtime messaging across background, newtab, and options scripts. Bump extension version to 1.2 and update build script for improved packaging and manifest handling for Firefox compatibility.
1 parent 34dc8e1 commit dec5f9b

File tree

7 files changed

+33
-41
lines changed

7 files changed

+33
-41
lines changed

build/build.sh

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,54 @@
11
#!/bin/bash
2-
3-
# PixTab 打包脚本
4-
# 使用 Node.js 处理 JSON,避免 sed 产生的语法错误
5-
2+
# PixTab 一键打包脚本 (macOS/Linux)
63
set -e
74

8-
# 切换到项目根目录
95
cd "$(dirname "$0")/.."
106

117
echo "🔨 开始打包 PixTab..."
128

13-
# 检查是否安装了 node
14-
if ! command -v node &> /dev/null; then
15-
echo "❌ 错误: 需要安装 Node.js 才能运行此脚本"
16-
exit 1
17-
fi
18-
19-
# 从 manifest.json 读取版本号 (使用 node 读取更稳健)
9+
# 读取版本号
2010
VERSION=$(node -e "console.log(require('./manifest.json').version)")
2111
echo "📋 版本号: $VERSION"
2212

23-
# 清空并重建 dist 目录
2413
rm -rf dist
2514
mkdir -p dist
2615

27-
# ------------------------------------------------------------------
28-
# 📦 1. 打包 Chrome/Edge 版本
29-
# ------------------------------------------------------------------
16+
# 打包 Chrome/Edge 版本
3017
echo "📦 打包 Chrome/Edge 版本..."
3118
zip -r "dist/pixtab-${VERSION}-chrome.zip" manifest.json LICENSE index.html options.html style.css _locales icons src -x "*.git*" -x "*.DS_Store"
3219

33-
# ------------------------------------------------------------------
34-
# 📦 2. 打包 Firefox 版本
35-
# ------------------------------------------------------------------
20+
# 打包 Firefox 版本(临时修改 manifest)
3621
echo "📦 打包 Firefox 版本..."
3722
cp manifest.json manifest.backup.json
3823

39-
# --- 关键修改:使用 Node.js 脚本修改 manifest ---
40-
# 这段脚本会自动处理逗号、格式和字段替换,100% 安全
24+
# 用 Node.js 处理 manifest 字段,兼容 Firefox
4125
node -e "
4226
const fs = require('fs');
4327
const manifestPath = 'manifest.json';
4428
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
45-
46-
// 1. 修改 background: 把 service_worker 换成 scripts
4729
if (manifest.background && manifest.background.service_worker) {
4830
const swPath = manifest.background.service_worker;
4931
manifest.background.scripts = [swPath];
5032
delete manifest.background.service_worker;
51-
// 移除 type: module(Firefox 不支持)
5233
if (manifest.background.type) delete manifest.background.type;
5334
}
54-
55-
// 2. 转换 action.default_icon(如果是对象)为单字符串(优先 48 -> 32 -> 16 -> 128)
5635
if (manifest.action && manifest.action.default_icon && typeof manifest.action.default_icon === 'object') {
5736
const sizes = ['48', '32', '16', '128'];
5837
let selected = null;
5938
for (const s of sizes) { if (manifest.action.default_icon[s]) { selected = manifest.action.default_icon[s]; break; } }
6039
if (!selected) selected = 'icons/icon-48.png';
6140
manifest.action.default_icon = selected;
6241
}
63-
64-
// 3. 确保 browser_specific_settings.gecko 的字段存在并合法,解决 Firefox 警告
6542
if (!manifest.browser_specific_settings) manifest.browser_specific_settings = {};
6643
if (!manifest.browser_specific_settings.gecko) manifest.browser_specific_settings.gecko = {};
67-
// gecko.strict_min_version: set to a version that supports data_collection_permissions (>=140) and options_page (>=126)
6844
manifest.browser_specific_settings.gecko.strict_min_version = '142.0';
69-
// gecko_android: set explicit Android min version to satisfy Android-specific warnings
7045
manifest.browser_specific_settings.gecko_android = { strict_min_version: '142.0' };
71-
// data_collection_permissions: requires 'none' entry in required
7246
manifest.browser_specific_settings.gecko.data_collection_permissions = manifest.browser_specific_settings.gecko.data_collection_permissions || { collects: false, required: ['none'], optional: [] };
73-
7447
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
7548
"
76-
# ------------------------------------------------------
7749

7850
zip -r "dist/pixtab-${VERSION}-firefox.xpi" manifest.json LICENSE index.html options.html style.css _locales icons src -x "*.git*" -x "*.DS_Store"
7951

80-
# 恢复原始 manifest
8152
mv manifest.backup.json manifest.json
8253

8354
echo ""

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "__MSG_extensionName__",
33
"description": "__MSG_extensionDescription__",
4-
"version": "1.1",
4+
"version": "1.2",
55
"manifest_version": 3,
66
"default_locale": "en",
77
"browser_specific_settings": {

src/background/runtime.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,22 @@ browserAPI.runtime.onMessage.addListener(function (
700700
try {
701701
browserAPI.runtime.sendMessage({ action: 'artworkLoadSucceeded' });
702702
} catch (e) {
703-
dbg('Failed to send artworkLoadSucceeded message', e);
703+
if (e && e.message && e.message.includes('Could not establish connection')) {
704+
// 静默忽略 Manifest V3 service worker 休眠导致的错误
705+
} else {
706+
dbg('Background sendMessage error:', e);
707+
}
704708
}
705709
} else {
706710
// Notify UI that artwork failed to load (keep spinner, but indicate failure)
707711
try {
708712
browserAPI.runtime.sendMessage({ action: 'artworkLoadFailed' });
709713
} catch (e) {
710-
dbg('Failed to send artworkLoadFailed message', e);
714+
if (e && e.message && e.message.includes('Could not establish connection')) {
715+
// 静默忽略 Manifest V3 service worker 休眠导致的错误
716+
} else {
717+
dbg('Background sendMessage error:', e);
718+
}
711719
}
712720
sendResponse(null);
713721
}

src/newtab/app.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,15 @@ import browserAPI from "../shared/browser-polyfill.js";
264264
setRefreshing(true);
265265
showSpinnerIfBlank();
266266
browserAPI.runtime.sendMessage({ action: "requestArtwork" }, (res) => {
267-
if (browserAPI.runtime.lastError) {
268-
console.warn("Context invalidated, message could not be processed:", browserAPI.runtime.lastError.message);
267+
const lastError = browserAPI.runtime.lastError;
268+
if (lastError) {
269+
if (lastError.message && lastError.message.includes('Could not establish connection')) {
270+
// 静默忽略 Manifest V3 service worker 休眠导致的错误
271+
setRefreshing(false);
272+
isRequestInProgress = false;
273+
return;
274+
}
275+
console.warn("Extension messaging error:", lastError.message);
269276
setRefreshing(false);
270277
isRequestInProgress = false;
271278
return;

src/options/panel.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ const saveOptions = () => {
5353
);
5454

5555
browserAPI.runtime.sendMessage({ action: "refreshPreferences" }, (response) => {
56-
let lastError = browserAPI.runtime.lastError;
56+
const lastError = browserAPI.runtime.lastError;
5757
if (lastError) {
58-
console.log(lastError.message);
58+
// 仅在调试时输出,生产环境可静默处理
59+
if (lastError.message && lastError.message.includes('Could not establish connection')) {
60+
// 静默忽略 Manifest V3 service worker 休眠导致的错误
61+
return;
62+
}
63+
console.warn('Extension messaging error:', lastError.message);
5964
return;
6065
}
66+
// 可根据需要处理 response
6167
});
6268
};
6369

0 commit comments

Comments
 (0)