找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 397|回复: 17

[求助] 修改 htmlFormatter.js 脚本使其支持 UTF-8

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式
htmlFormatter.js 是 EditPlus 的一个脚本,作用是重新格式化(格式美化) HTML 文件,但此文件仅支持 ANSI 的文本。
不知道有没有老大懂 JS 脚本,修改让其支持 UTF-8 的文本。谢谢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
发表于 5 天前 | 显示全部楼层
回复 1# chishingchan

没玩过 editplus,但看过这个脚本才知是在 editplus 编辑器内使用的,可否先用 editplus 将当前 utf-8 编码的 html 文本内容转为简中编码(gbk 或 gb2312 或 gb18030),再调用这个脚本处理就是了...
发表于 5 天前 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-3-11 12:27 编辑

回复 1# chishingchan

用2楼的思路,把 editplus 家里的活儿在外面练了...
以下代码存为 test.bat 运行,与 utf-8 编码源文件和 htmlFormatter.js 同目录,第二行两个自定义变量 %inF% 是utf-8编码的源文件名,%outF% 是简中编码的最终结果文件名

  1. 2>1/* ::
  2. @echo off &chcp 936>nul &setlocal &set "inF=utf-8.html" &set "outF=gbk.html"
  3. cscript /nologo /e:jscript "%~f0" "%inF%" "#tmp$"
  4. type "#tmp$"|cscript /nologo htmlFormatter.js>"%outF%"
  5. del "#tmp$" &pause&exit/b */
  6. v=WSH.arguments, ads=WSH.createObject('adodb.stream');
  7. ads.type=2, ads.charset='utf-8', ads.open(), ads.loadFromfile(v(0)), all=ads.readText(), ads.close();
  8. ads.charset='gbk', ads.open(), ads.writeText(all), ads.savetoFile(v(1),2), ads.close(), WSH.quit();
复制代码
 楼主| 发表于 5 天前 | 显示全部楼层
回复 2# aloha20200628


    在处理 UTF-8 格式的 HTML 文件时,Ctrl + N 新建文件,将 HTML 全文复制粘贴到新建文件中,按 Ctrl + 1 (此工具的快捷键),新建文件内容被格式化,再复制粘贴到原文件中!
我现在就是这样做的,有点麻烦!
 楼主| 发表于 5 天前 | 显示全部楼层
回复 3# aloha20200628


    这个办法也是相等麻烦。我问过 DeepSeek,给的答案都没用!所以来这问问了。
发表于 4 天前 | 显示全部楼层
回复 4# chishingchan
给个需要处理的示例?不知道按你的标准:格式化后怎样算正常
发表于 4 天前 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-3-12 12:53 编辑

回复 5# chishingchan

试试以下的 bat+jscript 代码(存与 *.html 和 htmlFormatter.js 同目录),可将4楼所述的内部操作流程在外部一气呵成,代码将当前目录中 utf-8 编码的全部 *.html 网页文件用 htmlFormatter.js 格式化后,再覆盖 *.html 源文件(已被转为简中编码)。若不要覆盖 *.html 源文件,可将代码第4行删除,并将第3行末尾的 "#.$" 改为所需的结果文件名,如 "%%F.gbk.txt"

  1. 2>1/* ::
  2. @echo off & for /f "delims=" %%F in ('dir /b/a-d *.html') do (
  3.    cscript /nologo /e:jscript "%~f0" "%%F"|cscript /nologo htmlFormatter.js>"#.$"
  4.    move /y "#.$" "%%F">nul
  5. )
  6. pause&exit/b */
  7. v=WSH.arguments, ads=WSH.createObject('adodb.stream');
  8. ads.type=2, ads.charset='utf-8', ads.open(), ads.loadFromfile(v(0)), all=ads.readText(), ads.close();
  9. ads.charset='gbk', ads.open(), ads.writeText(all), ads.position=0, all=ads.readText(), ads.close();
  10. WSH.echo(all), WSH.quit();
复制代码
 楼主| 发表于 4 天前 | 显示全部楼层
回复 6# holley


    内容省略了
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3.    
  4.     <head>
  5.         <meta charset="utf-8" />
  6.         <title>

  7.         </title>
  8.         <style>

  9.                 </style>
  10.     </head>
  11.    
  12.     <body>
  13.         <div>
  14.             <section>
  15.                 <h1>

  16.                                 </h1>
  17.             </section>
  18.             <footer>

  19.             </footer>
  20.         </div>
  21.     </body>

  22. </html>
复制代码
 楼主| 发表于 4 天前 | 显示全部楼层
本帖最后由 chishingchan 于 2026-3-12 13:52 编辑

回复 3# aloha20200628


    一般处理 html 或 htm 都是批量的,麻烦老大再改进一下。例如:处理当前目录(含子目录)的所有 html 或 htm。如果自动识别文件格式就更好 。

最好直接使用 VBS 脚本处理,我现在使用 AI 将 .JS 转换为 .VBS,看看行不行。
发表于 4 天前 | 显示全部楼层
回复 9# chishingchan

以下代码会处理当前目录及其全部子目录中的 *.html 网页文件(用覆盖源文件方式),用本坛第三方下载的 coder.exe 先判断源文件编码类型,coder.exe 也放在当前目录就是了...

  1. 2>1/* ::
  2. @echo off &setlocal
  3. for /f "delims=" %%F in ('dir /b/s/a-d *.html') do (
  4.    (for /f "delims=" %%c in ('coder -s -a gc -f "%%F" ') do if /i "%%c"=="ansi" (
  5.        type "%%F"|cscript /nologo htmlFormatter.js) else if /i "%%c"=="utf-8" (
  6.        cscript /nologo /e:jscript "%~f0" "%%F"|cscript /nologo htmlFormatter.js) )>"#.$"
  7.    move /y "#.$" "%%F">nul
  8. )
  9. pause&exit/b */
  10. v=WSH.arguments, ads=WSH.createObject('adodb.stream');
  11. ads.type=2, ads.charset='utf-8', ads.open, ads.loadFromfile(v(0)), all=ads.readText, ads.close;
  12. ads.charset='gbk', ads.open, ads.writeText(all), ads.position=0, all=ads.readText, ads.close;
  13. WSH.echo(all), WSH.quit();
复制代码
发表于 4 天前 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-3-12 16:03 编辑

回复 9# chishingchan

如果不用 coder.exe,可用 findstr 简单判断网页类型,此法根据网页文件头部字符集属性值判断(要求该属性值须与文件真实编码一致),以下假设被处理网页或是简中编码或是 utf-8 编码,据此修改10楼代码如下(仍采用覆盖源文件方式)

  1. 2>1/* ::
  2. @echo off
  3. for /f "delims=" %%F in ('dir /b/s/a-d *.html') do (
  4.    findstr /ir "<mata .*charset=.*utf-8.*" "%%F">nul
  5.    (if errorlevel 1 (type "%%F"|cscript /nologo htmlFormatter.js) else (cscript /nologo /e:jscript "%~f0" "%%F"|cscript /nologo htmlFormatter.js) )>"#.$"
  6.    move /y "#.$" "%%F">nul )
  7. pause&exit/b */
  8. v=WSH.arguments, ads=WSH.createObject('adodb.stream');
  9. ads.type=2, ads.charset='utf-8', ads.open, ads.loadFromfile(v(0)), all=ads.readText, ads.close;
  10. ads.charset='gbk', ads.open, ads.writeText(all), ads.position=0, all=ads.readText, ads.close;
  11. WSH.echo(all), WSH.quit();
复制代码
 楼主| 发表于 4 天前 | 显示全部楼层
回复 10# aloha20200628


    所有 html 文件都变 0 字节了!
发表于 4 天前 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-3-13 00:07 编辑

回复 12# chishingchan

我用10楼代码已测试过简中编码和Utf-8编码的网页文件,均无问题。以上各版代码中采用 jscript 只是解决文件编码的转换(从utf-8到简中),最终输出是 htmlFormatter.js 的处理结果... 总之,还是先用 '保留源文件' 方式更稳妥吧 》将10楼代码第7行删除,再将第6行末尾的 "#.$" 改为所需的结果文件名,如 "%%F.gbk.txt" ...
 楼主| 发表于 4 天前 | 显示全部楼层
回复 13# aloha20200628


    改了,原文件保留,生成的文件也是 0 字节。
发表于 3 天前 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-3-16 12:19 编辑

回复 14# chishingchan

以下代码没有调用 htmlFormmatr.js 仅实现 utf-8 到简中编码的转换,试试结果,以验证零字节之源?

  1. 2>1/* ::
  2. @echo off &setlocal
  3. for /f "delims=" %%F in ('dir /b/s/a-d *.html') do for /f "delims=" %%c in ('coder -s -a gc -f "%%F" ') do if /i "%%c"=="utf-8" (cscript /nologo /e:jscript "%~f0" "%%F">"%%F.gbk.txt")
  4. pause&exit/b */
  5. v=WSH.arguments, ads=WSH.createObject('adodb.stream');
  6. ads.mode=3, ads.type=2, ads.charset='utf-8', ads.open, ads.loadFromfile(v(0)), all=ads.readText, ads.close;
  7. ads.charset='gbk', ads.open, ads.writeText(all), ads.position=0, all=ads.readText, ads.close;
  8. WSH.echo(all), WSH.quit();
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 19:15 , Processed in 0.018253 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表