User:魔琴/gadgets/localTimeBracket/index.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$(document).ready(function () {
    mw.loader.load('/w/index.php?title=User:魔琴/gadgets/localTimeBracket/css.css&action=raw&ctype=text/css', 'text/css');

    offset = mw.user.options.get('timecorrection').replace(/^.*?\|(-?\d*).*?$/, '$1')
    var full = ""
    m = 0 + offset % 60
    h = (offset - m) / 60
    full = m === 0 ? h : h + ':' + Math.abs(m)
    if (h >= 0) {
        full = '+' + full
    }
    var z = window.localTimeBracket ? window.localTimeBracket : 'UTC' + full
    const regex = /(\d{3}(<span class="gadget-space">)*\d(<\/span>)*(<span class="gadget-space">)*年(<\/span>)*\d?(<span class="gadget-space">)*\d(<\/span>)*(<span class="gadget-space">)*月(<\/span>)*\d?(<span class="gadget-space">)*\d(<\/span>)*(<span class="gadget-space">)*日(<\/span>)* \([一二三四五六日]\) \d{2}:\d(<span class="gadget-space">\d<\/span>|\d)(?! \(UTC\))(?!<span class="localTimeBracket">)(?!()(?!)))/gm;
    const subst = '$1<span class="localTimeBracket"> (' + z + ')</span>';

    function addzone(a) {
        if ($(a).html().match(regex)) {
            $(a).html($(a).html().replace(regex, subst))
        }
    }

    $('a').each(function() {
        addzone(this);
    });
    $('span').each(function() {
        addzone(this);
    });
    $('li').each(function() {
        addzone(this);
    });
    $('.mw-search-result-data').each(function() {
        addzone(this);
    });
})