huiji-tieba-script

Automatic URL conversion, fix sticker link

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name               huiji-tieba-script
// @name:zh-CN         百度灰机汉化组吧脚本
// @version            0.1.2
// @description        Automatic URL conversion, fix sticker link
// @description:zh-cn  自动转换4位尾号为网址, 修复包含表情的链接
// @author             yahiousun
// @include            *://tieba.baidu.com/p/*
// @grant              none
// @namespace https://greasyfork.org/users/157242
// ==/UserScript==

(function(window, document) {
    'use strict';
    var CODE_REGEX = /(\u4ee3\u7801|\u4ee3\u53f7|\u5c3e\u53f7|\u672b\u4f4d|\u56db\u4f4d|\u56db\u4f4d\u6570|\u4f60\u4eec\u61c2\u5f97)[^\d]*?(\d{4})(?!:)/g;
    var STICKER_REGEX = /h([^<]*<img[^>]*class="BDE_Smiley[^>]*>)+[^<]*omnibus\/100(\d{4})/g;
    var TEMPLATE = "<a href=\"http://smp.yoedge.com/view/omnibus/100$2\" target=\"_blank\">http://smp.yoedge.com/view/omnibus/100$2</a>";
    var THRESHOLD = 300;

    function debounce(fn, thershold, context) {
        var timer = null;
        return function() {
            clearTimeout(timer);
            var args = arguments;
            timer = setTimeout(function() {
                fn.apply(context, args);
            }, thershold);
        };
    }

    function smp() {
        var contents = document.querySelectorAll('cc');
        var comments = document.querySelectorAll('.lzl_content_main');
        if (CODE_REGEX.test(document.body.innerHTML)) {
            if (typeof contents !== 'undefined') {
                [].forEach.call(contents, function(node) {
                    node.innerHTML = node.innerHTML.replace(CODE_REGEX, TEMPLATE);
                });
            }
            if (typeof comments !== 'undefined') {
                [].forEach.call(comments, function(node) {
                    node.innerHTML = node.innerHTML.replace(CODE_REGEX, TEMPLATE);
                });
            }
        }
        if (STICKER_REGEX.test(document.body.innerHTML)) {
            if (typeof contents !== 'undefined') {
                [].forEach.call(contents, function(node) {
                    node.innerHTML = node.innerHTML.replace(STICKER_REGEX, TEMPLATE);
                });
            }
            if (typeof comments !== 'undefined') {
                [].forEach.call(comments, function(node) {
                    node.innerHTML = node.innerHTML.replace(STICKER_REGEX, TEMPLATE);
                });
            }
        }
    }

    window.addEventListener('DOMNodeInserted', debounce(smp, THRESHOLD));
    window.addEventListener('DOMSubtreeModified', debounce(smp, THRESHOLD));
})(window, document);