Greasy Fork

Greasy Fork is available in English.

MCBBS Extender

MCBBS行为拓展/样式修复

目前为 2020-02-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         MCBBS Extender
// @namespace    https://i.zapic.cc
// @version      release-1.0.0
// @description  MCBBS行为拓展/样式修复
// @author       Zapic
// @match        https://www.mcbbs.net/*
// @run-at       document-body
// ==/UserScript==

(function () {
    var conf = {
        //美化代码块样式
        "fixCodeBlock": true,
        //修复代码块"复制代码"换行消失的问题
        "fixCodeCopy": true,
        //修复表格排版内含有多媒体导致元素戳破表格的问题
        "fixTableLayout": true,
        //后台轮询新消息
        "QueryMessage": true,
        //后台轮询消息间隔,单位为秒
        "QueryMessageInterval": 60,
        //板块页点击"下一页"按钮是否记住当前页码
        "rememberPage": true,
        //美化右侧回到顶部按钮
        "animateGoToTopButton": true,
        //固定顶栏在顶部
        "pinnedTopBar": true,
        //顶栏弹出菜单美化&对齐
        "fixTopBarPopMenu": true,
        //主题悬浮预览
        "hoverPreviewTheme": true,
        //玻璃质感勋章
        "hoverableMedal": true
    };
    if (typeof jQuery == "undefined") {
        console.log("This page does NOT contain JQuery,MCBBS Extender will not work.");
        return false;
    }
    var jq = jQuery;
    if (conf.fixCodeBlock) {
        jq("head").append("<style id=\"fixCodeBlock\">.hljs > ::-webkit-scrollbar {background: #00000000;height: 7px;width:7px}.hljs > ::-webkit-scrollbar-thumb:hover{background:#0000005c;}.hljs > ::-webkit-scrollbar-thumb{background:#0000002c;border-radius: 3px}.pl .blockcode{max-height:483px;overflow:auto;padding: 10px 0 5px 10px;background-attachment: local}.blockcode ol{overflow-x: unset!important;}.hljs{display:unset;}.pl .blockcode ol{margin: 0 0 0px -10px!important;padding: 0 0 0px 20px !important;}.pl .blockcode ol li:hover {background-color: #00000010;color: #666;}.pl .blockcode ol li {margin-left: 23px;min-width: fit-content;padding-right: 10px;transition-duration:.2s;}.pl .blockcode em {display: block;margin-top: 5px;}</style>");
    }
    if (conf.fixTableLayout) {
        jq("head").append("<style id=\"fixTableLayout\">.pl table.t_table{table-layout: fixed;}.pl table.t_table[style*=\"px\"]{table-layout: auto;}</style>");
    }
    if (conf.fixCodeCopy) {
        copycode = function (obj) {
            if (!obj)
                return false;
            var count = obj.children[0].children.length;
            var code = "";
            for (i = 0; i < count; i++) {
                code += obj.children[0].children[i].innerText + "\r\n";
            }
            setCopy(code, '代码已复制到剪贴板');
        };
    }
    if (conf.QueryMessage) {
        var queryId = hash(new Date().getTime().toLocaleString(),16);
        var starttime = Math.floor(new Date().getTime()/1000);
        if (!window.Html5notification) {
            jq.getScript("data/cache/html5notification.js?xm6");
        }
        var RunningTimer = setInterval(function () {
            if(getcookie("MExt_LastQuery")==""){
                setcookie("MExt_LastQuery",0,0);
            }
            var nowtime = Math.floor(new Date().getTime()/1000);
            if(getcookie("MExt_ActiveQueryId") == "" || nowtime - getcookie("MExt_LastQuery") > 5){
                setcookie("MExt_ActiveQueryId",queryId,0);
            }
            if(getcookie("MExt_ActiveQueryId") !== queryId){
                if(typeof QueryTimer != "undefined"){
                    clearInterval(QueryTimer);
                    QueryTimer = undefined;
                }
                return false;
            }
            setcookie("MExt_LastQuery",nowtime,0);
            if(typeof QueryTimer == "undefined"){
                QueryTimer = setInterval(function(){
                    localStorage.setItem('notifica-time',new Date().getTime());
                    jq.getJSON("https://www.mcbbs.net/api/mobile/index.php?module=profile",function(data){
                        var notices = data.Variables.notice;
                        var noticecontent = JSON.stringify(notices);
                        if(notices.newpm + notices.newprompt + notices.newmypost > 0){
                            jq.get("https://www.mcbbs.net/forum.php?mod=misc", function (d) {
                                var dom = jq(d);
                                var el = dom.filter("script[src*=html5notification]");
                                var ut = dom.find(".user_tools");
                                var pum = dom.filter("#myprompt_menu");
                                jq(".user_tools").html(ut.html());
                                jq("#myprompt_menu").html(pum.html());
                                jq("title").text(NOTICECURTITLE);
                                var s = el.nextUntil("div").last().text();
                                if(noticecontent != getcookie("MExt_LastNoticeContent")){
                                    localStorage.setItem('notifica-time',1);
                                    eval(s);
                                }
                                setcookie("MExt_LastNoticeContent",noticecontent,0);
                            });
                         }
                    });
                },conf.QueryMessageInterval*1000);
            }
        }, 1000);
    }
    if (conf.rememberPage) {
        jq(document).ready(function () {
            if ($('autopbn')) {
                var npbtn = $('autopbn')
                var dc = npbtn.onclick;
                npbtn.onclick = function () {
                    var nextpageurl = this.getAttribute('rel').valueOf();
                    var curpage = parseInt(this.getAttribute('curpage').valueOf());
                    this.setAttribute('curpage', curpage + 1);
                    nextpageurl = nextpageurl.replace(/&page=\d+/, '&page=' + (curpage + 1));
                    history.replaceState(null, null, nextpageurl);
                    dc();
                };
            }
        });
    }
    if (conf.animateGoToTopButton) {
        jq("head").append("<style id=\"GoToTopButton\">#scrolltop{bottom: 270px!important;visibility:visible;overflow-x:hidden;width:75px;}.scrolltopa{transition-duration:.15s;margin-left:-40px;opacity:0;}.scrolltopashow{margin-left:0px;opacity:1;}</style>");
        showTopLink = function () {
            var ft = $('ft');
            if (ft) {
                var scrolltop = $('scrolltop');
                var scrolltopbtn = jq(".scrolltopa");
                var viewPortHeight = parseInt(document.documentElement.clientHeight);
                var scrollHeight = parseInt(document.body.getBoundingClientRect().top);
                var basew = parseInt(ft.clientWidth);
                var sw = scrolltop.clientWidth;
                if (basew < 1000) {
                    var left = parseInt(fetchOffset(ft)['left']);
                    left = left < sw ? left * 2 - sw : left;
                    scrolltop.style.left = (basew + left + 44) + 'px';
                } else {
                    scrolltop.style.left = 'auto';
                    scrolltop.style.right = 0;
                }
                if (scrollHeight < -100) {
                    scrolltopbtn.addClass("scrolltopashow");
                } else {
                    scrolltopbtn.removeClass("scrolltopashow");
                }
            }
        }
        showTopLink();
    }
    if (conf.pinnedTopBar) {
        jq("head").append("<style id=\"pinnedTopBar\">#toptb{position: fixed;width: 100%;z-index:790;top:0;box-shadow: #0000004f 3px 3px 5px 1px;}.mc_map_wp{padding-top: 45px;}#scbar_type_menu{top:38px!important}#user_info_menu,#myprompt_menu,#usertools_menu,#sslct_menu {position:fixed!important;top:47px!important}</style>");
    }
    if (conf.fixTopBarPopMenu) {
        jq("head").append("<style id=\"fixTopBarPopMenu\">div#user_info_menu {margin-top: 5px;}.user_info_menu_info > li {margin-top: 2px;}a.rank {padding: 2px 7px!important; border-radius: 14px;}a.rank:hover {text-decoration: none;}ul.user_info_menu_btn {padding-top: 6px;}ul.user_info_menu_btn>li>a:hover {background: #36b030;color: white;}ul.user_info_menu_btn>li>a {padding: 5px 8px;border-radius: 5px;}ul.user_info_menu_btn>li>a[onclick]:hover {background: red!important;}#myprompt_menu {margin-left: -10px;}#myprompt_menu,#usertools_menu,#sslct_menu { z-index:791!important;margin-top: 5px!important;border: 1px solid #d1d1d1;min-width: unset;border-radius: 5px;}#myprompt_menu>li>a, #usertools_menu>li>a {border: none; border-radius: 5px;text-align: center;padding: 3px 15px;}#myprompt_menu>li>a:hover, #usertools_menu>li>a:hover {background: #36b030;color: white;}div#sslct_menu {margin-left: 16px;padding-left: 14px;}.sslct_btn {border: none!important;width: 15px;height: 15px;padding: 2px;}.sslct_btn i {border-radius: 50%;width: 13px;height: 13px;}</style>");
        var __extstyle = extstyle;
        extstyle = function (style) {
            __extstyle(style);
            jq("#fixTopBarPopMenuWinter").remove();
            if (style == "./template/mcbbs/style/winter") {
                jq("head").append("<style id=\"fixTopBarPopMenuWinter\">.user_info_menu_info li a.rank {background-color: #5c8dff!important;}ul.user_info_menu_btn>li>a:hover,#myprompt_menu>li>a:hover, #usertools_menu>li>a:hover {background: #5c8dff!important;}</style>");
            }
        }
        var theme = getcookie('extstyle');
        if (theme == "./template/mcbbs/style/winter") {
            jq("head").append("<style id=\"fixTopBarPopMenuWinter\">.user_info_menu_info li a.rank {background-color: #5c8dff!important;}ul.user_info_menu_btn>li>a:hover,#myprompt_menu>li>a:hover, #usertools_menu>li>a:hover {background: #5c8dff!important;}</style>");
        }
    }
    if (conf.hoverPreviewTheme) {
        jq(document).ready(function () {
            jq(".sslct_btn").first().on("mouseover", function () {
                previewstyle = getcookie('extstyle');
                extstyle('./template/mcbbs/style/default');
            });
            jq(".sslct_btn").last().on("mouseover", function () {
                previewstyle = getcookie('extstyle');
                extstyle('./template/mcbbs/style/winter');
            });
            jq(".sslct_btn").last().on("click", function () {
                previewstyle = './template/mcbbs/style/winter'
            });
            jq(".sslct_btn").first().on("click", function () {
                previewstyle = './template/mcbbs/style/default'
            });
            jq(".sslct_btn").on("mouseout", function () {
                extstyle(previewstyle);
            });
        });
    }
    if (conf.hoverableMedal) {
        jq(document).ready(() => {
            jq('.md_ctrl').each((t, v) => {
                jq(v).children(0).children('img').each((b, n) => {
                    var id = 'md' + new RegExp('\\_\\d*$').exec(jq(n).attr('id'))[0];
                    jq(v).append(jq(jq('<span class="hoverable-medal" id="' + jq(n).attr('id') + '" style="background-image:url(' + jq(n).attr('src') + ')"><div></div></span>').on('mouseover', () => {
                        showMenu({
                            'ctrlid': jq(n).attr('id'),
                            'menuid': id + '_menu',
                            'pos': '12!'
                        });
                    })));
                    jq("#" + id + "_menu .tip_horn").css("background-image", "url(" + jq(n).attr('src') + ")");
                    jq(n).remove();
                })
            })
        });
        jq("head").append("<style id=\"hoverableMedal\">.hoverable-medal:hover div {margin-top: 0px!important;opacity: 1!important;}.hoverable-medal div {margin-top: -15px;opacity: 0.6;transition-duration: .4s;background-image:url(//i.loli.net/2020/02/10/6qauEUHK8cty4lR.png);width:100%;height:100%;filter: blur(2px);}div.tip.tip_4[id*=md_] {width: 105px;height: 165px;border: none;box-shadow: black 0px 2px 10px -3px;margin-left: 38px;margin-top: 115px;background: black;overflow: hidden;pointer-events:none!important;border-radius: 5px;padding: 0px;}div.tip.tip_4[id*=md_] .tip_horn {background-size: cover;background-position: center;height: 200%;width: 200%;z-index: -1;filter: blur(7px) brightness(0.8);top: -50%;left: -50%;}div.tip.tip_4[id*=md_] .tip_c {color: #fffffffc;}div.tip.tip_4[id*=md_] h4 {text-align: center;padding: 10px 5px;background-color: #ffffff52;}div.tip.tip_4[id*=md_] p {padding: 20px 10px;}.md_ctrl{margin-left:17px!important}.hoverable-medal {width: 31px;height: 53px;transition-duration: 0.4s;border-radius: 3px;display: inline-block;margin: 5px;background-position: center;box-shadow: 0px 2px 5px 0px black;overflow:hidden;}.hoverable-medal:hover {transform: matrix3d(1, 0, 0, 0, 0, 1, 0, -0.003, 0, 0, 1, 0, 0, -1.5, 0, 0.9);box-shadow: 0px 2px 10px -3px black;}</style>");
    }
})();