V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Arthur2e5
V2EX  ›  V2EX

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

  •  
  •   Arthur2e5 · 2021-05-14 18:02:11 +08:00 · 1480 次点击
    这是一个创建于 1049 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我糊了个 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 在标点周围错误插入的盘古之白的。不过看了一下最近的贴似乎是修了,老帖还是坏的。)我懒得把这个脚本放进任何有版本控制的地方。

    1 条回复    2021-05-14 18:24:54 +08:00
    orzfly
        1
    orzfly  
       2021-05-14 18:24:54 +08:00
    $("head").append => $("head").prepend

    不然 desktop.css 里覆盖的例如 h1 的 font-size / magin 就无效了,显得比较怪
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3937 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:24 · PVG 18:24 · LAX 03:24 · JFK 06:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.