给 V2EX 使用汉字标准格式……其实很简单!

2021-05-14 18:02:11 +08:00
 Arthur2e5

我糊了个 userscript 用来把盘古之白之类的事交给 han.css 做,安装“greasemonkey”就能使用:

// ==UserScript==
// @name         Han.css on V2EX
// @namespace    https://artoria2e5.moe
// @version      0.1
// @description  what description?
// @author       You
// @match        https://v2ex.com/*
// @match        https://www.v2ex.com/*
// @icon         https://www.google.com/s2/favicons?domain=https://v2ex.com/
// @grant        unsafeWindow
// ==/UserScript==

$("head").append(
  '<link rel="stylesheet" media="all" href="//cdnjs.cloudflare.com/ajax/libs/Han/3.3.0/han.min.css">'
)
await $.getScript("//cdnjs.cloudflare.com/ajax/libs/Han/3.3.0/han.min.js")

// Undo the terrible space insertion
// We know it incorrectly includes punctuations. I *guess* this is what they are using... (intentionally bad and BMP-only)
const CJK_BMP =
  "[\\u2E80-\\u303F" +
  "\\u31c0-\\u33ff" +
  "\\u4e00-\\u9fff" +
  "\\uf900-\\ufaff" +
  "\\ufe30-\\ufe4f" +
  "\\uff00-\\uffef]"
const ASCII = "[\x21-\x7e]"
const NEIGHBOR_REGEX = new RegExp(
  `(?<c1>${CJK_BMP}) (?<a1>${ASCII})( (?<c1x>${CJK_BMP}))?|(?<a2>${ASCII}) (?<c2>${CJK_BMP})( (?<a2x>{ASCII}))?`,
  "g"
)

function iterate_node(node, operation, predicate) {
  if (node.nodeType === Node.TEXT_NODE) {
    node.data = operation(node.data)
  } else if (node.nodeType === Node.ELEMENT_NODE && predicate(node)) {
    node.childNodes.forEach((n) => iterate_node(n, operation, predicate))
  }
}
document.querySelectorAll(".reply_content, .topic_content").forEach((e) => {
  iterate_node(
    e,
    (s) => s.replace(NEIGHBOR_REGEX, "$<c1>$<a1>$<c1x>$<a2>$<c2>$<a2x>"),
    (e) => e.name != "pre"
  )
  Han(e).render()
})

(我本来是用来处理 V2EX 在标点周围错误插入的盘古之白的。不过看了一下最近的贴似乎是修了,老帖还是坏的。)我懒得把这个脚本放进任何有版本控制的地方。

1536 次点击
所在节点    V2EX
1 条回复
orzfly
2021-05-14 18:24:54 +08:00
$("head").append => $("head").prepend

不然 desktop.css 里覆盖的例如 h1 的 font-size / magin 就无效了,显得比较怪

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/776988

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX