MCBBS Observer

以另一种视角看MCBBS

当前为 2021-04-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         MCBBS Observer
// @namespace    sheep-realms
// @version      1.3.1
// @description  以另一种视角看MCBBS
// @author       Sheep-realms
// @match        *://www.mcbbs.net/home.php*
// @run-at       document-body
// @grant        none
// ==/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);
}

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></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="时间戳:' + 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="时间戳:' + ajson.Variables.forum_threadlist[i].dblastpost + '" 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]) + '">' + getLastPostAuthor(ajson.Variables.forum_threadlist[i]) + '</a></td><td>' + getLastPost(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;
        }
    }
}