打开/关闭菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

用户:UnownHearn/common.js:修订间差异

无编辑摘要
无编辑摘要
第48行: 第48行:
regex: /(sm|nm|im|av|ac)\d+/g,
regex: /(sm|nm|im|av|ac)\d+/g,
link: function (text) {
link: function (text) {
console.log([text, text.slice(0, 2)])
switch (text.slice(0, 2)) {
switch (text.slice(0, 2)) {
case "sm", "nm":
case "sm": case "nm":
return "https://www.nicovideo.jp/watch/" + text;
return "https://www.nicovideo.jp/watch/" + text;
case "im":
case "im":
第58行: 第59行:
return "http://www.acfun.cn/v/" + text;
return "http://www.acfun.cn/v/" + text;
}
}
// throw("?");
}
}
}, {
}, {
第82行: 第84行:
var rule = rules[ruleIndex];
var rule = rules[ruleIndex];
html = html.replace(rule.regex, function (old) {
html = html.replace(rule.regex, function (old) {
var link;
if (typeof rule.link === "function") {
if (typeof rule.link === "function") {
return rule.link(old);
link = rule.link(old);
} else {
link = rule.link.replace("{}", old);
}
}
return "<a class=\"autolink\" href=\"" + rule.link.replace("{}", old) + "\">" +
return "<a class=\"autolink\" href=\"" + link + "\">" +
old + "</a>";
old + "</a>";
});
});

2019年3月18日 (一) 18:05的版本

"use strict";

/**
 * 去掉内容与链接文本相同的 popup
 * @author [[User:UnownHearn]]
 * @version 1 (2018/07/28)
*/
(function () {
    // https://stackoverflow.com/questions/14346414/how-do-you-do-html-encode-using-javascript
    function htmlEncode(value) {
        //create a in-memory div, set it's inner text(which jQuery automatically encodes)
        //then grab the encoded contents back out.  The div never exists on the page.
        return $('<div/>').text(value).html();
    }

    $(".WikiaArticle a[title]").hover(function () {
        if (this.className != "") { return; } // 作为 wikilink 的超链接没有 class
        var a = $(this);
        var title = a.attr("title");
        if (title != a.text()) {
            a.append($('<span class="wikilink-popup">' + htmlEncode(title) + '</span>'));
        }
    }, function () {
        $(this).find(".wikilink-popup").remove();
    });
})();

/**
* 自动转换链接
* @author [[User:UnownHearn]]
* @version 1.4 (2019/03/18)
*/
(function () { /* 自动链接 */
    var filter = (function (node) {
        if (node.nodeType == 3 /* TEXT_NODE */) {
            return NodeFilter.FILTER_ACCEPT;
        } else if (node.nodeType != 1 /* ELEMENT_NODE */
            || node.tagName.toLowerCase() == "a"
            || node.classList.contains("no-autolink")) {
            return NodeFilter.FILTER_REJECT;
        }
        return NodeFilter.FILTER_SKIP;

    })

    var rules = [{
        // id: ["sm", "nm"],
        regex: /(sm|nm|im|av|ac)\d+/g,
        link: function (text) {
            console.log([text, text.slice(0, 2)])
            switch (text.slice(0, 2)) {
                case "sm": case "nm":
                    return "https://www.nicovideo.jp/watch/" + text;
                case "im":
                    return "https://seiga.nicovideo.jp/seiga/" + text;
                case "av":
                    return "https://www.bilibili.com/video/" + text;
                case "ac":
                    return "http://www.acfun.cn/v/" + text;
            }
            // throw("?");
        }
    }, {
        // id: ["mylist", "user"],
        regex: /(mylist|user)\/\d+/g,
        link: "https://www.nicovideo.jp/{}"
    }];

    var nodes = [];

    var as = document.getElementsByClassName("WikiaArticle");
    Array.prototype.forEach.call(as, function (a) {
        var walker = document.createTreeWalker(a, NodeFilter.SHOW_ALL, { acceptNode: filter });
        while (walker.nextNode()) {
            nodes.push(walker.currentNode);
        }
    })

    for (var nodeIndex in nodes) {
        var node = nodes[nodeIndex];
        var html = node.textContent;

        for (var ruleIndex in rules) {
            var rule = rules[ruleIndex];
            html = html.replace(rule.regex, function (old) {
                var link;
                if (typeof rule.link === "function") {
                    link = rule.link(old);
                } else {
                    link = rule.link.replace("{}", old);
                }
                return "<a class=\"autolink\" href=\"" + link + "\">" +
                    old + "</a>";
            });
        }

        if (html != node.textContent) {
            var parentNode = node.parentNode;
            var dummy = document.createElement("span");
            dummy.innerHTML = html;
            parentNode.replaceChild(dummy, node);
        }
    }
})();