MCBBS Observer

以另一种视角看MCBBS

目前为 2021-04-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         MCBBS Observer
// @namespace    sheep-realms
// @version      1.3.3
// @description  以另一种视角看MCBBS
// @author       Sheep-realms
// @match        *://www.mcbbs.net/home.php*
// @run-at       document-body
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

let $ = jQuery;
let getRequest = (variable, url = "") => {
    let query = url ? /\?(.*)/.exec(url)[1] : window.location.search.substring(1);
    let vars = query.split("&");
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

//获取URL参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//获取启动参数
var observer = getUrlParam("observer");

//判断是否需要启动
if(observer) {
    $('body *').remove();
    $('head link, head style, head script, head meta:not([http-equiv="Content-Type"])').remove();
    $('head title').text("MCBBS Observer");
    window.stop ? window.stop() : document.execCommand("Stop");
} else {
    $('#wp').ready(function () {
        $('#uhd ul.tb').append('<li id="observer-start" style="display:none;"><a href="/home.php?observer=1&action=main">启动 MCBBS Observer</a></li>');
        $('#uhd').dblclick(function () {
            $('#observer-start').attr("style","");
        });
    });
    return;
}

//初始化
var pageinit = 1;
var action = getUrlParam("action");

var fid = getUrlParam("fid");
var orderby = getUrlParam("orderby");
var filter = getUrlParam("filter");
var typeid = getUrlParam("typeid");

var filtervalue = "";
if (filter == "typeid") {
    filtervalue = "&typeid=" + typeid;
}

//有点乱,需要整理
(function() {

    $('body').append('<div id="header-box"></div>');
    $('body').append('<div id="content-box"></div>');
    $('body').append('<div id="footer-box"></div>');

    if(action == "main") {
        $('#header-box').append('<h1 id="obs-title">MCBBS Observer</h1>');
        $('#content-box').append('<p>欢迎使用MCBBS Observer!脚本基于MCBBS移动端API运行。不过众所周知MCBBS的移动端API一团糟,所以如果MCBBS的移动端API出点什么问题,这个脚本就不能用了。</p>');
        $('#content-box').append('<p>该脚本目前尚未完善,几乎没有图形界面,如需使用请自行传参。例如:<a href="/home.php?observer=1&action=threadlist&fid=52">矿工茶馆</a></p>');
    } else if(action == "threadlist") {
        $('#header-box').append('<h2 id="forum-name"></h2>');
        $('#header-box').append('<div id="forum-description"></div>');
        $('#header-box').append('<div id="forum-filter"></div>');
        $('#header-box').append('<div id="forum-type"></div>');

        //getThreadList(1);
        var jsonsave = getThreadList(1);

        $('#forum-filter').html('<p><a href="/home.php?observer=1&fid='+fid+'">默认排序</a> | <a href="/home.php?observer=1&fid='+fid+'&orderby=dateline">发帖时间</a></p>');
        $('#content-box').append('<table id="threadlist"><tr><th>标题</th><th>作者</th><th>发布时间</th><th>查看</th><th>回复</th><th>最后回复</th><th>最后回复作者</th><th>最后回复内容</th><th>操作</th></tr></table>');

        $('#footer-box').append('<div id="cmd-getlestpage" data-last="2">下一页</div>');
    }

    var csslist = "";
    csslist += "* {margin: 0; padding:0;}";
    csslist += "body {background:#FFF; font-size:12px;}";
    csslist += "a {text-decoration:none;}";
    csslist += "h1 {font-size:16px;}";
    csslist += "h2 {font-size:14px;}";
    csslist += "#forum-name {background:#EEE; padding:5px 10px;}";
    csslist += "#forum-description, #forum-filter, #forum-type {padding:5px 10px;}";
    csslist += "#threadlist {width:100%; border-collapse:separate; border-spacing:1px;}";
    csslist += "#threadlist tr:nth-child(odd) {background: #EEE;}";
    csslist += "#threadlist tr:hover {background: #fffce1;}";
    csslist += "#threadlist tr:nth-child(odd):hover {background: #f5f2d0;}";
    csslist += "#threadlist tr td:first-child, #threadlist tr th:first-child {padding-left:10px;}";
    csslist += "#threadlist th {text-align:left;}";
    csslist += "#threadlist td, #threadlist th {padding:5px 10px;}";
    csslist += "#cmd-getlestpage {text-align: center;padding: 12px 0;";


    var style = document.createElement("style");
    style.type = "text/css";
    style.innerHTML = csslist;
    document.getElementsByTagName("HEAD").item(0).appendChild(style);

})();

//绑定“下一页”事件
$('#cmd-getlestpage').click(function () {
    var lastpage = $('#cmd-getlestpage').data("last");
    getThreadList(lastpage);
    $('#cmd-getlestpage').data("last",lastpage+1);
});

//获取帖子列表
function getThreadList(page) {
$.getJSON("/api/mobile/index.php?version=4&module=forumdisplay&fid=" + fid + "&filter=" + filter + filtervalue + "&orderby=" + orderby + "&page=" + page,
function (ajson) {
    if(ajson.Variables.forum.redirect == undefined) {
        if(ajson.Message == undefined) {
            for(var i=0; i<ajson.Variables.forum_threadlist.length; i++) {
                $('#threadlist').append('<tr><td><a href="/thread-' + ajson.Variables.forum_threadlist[i].tid + '-1-1.html" title="附件:' + ajson.Variables.forum_threadlist[i].attachment + '" target="_blank">' + ajson.Variables.forum_threadlist[i].subject + '</td><td><a href="/?' + ajson.Variables.forum_threadlist[i].authorid + '" target="_blank">' + ajson.Variables.forum_threadlist[i].author + '</a></td><td><span title="' + timestampToTime(ajson.Variables.forum_threadlist[i].dbdateline) + '" data-timestamp="' + ajson.Variables.forum_threadlist[i].dbdateline + '">' + ajson.Variables.forum_threadlist[i].dateline + '</span></td><td>' + ajson.Variables.forum_threadlist[i].views + '</td><td>' + ajson.Variables.forum_threadlist[i].replies + '</td><td><span title="' + timestampToTime(ajson.Variables.forum_threadlist[i].dbdateline) + '" data-timestamp="' + ajson.Variables.forum_threadlist[i].dblastpost + '">' + ajson.Variables.forum_threadlist[i].lastpost + '</span></td><td><a href="/?' + getLastPostAuthorId(ajson.Variables.forum_threadlist[i]) + '" target="_blank">' + getLastPostAuthor(ajson.Variables.forum_threadlist[i]) + '</a></td><td>' + getLastPost(ajson.Variables.forum_threadlist[i]) + '</td><td>' + getThreadAction(ajson.Variables.forum_threadlist[i]) + '</td></tr>');
            }
        } else {
            $('#threadlist, #cmd-getlestpage').remove();
            $('#content-box').append(ajson.Message.messagestr);
        }
    } else {
        $('#threadlist, #cmd-getlestpage').remove();
        $('#content-box').append('<p>这是一个重定向版块,没有任何内容。</p>');
        $('#content-box').append('<p>重定向地址:' + ajson.Variables.forum.redirect + '</p>');
    }
    if (pageinit == 1) {setHeader(ajson);}
});
}

//设置版块头部
function setHeader(obj) {
    pageinit = 0;
    $('#forum-name').html(obj.Variables.forum.name + " (fid:" + obj.Variables.forum.fid + ")");
    $('#forum-description').html("版块描述:" + obj.Variables.forum.description);
    for (var val in obj.Variables.threadtypes.types) {
        $('#forum-type').append('<a href="/home.php?observer=1&fid=' + fid + '&orderby=' + orderby + '&filter=typeid&typeid=' + val + '">' + obj.Variables.threadtypes.types[val] + '</a> | ');
    }
}

//获取最后回复
function getLastPost(obj) {
    if(obj.reply == undefined) {
        return "";
    } else {
        if(obj.displayorder == 1) {
            return obj.reply[obj.reply.length-1].message;
        } else {
            return obj.reply[0].message;
        }
    }
}

//获取最后回复作者
function getLastPostAuthor(obj) {
    if(obj.reply == undefined) {
        return "";
    } else {
        if(obj.displayorder == 1) {
            return obj.reply[obj.reply.length-1].author;
        } else {
            return obj.reply[0].author;
        }
    }
}

//获取最后回复作者UID
function getLastPostAuthorId(obj) {
    if(obj.reply == undefined) {
        return "";
    } else {
        if(obj.displayorder == 1) {
            return obj.reply[obj.reply.length-1].authorid;
        } else {
            return obj.reply[0].authorid;
        }
    }
}

//获取帖子操作按钮
function getThreadAction(obj) {
    var posturl;
    if(obj.displayorder == 1) {
        if(obj.reply == undefined) {
            posturl = "/thread-" + obj.tid + "-1-1.html"; //这里似乎有点逻辑漏洞,以后再修
        } else {
            posturl = "/forum.php?mod=redirect&goto=findpost&ptid=" + obj.tid + "&pid=" + obj.reply[obj.reply.length-1].pid;
        }
    } else {
        posturl = "/forum.php?mod=redirect&tid=" + obj.tid + "&goto=lastpost#lastpost";
    }
    return '<a href="' + posturl + '" target="_blank" title="查看最新回复">查</a> · <a href="/forum.php?mod=post&action=reply&tid=' + obj.tid + '" target="_blank" title="回复主题">论</a>';
}

//时间戳转换
function timestampToTime(timestamp) {
    var date = new Date();
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());

    return Y+M+D+h+m+s;
}