中英標點轉換

Punctuation: English<->Chinese

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           punctuation_convert
// @name:zh-CN        中英标点转换
// @name:zh-TW        中英標點轉換
// @namespace      punctuation_convert
// @description    Punctuation: English<->Chinese
// @description:zh-CN Punctuation: English<->Chinese
// @description:zh-TW Punctuation: English<->Chinese
// @include        http://*
// @include        https://*
// @run-at      document-end
// @grant         GM_registerMenuCommand
// @grant         GM_setValue
// @grant         GM_getValue
// @author      zhuzemin
// @version     1.01
// ==/UserScript==
//config
let config = {
        'debug': false,
        'mode': GM_getValue('mode') || 1
}
let debug = config.debug ? console.log.bind(console) : function () {
};
// prepare UserPrefs
setUserPref(
        'mode',
        config.mode,
        'Punctuation: English<->Chinese',
        `English->Chinese: "1"; Chinese->English: "0"`,
);

let init = function () {
        try {
                document.title = convert(document.title);
                findTextNode(document.documentElement);
                debug(document.title);
        } catch (e) {
                debug(e);
        }
}
window.addEventListener('load', init);

function findTextNode(o) {
        let obj = o.childNodes;
        let olen = obj.length;
        for (let i = 0; i < olen; i++) {
                if (obj[i].splitText)
                        obj[i].data = convert(obj[i].data);
                else if (obj[i].childNodes)
                        findTextNode(obj[i]);
        }
}
function convert(cc) {
        let E_pun = ',.!?[]()<>"\'';
        let C_pun = ',。!?【】()《》“‘';
        let str = "";
        let find;
        let clen = cc.length;
        let src = null;
        let dest = null;
        let pattern = null;
        if (config.mode == 1) {
                src = E_pun;
                dest = C_pun;
                pattern = '[/x00-xff]';
        }
        else if (config.mode == 0) {
                src = C_pun;
                dest = E_pun;
                pattern = "[^\x00-\xff]";
        }
        for (let i = 0; i < clen; i++) {
                let ch = cc.charAt(i);
                let rerr = new RegExp(pattern);
                if (ch.search(rerr) == -1)
                        find = -1;
                else
                        find = src.indexOf(ch);
                if (find != -1)
                        str += dest.charAt(find);
                else
                        str += ch;
        }
        return str
}
/**
 * Create a user setting prompt
 * @param {string} varName
 * @param {any} defaultVal
 * @param {string} menuText
 * @param {string} promtText
 * @param {function} func
 */
function setUserPref(varName, defaultVal, menuText, promtText, func = null) {
        GM_registerMenuCommand(menuText, function () {
                let val = prompt(promtText, GM_getValue(varName, defaultVal));
                if (val === null) { return; }  // end execution if clicked CANCEL
                GM_setValue(varName, val);
                if (func != null) {
                        func(val);
                }
        });
}