Ver Fonte

调整代码美化工具,增加worker file支持;同时调整各浏览器的manifest.json打包逻辑

zxlie há 4 meses atrás
pai
commit
3349682bc3

+ 1 - 1
apps/chrome.json

@@ -1,7 +1,7 @@
 {
   "name": "FeHelper(前端助手)-Dev",
   "short_name": "FeHelper",
-  "version": "2025.05.2014",
+  "version": "2025.5.2014",
   "manifest_version": 3,
   "description": "JSON自动格式化、手动格式化,支持排序、解码、下载等,更多功能可在配置页按需安装!",
   "icons": {

+ 2 - 199
apps/code-beautify/beautify-css.js

@@ -58,8 +58,8 @@
     function css_beautify(source_text, options, callback) {
         "use strict";
 
-        // 用webwork的方式来进行格式化,效率更高
-        let worker = new Worker(URL.createObjectURL(new Blob(["(" + beautifyWebWorker.toString() + ")()"], {type: 'text/javascript'})));
+        // 用独立worker文件的方式来进行格式化,兼容Firefox等浏览器
+        let worker = new Worker('./beautify-worker.js');
         worker.onmessage = function (evt) {
             callback && callback(evt.data);
         };
@@ -69,202 +69,5 @@
         });
     }
 
-    function beautifyWebWorker() {
-        function Beautifier(source_text, options) {
-            options = options || {};
-            var indentSize = options.indent_size || 4;
-            var indentCharacter = options.indent_char || ' ';
-
-            // compatibility
-            if (typeof indentSize === "string") {
-                indentSize = parseInt(indentSize, 10);
-            }
-
-
-            // tokenizer
-            var whiteRe = /^\s+$/;
-            var wordRe = /[\w$\-_]/;
-
-            var pos = -1, ch;
-
-            function next() {
-                ch = source_text.charAt(++pos);
-                return ch;
-            }
-
-            function peek() {
-                return source_text.charAt(pos + 1);
-            }
-
-            function eatString(comma) {
-                var start = pos;
-                while (next()) {
-                    if (ch === "\\") {
-                        next();
-                        next();
-                    } else if (ch === comma) {
-                        break;
-                    } else if (ch === "\n") {
-                        break;
-                    }
-                }
-                return source_text.substring(start, pos + 1);
-            }
-
-            function eatWhitespace() {
-                var start = pos;
-                while (whiteRe.test(peek())) {
-                    pos++;
-                }
-                return pos !== start;
-            }
-
-            function skipWhitespace() {
-                var start = pos;
-                do {
-                } while (whiteRe.test(next()));
-                return pos !== start + 1;
-            }
-
-            function eatComment() {
-                var start = pos;
-                next();
-                while (next()) {
-                    if (ch === "*" && peek() === "/") {
-                        pos++;
-                        break;
-                    }
-                }
-
-                return source_text.substring(start, pos + 1);
-            }
-
-
-            function lookBack(str) {
-                return source_text.substring(pos - str.length, pos).toLowerCase() === str;
-            }
-
-            // printer
-            var indentString = source_text.match(/^[\r\n]*[\t ]*/)[0];
-            var singleIndent = Array(indentSize + 1).join(indentCharacter);
-            var indentLevel = 0;
-
-            function indent() {
-                indentLevel++;
-                indentString += singleIndent;
-            }
-
-            function outdent() {
-                indentLevel--;
-                indentString = indentString.slice(0, -indentSize);
-            }
-
-            var print = {};
-            print["{"] = function (ch) {
-                print.singleSpace();
-                output.push(ch);
-                print.newLine();
-            };
-            print["}"] = function (ch) {
-                print.newLine();
-                output.push(ch);
-                print.newLine();
-            };
-
-            print.newLine = function (keepWhitespace) {
-                if (!keepWhitespace) {
-                    while (whiteRe.test(output[output.length - 1])) {
-                        output.pop();
-                    }
-                }
-
-                if (output.length) {
-                    output.push('\n');
-                }
-                if (indentString) {
-                    output.push(indentString);
-                }
-            };
-            print.singleSpace = function () {
-                if (output.length && !whiteRe.test(output[output.length - 1])) {
-                    output.push(' ');
-                }
-            };
-            var output = [];
-            if (indentString) {
-                output.push(indentString);
-            }
-            /*_____________________--------------------_____________________*/
-
-            while (true) {
-                var isAfterSpace = skipWhitespace();
-
-                if (!ch) {
-                    break;
-                }
-
-
-                if (ch === '{') {
-                    indent();
-                    print["{"](ch);
-                } else if (ch === '}') {
-                    outdent();
-                    print["}"](ch);
-                } else if (ch === '"' || ch === '\'') {
-                    output.push(eatString(ch));
-                } else if (ch === ';') {
-                    output.push(ch, '\n', indentString);
-                } else if (ch === '/' && peek() === '*') { // comment
-                    print.newLine();
-                    output.push(eatComment(), "\n", indentString);
-                } else if (ch === '(') { // may be a url
-                    if (lookBack("url")) {
-                        output.push(ch);
-                        eatWhitespace();
-                        if (next()) {
-                            if (ch !== ')' && ch !== '"' && ch !== '\'') {
-                                output.push(eatString(')'));
-                            } else {
-                                pos--;
-                            }
-                        }
-                    } else {
-                        if (isAfterSpace) {
-                            print.singleSpace();
-                        }
-                        output.push(ch);
-                        eatWhitespace();
-                    }
-                } else if (ch === ')') {
-                    output.push(ch);
-                } else if (ch === ',') {
-                    eatWhitespace();
-                    output.push(ch);
-                    print.singleSpace();
-                } else if (ch === ']') {
-                    output.push(ch);
-                } else if (ch === '[' || ch === '=') { // no whitespace before or after
-                    eatWhitespace();
-                    output.push(ch);
-                } else {
-                    if (isAfterSpace) {
-                        print.singleSpace();
-                    }
-
-                    output.push(ch);
-                }
-            }
-
-
-            var sweetCode = output.join('').replace(/[\n ]+$/, '');
-            return sweetCode;
-        }
-
-        self.onmessage = function (evt) {
-            var result = Beautifier(evt.data.source_text, evt.data.options);
-            self.postMessage(result);
-        };
-    }
-
     window.css_beautify = css_beautify;
 }());

+ 186 - 0
apps/code-beautify/beautify-worker.js

@@ -0,0 +1,186 @@
+// beautify-worker.js
+// 独立Web Worker脚本,用于CSS代码美化
+
+self.onmessage = function (evt) {
+    function Beautifier(source_text, options) {
+        options = options || {};
+        var indentSize = options.indent_size || 4;
+        var indentCharacter = options.indent_char || ' ';
+
+        // compatibility
+        if (typeof indentSize === "string") {
+            indentSize = parseInt(indentSize, 10);
+        }
+
+        // tokenizer
+        var whiteRe = /^\s+$/;
+        var wordRe = /[\w$\-_]/;
+
+        var pos = -1, ch;
+
+        function next() {
+            ch = source_text.charAt(++pos);
+            return ch;
+        }
+
+        function peek() {
+            return source_text.charAt(pos + 1);
+        }
+
+        function eatString(comma) {
+            var start = pos;
+            while (next()) {
+                if (ch === "\\") {
+                    next();
+                    next();
+                } else if (ch === comma) {
+                    break;
+                } else if (ch === "\n") {
+                    break;
+                }
+            }
+            return source_text.substring(start, pos + 1);
+        }
+
+        function eatWhitespace() {
+            var start = pos;
+            while (whiteRe.test(peek())) {
+                pos++;
+            }
+            return pos !== start;
+        }
+
+        function skipWhitespace() {
+            var start = pos;
+            do {
+            } while (whiteRe.test(next()));
+            return pos !== start + 1;
+        }
+
+        function eatComment() {
+            var start = pos;
+            next();
+            while (next()) {
+                if (ch === "*" && peek() === "/") {
+                    pos++;
+                    break;
+                }
+            }
+            return source_text.substring(start, pos + 1);
+        }
+
+        function lookBack(str) {
+            return source_text.substring(pos - str.length, pos).toLowerCase() === str;
+        }
+
+        // printer
+        var indentString = source_text.match(/^[\r\n]*[\t ]*/)[0];
+        var singleIndent = Array(indentSize + 1).join(indentCharacter);
+        var indentLevel = 0;
+
+        function indent() {
+            indentLevel++;
+            indentString += singleIndent;
+        }
+
+        function outdent() {
+            indentLevel--;
+            indentString = indentString.slice(0, -indentSize);
+        }
+
+        var print = {};
+        print["{"] = function (ch) {
+            print.singleSpace();
+            output.push(ch);
+            print.newLine();
+        };
+        print["}"] = function (ch) {
+            print.newLine();
+            output.push(ch);
+            print.newLine();
+        };
+
+        print.newLine = function (keepWhitespace) {
+            if (!keepWhitespace) {
+                while (whiteRe.test(output[output.length - 1])) {
+                    output.pop();
+                }
+            }
+            if (output.length) {
+                output.push('\n');
+            }
+            if (indentString) {
+                output.push(indentString);
+            }
+        };
+        print.singleSpace = function () {
+            if (output.length && !whiteRe.test(output[output.length - 1])) {
+                output.push(' ');
+            }
+        };
+        var output = [];
+        if (indentString) {
+            output.push(indentString);
+        }
+        /*_____________________--------------------_____________________*/
+
+        while (true) {
+            var isAfterSpace = skipWhitespace();
+            if (!ch) {
+                break;
+            }
+            if (ch === '{') {
+                indent();
+                print["{"](ch);
+            } else if (ch === '}') {
+                outdent();
+                print["}"](ch);
+            } else if (ch === '"' || ch === '\'') {
+                output.push(eatString(ch));
+            } else if (ch === ';') {
+                output.push(ch, '\n', indentString);
+            } else if (ch === '/' && peek() === '*') { // comment
+                print.newLine();
+                output.push(eatComment(), "\n", indentString);
+            } else if (ch === '(') { // may be a url
+                if (lookBack("url")) {
+                    output.push(ch);
+                    eatWhitespace();
+                    if (next()) {
+                        if (ch !== ')' && ch !== '"' && ch !== '\'') {
+                            output.push(eatString(')'));
+                        } else {
+                            pos--;
+                        }
+                    }
+                } else {
+                    if (isAfterSpace) {
+                        print.singleSpace();
+                    }
+                    output.push(ch);
+                    eatWhitespace();
+                }
+            } else if (ch === ')') {
+                output.push(ch);
+            } else if (ch === ',') {
+                eatWhitespace();
+                output.push(ch);
+                print.singleSpace();
+            } else if (ch === ']') {
+                output.push(ch);
+            } else if (ch === '[' || ch === '=') { // no whitespace before or after
+                eatWhitespace();
+                output.push(ch);
+            } else {
+                if (isAfterSpace) {
+                    print.singleSpace();
+                }
+                output.push(ch);
+            }
+        }
+        var sweetCode = output.join('').replace(/[\n ]+$/, '');
+        return sweetCode;
+    }
+    var result = Beautifier(evt.data.source_text, evt.data.options);
+    self.postMessage(result);
+}; 

+ 2 - 4
apps/edge.json

@@ -1,7 +1,7 @@
 {
   "name": "FeHelper(前端助手)-Dev",
   "short_name": "FeHelper",
-  "version": "2025.05.2014",
+  "version": "2025.5.2014",
   "manifest_version": 3,
   "description": "JSON自动格式化、手动格式化,支持排序、解码、下载等,更多功能可在配置页按需安装!",
   "icons": {
@@ -64,9 +64,7 @@
             "background/tools.js",
 
             "code-beautify/beautify.js",
-            "code-beautify/beautify-css.js",
-
-            "page-timing/timing.js"
+            "code-beautify/beautify-css.js"
         ],
         "matches": ["<all_urls>"]
       }

+ 3 - 5
apps/firefox.json

@@ -20,8 +20,8 @@
   },
   "browser_specific_settings": {
       "gecko": {
-          "id": "fehelper@baidufe.com",
-          "strict_min_version": "99.0"
+          "id": "firefox@fehelper.com",
+          "strict_min_version": "109.0"
       }
   },
   "options_ui": {
@@ -70,9 +70,7 @@
             "background/tools.js",
 
             "code-beautify/beautify.js",
-            "code-beautify/beautify-css.js",
-
-            "page-timing/timing.js"
+            "code-beautify/beautify-css.js"
         ],
         "matches": ["<all_urls>"]
       }

+ 8 - 10
apps/manifest.json

@@ -3,7 +3,7 @@
   "short_name": "FeHelper",
   "version": "2025.5.2014",
   "manifest_version": 3,
-  "description": "FE助手:前端开发必备工具集,涵盖JSON格式化、代码美化与压缩、二维码生成、网页定制、便签笔记等数十种实用功能。界面现代、响应式设计,极致性能优化,国内可用,助力高效开发!",
+  "description": "JSON自动格式化、手动格式化,支持排序、解码、下载等,更多功能可在配置页按需安装!",
   "icons": {
     "16": "static/img/fe-16.png",
     "48": "static/img/fe-48.png",
@@ -15,15 +15,9 @@
     "default_popup": "popup/index.html"
   },
   "background": {
-    "scripts": ["background/background.js"],
+    "service_worker": "background/background.js",
     "type": "module"
   },
-  "browser_specific_settings": {
-      "gecko": {
-          "id": "[email protected]",
-          "strict_min_version": "99.0"
-      }
-  },
   "options_ui": {
     "page": "options/index.html",
     "open_in_tab": true
@@ -96,7 +90,11 @@
     }
   ],
   "content_security_policy": {
-    "extension_pages": "script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self' 'unsafe-inline'; object-src 'self'; connect-src 'self' blob: https://chrome.fehelper.com https://api.siliconflow.cn https://baidufe.com https://www.baidufe.com https://img.shields.io;"
+    "extension_pages": "script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'self'"
   },
+  "update_url": "https://clients2.google.com/service/update2/crx",
   "homepage_url": "https://www.fehelper.com"
-}
+}
+
+
+

+ 1 - 2
gulpfile.js

@@ -138,7 +138,7 @@ function zipPackage(outputRoot = 'output-chrome', cb) {
     if (outputRoot === 'output-firefox') {
         pkgName = 'fehelper.xpi';
     }
-    shell.exec(`cd ${outputRoot}/ && rm -rf ${pkgName} && zip -r ${pkgName} apps/ > /dev/null && cd ../`);
+    shell.exec(`cd ${outputRoot}/apps && rm -rf ../${pkgName} && zip -r ../${pkgName} ./* > /dev/null && cd ../../`);
     let size = fs.statSync(`${outputRoot}/${pkgName}`).size;
     size = pretty(size);
     console.log('\n\n================================================================================');
@@ -350,7 +350,6 @@ function firefoxPreprocess(cb) {
         const toolDir = path.join(destDir, tool);
         if (fs.existsSync(toolDir)) {
             shell.exec(`rm -rf ${toolDir}`);
-            console.log(`已删除不支持的工具: ${tool}`);
         }
     });
     cb();

BIN
output-edge/fehelper.zip


BIN
output-firefox/fehelper.xpi