From 7820bfab0249bbe3325fb1046cacab5a2bf6061a Mon Sep 17 00:00:00 2001 From: wszgr123456 <2939183730@qq.com> Date: Thu, 2 Jul 2026 20:54:45 +0800 Subject: [PATCH 1/2] Add files via upload --- json_minifier.js | 100 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 json_minifier.js diff --git a/json_minifier.js b/json_minifier.js new file mode 100644 index 00000000..919205e3 --- /dev/null +++ b/json_minifier.js @@ -0,0 +1,100 @@ +Plugin.register('json_minifier', { + title: 'JSON压缩', + author: 'XXQY', + description: '导出JSON时自动极限压缩,移除所有空白和冗余', + icon: 'fa-compress', + version: '1.0.0', + min_version: '4.0.0', + variant: 'both', + + onload() { + // 保存原始函数 + this._originalExport = Blockbench.export; + this._originalCodecs = {}; + + // 拦截 Blockbench.export + Blockbench.export = (options) => { + if (options && options.content && typeof options.content === 'string') { + let t = options.content.trim(); + if (t.startsWith('{') || t.startsWith('[')) { + try { + let obj = JSON.parse(t); + options.content = minifyJSON(obj); + } catch(e) {} + } + } + return this._originalExport.call(Blockbench, options); + }; + for (let id in Codecs) { + let codec = Codecs[id]; + if (codec && typeof codec.compile === 'function') { + this._originalCodecs[id] = codec.compile.bind(codec); + (function(originalCompile, codecRef) { + codecRef.compile = function(...args) { + let result = originalCompile.apply(this, args); + if (result && typeof result === 'object') { + return minifyJSON(result); + } + if (typeof result === 'string') { + let t = result.trim(); + if (t.startsWith('{') || t.startsWith('[')) { + try { + return minifyJSON(JSON.parse(t)); + } catch(e) {} + } + } + return result; + }; + })(this._originalCodecs[id], codec); + } + } + + Blockbench.showQuickMessage('JSON压缩插件已加载 - XXQY'); + }, + + onunload() { + if (this._originalExport) { + Blockbench.export = this._originalExport; + } + for (let id in this._originalCodecs) { + if (Codecs[id]) { + Codecs[id].compile = this._originalCodecs[id]; + } + } + Blockbench.showQuickMessage('JSON压缩插件已卸载'); + } +}); + +function minifyJSON(obj) { + function walk(value) { + if (typeof value === 'number') { + if (value === 0) return 0; + if (value === Math.floor(value)) return Math.floor(value); + let str = value.toString(); + if (str.indexOf('.') !== -1) { + str = str.replace(/\.?0+$/, ''); + let num = parseFloat(str); + if (!isNaN(num)) return num; + } + return value; + } + if (Array.isArray(value)) { + return value.map(walk); + } + if (value !== null && typeof value === 'object') { + let sorted = {}; + Object.keys(value).sort().forEach(function(k) { + sorted[k] = walk(value[k]); + }); + return sorted; + } + return value; + } + + try { + let processed = walk(obj); + return JSON.stringify(processed); + } catch(e) { + return JSON.stringify(obj); + } +} From 1b05b60d63d596d1ca2f78b485cfaf92df637c1c Mon Sep 17 00:00:00 2001 From: wszgr123456 <2939183730@qq.com> Date: Thu, 2 Jul 2026 20:57:57 +0800 Subject: [PATCH 2/2] Add files via upload --- plugins/json_minifier.js | 100 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 plugins/json_minifier.js diff --git a/plugins/json_minifier.js b/plugins/json_minifier.js new file mode 100644 index 00000000..919205e3 --- /dev/null +++ b/plugins/json_minifier.js @@ -0,0 +1,100 @@ +Plugin.register('json_minifier', { + title: 'JSON压缩', + author: 'XXQY', + description: '导出JSON时自动极限压缩,移除所有空白和冗余', + icon: 'fa-compress', + version: '1.0.0', + min_version: '4.0.0', + variant: 'both', + + onload() { + // 保存原始函数 + this._originalExport = Blockbench.export; + this._originalCodecs = {}; + + // 拦截 Blockbench.export + Blockbench.export = (options) => { + if (options && options.content && typeof options.content === 'string') { + let t = options.content.trim(); + if (t.startsWith('{') || t.startsWith('[')) { + try { + let obj = JSON.parse(t); + options.content = minifyJSON(obj); + } catch(e) {} + } + } + return this._originalExport.call(Blockbench, options); + }; + for (let id in Codecs) { + let codec = Codecs[id]; + if (codec && typeof codec.compile === 'function') { + this._originalCodecs[id] = codec.compile.bind(codec); + (function(originalCompile, codecRef) { + codecRef.compile = function(...args) { + let result = originalCompile.apply(this, args); + if (result && typeof result === 'object') { + return minifyJSON(result); + } + if (typeof result === 'string') { + let t = result.trim(); + if (t.startsWith('{') || t.startsWith('[')) { + try { + return minifyJSON(JSON.parse(t)); + } catch(e) {} + } + } + return result; + }; + })(this._originalCodecs[id], codec); + } + } + + Blockbench.showQuickMessage('JSON压缩插件已加载 - XXQY'); + }, + + onunload() { + if (this._originalExport) { + Blockbench.export = this._originalExport; + } + for (let id in this._originalCodecs) { + if (Codecs[id]) { + Codecs[id].compile = this._originalCodecs[id]; + } + } + Blockbench.showQuickMessage('JSON压缩插件已卸载'); + } +}); + +function minifyJSON(obj) { + function walk(value) { + if (typeof value === 'number') { + if (value === 0) return 0; + if (value === Math.floor(value)) return Math.floor(value); + let str = value.toString(); + if (str.indexOf('.') !== -1) { + str = str.replace(/\.?0+$/, ''); + let num = parseFloat(str); + if (!isNaN(num)) return num; + } + return value; + } + if (Array.isArray(value)) { + return value.map(walk); + } + if (value !== null && typeof value === 'object') { + let sorted = {}; + Object.keys(value).sort().forEach(function(k) { + sorted[k] = walk(value[k]); + }); + return sorted; + } + return value; + } + + try { + let processed = walk(obj); + return JSON.stringify(processed); + } catch(e) { + return JSON.stringify(obj); + } +}