NGA Character Code Converter

NGA 字符转码:发表、回复、编辑时,在提交前自动将部分原本直接提交无法正常显示的字符进行转码,使之提交后可以正常显示;编辑时将代码转回字符,方便编辑。

La data de 28-03-2017. Vezi ultima versiune.

// ==UserScript==
// @name         NGA Character Code Converter
// @namespace    https://greasyfork.org/zh-CN/scripts/28052-nga-character-code-converter
// @version      0.0.4
// @icon         http://bbs.nga.cn/favicon.ico
// @description  NGA 字符转码:发表、回复、编辑时,在提交前自动将部分原本直接提交无法正常显示的字符进行转码,使之提交后可以正常显示;编辑时将代码转回字符,方便编辑。
// @author       AgLandy
// @include      /^https?://(bbs\.ngacn\.cc|nga\.178\.com|bbs\.nga\.cn)/.+/
// @grant        none
// @require      http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js
// ==/UserScript==

var jQ = jQuery.noConflict();

function addScript(sc){
    jQ('head').append(jQ('<script type="text/javascript" />').html(sc));
}

function u2a(){
    var t = jQ('TEXTAREA').last()[0].value;
    if(t){
        var r = /&#(\d+);/;
        while(r.test(t))
            t = t.replace(r, String.fromCharCode(RegExp.$1));
        jQ('TEXTAREA').last()[0].value = t;
    }
}

function codeC(){
    var sc = "function nCCC(e){\
        var t = jQuery(e.target).parentsUntil('div.single_ttip2, div.mc').last().find('TEXTAREA')[0];\
        var v = t.value;\
        if(v){\
            if(e.button == 2){\
                var r = /&#(\\d+);/;\
                while(r.test(v))\
                v = v.replace(r, String.fromCharCode(RegExp.$1));\
            }\
            else{\
                var r = /([\\u00a0-\\u02ff\\u2010-\\u2013\\u2015-\\u2017\\u2025\\u2030-\\u203e\\u2105\\u2109\\u2116\\u2121\\u2160-\\u217f\\u2190-\\u2199\\u2200-\\u22ff\\u2312\\u2460-\\u249b\\u2500-\\u2642\\u3000\\u3003\\u3005\\u3007\\u300c-\\u300f\\u3012-\\u3015\\u301d-\\u3029\\u30f4-\\u30f6\\u3100-\\u312f\\u3190-\\u319f\\u3220-\\u3243\\u3280-\\u32b0\\u338e-\\u33d5\\ufe30-\\ufe6f\\uff02-\\uff0b\\uff0d-\\uff19\\uff1c-\\uff1e\\uff20-\\uff5d\\uffe2-\\uffe4])/;\
                while(r.test(v))\
                v = v.replace(r, '&#' + RegExp.$1.charCodeAt() + ';');\
            }\
            t.value = v;\
        }\
    }";

    addScript(sc);

    if(jQ('TEXTAREA').length)
        u2a();

    if(jQ('#fast_post_c').length)
        jQ('#fast_post_c').find('.uitxt1').attr('onmousedown','nCCC(event)');
    else
        jQ('.uitxt1').attr('onmousedown','nCCC(event)');

    jQ('body').on("click","a[href^='/post.php']",function(){
        setTimeout(function(){
            u2a();
            jQ('.single_ttip2').find('.uitxt1').attr('onmousedown','nCCC(event)');
        },500);
    });
}

addEventListener ('DOMContentLoaded', codeC(), false);













//  ↓  以下两段代码是用 match 抽出匹配项再用 for 循环替换的方案

/*
function u2a(){
    var t = document.getElementsByTagName("TEXTAREA")[0].value;
    if(t){
        var a = t.match(/&#(\d+);/g);
        if(a === null)
            return;
        for (i = 0; i < a.length; i++)
            t = t.replace(/&#(\d+);/, String.fromCharCode(a[i].replace(/[&#;]/g, '')));
        document.getElementsByTagName("TEXTAREA")[0].value = t;
    }
}
*/


/*
var sc = "function a2u(){\
var t = document.getElementsByTagName('TEXTAREA')[0].value;\
if(t){\
var a = t.match(/[\\u00a0-\\u02ff\\u2010-\\u203e\\u2105\\u2109\\u2116\\u2121\\u2160-\\u217f\\u2190-\\u2199\\u2200-\\u22ff\\u2312\\u2460-\\u249b\\u2500-\\u2642\\u3000-\\u3029\\u30f4-\\u30f6\\u3100-\\u312f\\u3190-\\u319f\\u3220-\\u3243\\u3280-\\u32b0\\u338e-\\u33d5\\ufe30-\\ufe6f\\uff02-\\uff5d\\uffe2-\\uffe4]/g);\
if(a === null)\
return;\
for (i = 0; i < a.length; i++)\
t = t.replace(/[\\u00a0-\\u02ff\\u2010-\\u203e\\u2105\\u2109\\u2116\\u2121\\u2160-\\u217f\\u2190-\\u2199\\u2200-\\u22ff\\u2312\\u2460-\\u249b\\u2500-\\u2642\\u3000-\\u3029\\u30f4-\\u30f6\\u3100-\\u312f\\u3190-\\u319f\\u3220-\\u3243\\u3280-\\u32b0\\u338e-\\u33d5\\ufe30-\\ufe6f\\uff02-\\uff5d\\uffe2-\\uffe4]/, '&#' + a[i].charCodeAt() + ';');\
document.getElementsByTagName('TEXTAREA')[0].value = t;\
}\
}";
*/