v2ex 和谐插件

世界本该和谐

目前為 2019-09-11 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         v2ex 和谐插件
// @namespace    https://github.com/imdong/v2hx?u
// @version      0.1
// @description  世界本该和谐
// @author       青石
// @match        https://www.v2ex.com/t/*
// @match        https://www.v2ex.com/new
// @grant        none
// ==/UserScript==

(function (window, $) {
    'use strict';

    let _v2hx = window._v2hx || {
        /**
         * 字符串转 base64 并翻转
         * @param {string} str 需要编码的字符串
         */
        str2b64: function (str) {
            return btoa(escape(str)).split("").reverse().join('');
        },
        /**
         * 翻转 base64 编码并转 字符串
         * @param {string}} str 编码后的字符串
         */
        b642str: function (str) {
            return unescape(atob(str.split("").reverse().join('')));
        },
        /**
         * 替换文本中需要编码的字符串
         * @param {string} content 待处理的文本内容
         */
        encode: function (content) {
            let result = content.replace(/~~([!@#$%^&])([^~]+)~~/g, function (match_full, type, content, offset, string) {
                // 对于 type 做备用,暂不处理
                return "[v2hx::" + _v2hx.str2b64(content) + "]";
            });
            return result;
        },
        /**
         * 找出需要解编码的字符串解码后替换
         * @param {string} str 待处理的文本内容
         */
        decode: function (str) {
            let result = str.replace(/\[([vV]2[hH][xX]):([^:]{0,2}):([^\]]+)\]/g, function (match_full, head, type, content, offset, string) {
                // 对于 type / head 做备用,暂不处理
                return _v2hx.b642str(content);
            });
            return result;
        },
        /**
         * 拦截 发主题 / 回复 输入框,尝试替换内容
         */
        changeEdit: function () {
            if (location.pathname == '/new') {
                window.editor.setValue(this.encode(window.editor.getValue()));
            } else if (location.pathname.substr(0, 3) == '/t/') {
                $("#reply_content").val(this.encode($("#reply_content").val()));
            } else {
                return;
            }
            return true;
        },
        /**
         * 解码正文 / 评论的内容
         */
        decodeText: function () {
            if (location.pathname.substr(0, 3) != '/t/') return;
            // 评论回复
            $('.topic_content,.reply_content').each(function (index, item) {
                item.childNodes.forEach(function (child, id) {
                    if (child.nodeType == 3) {
                        child.textContent = _v2hx.decode(child.textContent);
                    }
                });
            });
        },
        /**
         * 初始化
         */
        init: function () {
            $('#editor,#reply_content').parents('form').submit(function (event) {
                return _v2hx.changeEdit();
            });
            this.decodeText();
        }
    };

    _v2hx.init();
    window.v2hx = _v2hx;
})(window, jQuery);