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

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

无编辑摘要
(test)
第1行: 第1行:
/**
// https://stackoverflow.com/questions/14346414/how-do-you-do-html-encode-using-javascript
* 去掉内容与链接文本相同的 popup
function htmlEncode(value){
* @author [[User:UnownHearn]]
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
* @version 1 (2018/07/28)
//then grab the encoded contents back out. The div never exists on the page.
*/
return $('<div/>').text(value).html();
(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() {
$(".WikiaArticle a[title]").hover(function () {
if (this.className != "") { return; } // 作为 wikilink 的超链接没有 class
if (this.className != "") { return; } // 作为 wikilink 的超链接没有 class
var a = $(this);
var a = $(this);
var title = a.attr("title");
var title = a.attr("title");
if (title != a.text()) {
if (title != a.text()) {
a.append($('<span class="wikilink-popup">' + htmlEncode(title) + '</span>'));
a.append($('<span class="wikilink-popup">' + htmlEncode(title) + '</span>'));
}
}
}, function() {
}, function () {
$(this).find(".wikilink-popup").remove();
$(this).find(".wikilink-popup").remove();
});
});
})();

/**
* 自动转换链接
* @author [[User:UnownHearn]]
* @version 1 (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: "mylist",
regex: /(mylist)\/\d+/g,
link: "https://www.nicovideo.jp/{}"
}, {
// id: "av",
regex: /(av)\d+/g,
link: "http://www.bilibili.com/video/{}"
}, {
// id: ["sm", "nm"],
regex: /(sm|nm)\d+/g,
link: "http://www.nicovideo.jp/watch/{}"
}]

var nodes = []

for (var a of document.getElementsByClassName("WikiaArticle")) {
var walker = document.createTreeWalker(a, NodeFilter.SHOW_ALL, { acceptNode: filter });
while (walker.nextNode()) {
nodes.push(walker.currentNode)
}
}

for (node of nodes) {
var html = node.textContent;

for (var rule of rules) {
html = html.replace(rule.regex, function (old) {
console.log(old);
return "<a class=\"autolink\" href=\"" + rule.link.replace("{}", old) + "\">" +
old + "</a>";
});
}

if (html != node.textContent) {
console.log(1);
var parentNode = node.parentNode;
var div = document.createElement("div");
div.innerHTML = html;
parentNode.replaceChild(div, node);
console.log(parentNode);
}
}
})();

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

/**
 * 去掉内容与链接文本相同的 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 (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: "mylist",
        regex: /(mylist)\/\d+/g,
        link: "https://www.nicovideo.jp/{}"
    }, {
        // id: "av",
        regex: /(av)\d+/g,
        link: "http://www.bilibili.com/video/{}"
    }, {
        // id: ["sm", "nm"],
        regex: /(sm|nm)\d+/g,
        link: "http://www.nicovideo.jp/watch/{}"
    }]

    var nodes = []

    for (var a of document.getElementsByClassName("WikiaArticle")) {
        var walker = document.createTreeWalker(a, NodeFilter.SHOW_ALL, { acceptNode: filter });
        while (walker.nextNode()) {
            nodes.push(walker.currentNode)
        }
    }

    for (node of nodes) {
        var html = node.textContent;

        for (var rule of rules) {
            html = html.replace(rule.regex, function (old) {
                console.log(old);
                return "<a class=\"autolink\" href=\"" + rule.link.replace("{}", old) + "\">" +
                    old + "</a>";
            });
        }

        if (html != node.textContent) {
            console.log(1);
            var parentNode = node.parentNode;
            var div = document.createElement("div");
            div.innerHTML = html;
            parentNode.replaceChild(div, node);
            console.log(parentNode);
        }
    }
})();