Skip to content

Instantly share code, notes, and snippets.

@matori
Created May 26, 2024 12:53
Show Gist options
  • Save matori/ec1f2302d1a8d79607b188b4bf2fb7a5 to your computer and use it in GitHub Desktop.
Save matori/ec1f2302d1a8d79607b188b4bf2fb7a5 to your computer and use it in GitHub Desktop.
カクヨム用一字下げスクリプト
// ==UserScript==
// @name カクヨム一字下げスクリプト
// @namespace http://tampermonkey.net/
// @version 2024-05-24
// @description try to take over the world!
// @author You
// @match https://kakuyomu.jp/works/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=kakuyomu.jp
// @grant none
// ==/UserScript==
(function() {
const segmenter = new Intl.Segmenter("ja-JP");
// 1文字目がこれらのどれかに当てはまったら1字下げは実行しない
const segmentList = segmenter.segment(`「『〈《【(⦅〔✓ :◇◆□■`);
const brackets = Array.from(segmentList).map(item => item.segment);
const paragraphs = document.querySelectorAll('.js-episode-body > p');
let converted = false;
for (const paragraph of paragraphs) {
const paragraphLetters = segmenter.segment(paragraph.textContent);
const firstLetter = Array.from(paragraphLetters).map(item => item.segment)[0];
if (!firstLetter) {
continue;
}
const isBracketFirst = brackets.includes(firstLetter);
if (!isBracketFirst) {
paragraph.textContent = ' ' + paragraph.textContent;
converted = true;
}
}
// 不要なら消していい
if (converted) {
console.log('%c⚠️ユーザースクリプトによって一字下げが実行されました%c', 'font-size: 18px; font-weight: bold; color: tomato;')
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment