Greasy Fork is available in English.

Kana Convert Script

This script randomly converts kanji on webpages to hiragana.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Kana Convert Script
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  This script randomly converts kanji on webpages to hiragana.
// @author       Wantitled
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @match        https://www3.nhk.or.jp/*
// @match        https://ja.wikipedia.org/*
// @grant        none
// @license      MIT
// ==/UserScript==

// ============================= Config =============================

// APIKEY is a personal key which can be created via the steps listed here: https://labs.goo.ne.jp/apiusage/
const APIKEY = '';

// probability sets the amount of kanji on a page that will be converted, which goes up to 100 for all kanji (although most likely not all kanji will be converted).
// The API calls can be quite slow so above 50 is not recommended.
const probability = 30;

// leftHighlight and rightHighlight highlight where the converted kana is and can be left blank
const leftHighlight = "";
const rightHighlight = "";

// maxChunkSize determines the maximumm size of a chunk of text that will be converted
const maxChunkSize = 30;

// ==================================================================

(function() {
    'use strict';
    const textReg1 = / \\+ /g;
    const textReg2 = /[一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+|[々〆〤ヶ]+/u;
    let convertedText;

    var convertText = (text) => {
        return $.ajax({
            url: "https://labs.goo.ne.jp/api/hiragana",
            type: "POST",
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                                  'app_id': APIKEY,
                                  'sentence':text,
                                  'output_type':'hiragana'
                                 }),
            success: function (result){
                return result.converted;
            },
            error: function(error){
                console.log("Error: ", error);
            }
        }).responseJSON.converted;
    }

    var decideConvert = (val) => {
        return Math.floor(Math.random() * 100) <= val;
    }

    var convertSection = (text) => {
        let textArr = text.split(/(?![一-龠]+|[ぁ-ゔ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+|[々〆〤ヶ]+)/u);
        let returnArr = [];
        for (let i = 0; i < textArr.length; i++){
            if (decideConvert(probability) && textArr[i].length <= maxChunkSize && /[一-龠]+/u.test(textArr[i])){
                convertedText = convertText(textArr[i]);
                if (convertedText !== undefined){
                    textArr[i] = leftHighlight+convertedText+rightHighlight;
                }
            }
        }
        return textArr.join("");
    }

    $(document).ready(function(){

        (function() {
            var treeWalker = document.createTreeWalker(
                document.body,
                NodeFilter.SHOW_TEXT,
                { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } }
            );
            var currentNode = treeWalker.currentNode;
            while(currentNode) {
                if (currentNode.nodeType == 3
                    && !textReg1.test(currentNode.textContent) && textReg2.test(currentNode.textContent)){
                    if (currentNode.parentNode.nodeName !== "SCRIPT"
                        && currentNode.parentNode.nodeName !== "IMG"
                        && currentNode.parentNode.nodeName !== "CITE"
                        && currentNode.parentNode.nodeName !== "STYLE"
                        && currentNode.parentNode.nodeName !== "IMG"
                        && currentNode.parentNode.nodeName !== "NOSCRIPT"
                       ){
                        if(decideConvert(probability)){
                            currentNode.textContent = convertSection(currentNode.textContent);
                        }
                    }
                }
                currentNode = treeWalker.nextNode();
            }
        })()
     })

})();