|
1 | 1 | #!/bin/bash |
2 | | - |
3 | | -# PixTab 打包脚本 |
4 | | -# 使用 Node.js 处理 JSON,避免 sed 产生的语法错误 |
5 | | - |
| 2 | +# PixTab 一键打包脚本 (macOS/Linux) |
6 | 3 | set -e |
7 | 4 |
|
8 | | -# 切换到项目根目录 |
9 | 5 | cd "$(dirname "$0")/.." |
10 | 6 |
|
11 | 7 | echo "🔨 开始打包 PixTab..." |
12 | 8 |
|
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 | +# 读取版本号 |
20 | 10 | VERSION=$(node -e "console.log(require('./manifest.json').version)") |
21 | 11 | echo "📋 版本号: $VERSION" |
22 | 12 |
|
23 | | -# 清空并重建 dist 目录 |
24 | 13 | rm -rf dist |
25 | 14 | mkdir -p dist |
26 | 15 |
|
27 | | -# ------------------------------------------------------------------ |
28 | | -# 📦 1. 打包 Chrome/Edge 版本 |
29 | | -# ------------------------------------------------------------------ |
| 16 | +# 打包 Chrome/Edge 版本 |
30 | 17 | echo "📦 打包 Chrome/Edge 版本..." |
31 | 18 | zip -r "dist/pixtab-${VERSION}-chrome.zip" manifest.json LICENSE index.html options.html style.css _locales icons src -x "*.git*" -x "*.DS_Store" |
32 | 19 |
|
33 | | -# ------------------------------------------------------------------ |
34 | | -# 📦 2. 打包 Firefox 版本 |
35 | | -# ------------------------------------------------------------------ |
| 20 | +# 打包 Firefox 版本(临时修改 manifest) |
36 | 21 | echo "📦 打包 Firefox 版本..." |
37 | 22 | cp manifest.json manifest.backup.json |
38 | 23 |
|
39 | | -# --- 关键修改:使用 Node.js 脚本修改 manifest --- |
40 | | -# 这段脚本会自动处理逗号、格式和字段替换,100% 安全 |
| 24 | +# 用 Node.js 处理 manifest 字段,兼容 Firefox |
41 | 25 | node -e " |
42 | 26 | const fs = require('fs'); |
43 | 27 | const manifestPath = 'manifest.json'; |
44 | 28 | const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); |
45 | | -
|
46 | | -// 1. 修改 background: 把 service_worker 换成 scripts |
47 | 29 | if (manifest.background && manifest.background.service_worker) { |
48 | 30 | const swPath = manifest.background.service_worker; |
49 | 31 | manifest.background.scripts = [swPath]; |
50 | 32 | delete manifest.background.service_worker; |
51 | | - // 移除 type: module(Firefox 不支持) |
52 | 33 | if (manifest.background.type) delete manifest.background.type; |
53 | 34 | } |
54 | | -
|
55 | | -// 2. 转换 action.default_icon(如果是对象)为单字符串(优先 48 -> 32 -> 16 -> 128) |
56 | 35 | if (manifest.action && manifest.action.default_icon && typeof manifest.action.default_icon === 'object') { |
57 | 36 | const sizes = ['48', '32', '16', '128']; |
58 | 37 | let selected = null; |
59 | 38 | for (const s of sizes) { if (manifest.action.default_icon[s]) { selected = manifest.action.default_icon[s]; break; } } |
60 | 39 | if (!selected) selected = 'icons/icon-48.png'; |
61 | 40 | manifest.action.default_icon = selected; |
62 | 41 | } |
63 | | -
|
64 | | -// 3. 确保 browser_specific_settings.gecko 的字段存在并合法,解决 Firefox 警告 |
65 | 42 | if (!manifest.browser_specific_settings) manifest.browser_specific_settings = {}; |
66 | 43 | 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) |
68 | 44 | manifest.browser_specific_settings.gecko.strict_min_version = '142.0'; |
69 | | -// gecko_android: set explicit Android min version to satisfy Android-specific warnings |
70 | 45 | manifest.browser_specific_settings.gecko_android = { strict_min_version: '142.0' }; |
71 | | -// data_collection_permissions: requires 'none' entry in required |
72 | 46 | manifest.browser_specific_settings.gecko.data_collection_permissions = manifest.browser_specific_settings.gecko.data_collection_permissions || { collects: false, required: ['none'], optional: [] }; |
73 | | -
|
74 | 47 | fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2)); |
75 | 48 | " |
76 | | -# ------------------------------------------------------ |
77 | 49 |
|
78 | 50 | zip -r "dist/pixtab-${VERSION}-firefox.xpi" manifest.json LICENSE index.html options.html style.css _locales icons src -x "*.git*" -x "*.DS_Store" |
79 | 51 |
|
80 | | -# 恢复原始 manifest |
81 | 52 | mv manifest.backup.json manifest.json |
82 | 53 |
|
83 | 54 | echo "" |
|
0 commit comments