Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phroneris/53c31f09d43454fd86d7834708300c67 to your computer and use it in GitHub Desktop.
Save Phroneris/53c31f09d43454fd86d7834708300c67 to your computer and use it in GitHub Desktop.
Tampermonkey用
// ==UserScript==
// @name 東方三次創作小説『水鏡』ルビ表示改善
// @namespace https://github.com/Phroneris
// @version 0.1
// @description 東方三次創作小説『水鏡』のルビ書きを右クリックメニューから真のHTMLルビに変換して読みやすくするスクリプト。
// @author 森の子リスのミーコの大冒険
// @match https://www.pixiv.net/novel/show.php?id=13611358
// @grant none
// @license public domain
// @run-at context-menu
// ==/UserScript==
/*
※ 右クリック後にマウスでこのスクリプトを選ぶと、
選んだその時のマウス位置で実行される(右クリックをした位置ではなく)ので
処理に失敗することがある。
本文を右クリックした後はキーボードの矢印キー操作で選ぶのが良い。
*/
dbg = v => { if (0 /* ←デバッグ時は1に */) { console.log(v); } }
dbg('デバッグ出力(F12 → コンソール)');
(() => {
const elmAll = document.querySelectorAll(':hover'); // マウス位置の全要素
const elmFrt = elmAll[elmAll.length - 1]; // そのうちの最前面要素=本文全体
dbg(elmFrt);
const txtOrg = elmFrt.innerHTML; // オリジナルのテキスト
// "漢字(かな)" というパターンを全て探し出してHTMLルビ要素に置換
// …という仕様上、例えば「某神主(かんぬし)」のように漢字の途中からルビがあると、
// 境目を認識できず「某神主」の漢字全体に「かんぬし」のルビが振られる。
// そういう仕様を予め承知して、変なとこは脳内変換して読めばヨシ!
const re = new RegExp(String.raw`(\p{sc=Han}+)([\((])([\p{sc=Hiragana}\p{sc=Katakana}]+)([\))])`, 'ug');
const txtNew = txtOrg.replace(re, '<ruby>$1<rp>$2</rp><rt>$3</rt><rp>$4</rp></ruby>');
// dbg(txtNew);
elmFrt.innerHTML = txtNew; // ページに書き込み
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment