// ==UserScript==
// @name MCBBS Observer
// @namespace sheep-realms
// @version 1.3.6
// @description 以另一种视角看MCBBS
// @author Sheep-realms
// @match *://www.mcbbs.net/home.php*
// @run-at document-body
// @grant GM_setValue
// @grant GM_getValue
// @downloadURL https://update.greasyfork.icu/scripts/424895/MCBBS%20Observer.user.js
// @updateURL https://update.greasyfork.icu/scripts/424895/MCBBS%20Observer.meta.js
// ==/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('
启动 MCBBS Observer');
$('#uhd').dblclick(function () {
$('#observer-start').attr("style","");
});
});
return;
}
//初始化
var domain = document.domain;
var pageinit = 1;
var action = getUrlParam("action");
var fid = getUrlParam("fid");
var forumName;
var orderby = getUrlParam("orderby");
var filter = getUrlParam("filter");
var typeid = getUrlParam("typeid");
var filtervalue = "";
if (filter == "typeid") {
filtervalue = "&typeid=" + typeid;
}
if (GM_getValue("obs_forumStar") == undefined) {
GM_setValue("obs_forumStar", {
"version": 1,
"starlist": []
})
}
//console.log(GM_getValue("obs_forumStar"));
//addForumStar(52, "矿工茶馆", "www.mcbbs.net");
//有点乱,需要整理
(function() {
$('body').append('');
$('body').append('');
$('body').append('');
if(action == "main") {
var forumStarList = GM_getValue("obs_forumStar").starlist.filter(function(a){return a.webSpace==domain});
forumStarList.sort(function(a,b){return a.index-b.index});
//console.log(forumStarList);
$('#header-box').append('MCBBS Observer
');
$('#content-box').append('欢迎使用MCBBS Observer!脚本基于MCBBS移动端API运行。不过众所周知MCBBS的移动端API一团糟,所以如果MCBBS的移动端API出点什么问题,这个脚本就不能用了。
');
$('#content-box').append('该脚本目前尚未完善,几乎没有图形界面,如需使用请自行传参。例如:矿工茶馆
');
$('#content-box').append('版块收藏功能仍未完善,如有需要请自行编写代码。
');
$('#content-box').append('');
for(var i = 0; i < forumStarList.length; i++) {
$('#threadlist').append('| ' + forumStarList[i].index + ' | ' + forumStarList[i].forumName + ' |
');
}
} else if(action == "threadlist") {
$('#header-box').append('');
$('#header-box').append('');
$('#header-box').append('');
$('#header-box').append('');
//getThreadList(1);
var jsonsave = getThreadList(1);
$('#forum-filter').html('默认排序 | 发帖时间 | [显示类型:默认]
');
$('#content-box').append('| 标题 | 作者 | 发布时间 | 置顶 | 附件 | 查看 | 回复 | 最后回复 | 最后回复作者 | 最后回复内容 | 操作 |
|---|
');
$('#footer-box').append('下一页
');
} else if(action == "edit") {
var data = getUrlParam("data");
$('#header-box').append('MCBBS Observer Editor
');
$('#content-box').append('');
if(data == "forumStar.json") {
$('#editor-content').text(formatJson(GM_getValue("obs_forumStar")));
} else {
$('#editor-content').text("不存在的数据。请检查参数。");
}
} else {
$('#header-box').append('MCBBS Observer Error
');
$('#content-box').append('请求参数错误:不合法的 action 参数:' + action + '
');
}
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 += "#threadlist th.action {width: 38px;}";
csslist += "#threadlist.view-default .attachment, #threadlist.view-default .displayorder {display:none;}";
csslist += "#threadlist.view-attribute .views, #threadlist.view-attribute .replies, #threadlist.view-attribute .lastpost, #threadlist.view-attribute .lastpost-author, #threadlist.view-attribute .lastpost-msg {display:none;}";
csslist += "#threadlist .index {width: 50px}";
csslist += "#editor-content {margin:10px; width:calc(100vw - 22px); height:calc(100vh - 100px);}";
csslist += "#cmd-getlestpage {text-align: center;padding: 12px 0;}";
csslist += ".cursor-pointer {cursor:pointer}";
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = csslist;
document.getElementsByTagName("HEAD").item(0).appendChild(style);
})();
//获取帖子列表
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[i].subject + ' | ' + ajson.Variables.forum_threadlist[i].author + ' | ' + ajson.Variables.forum_threadlist[i].dateline + ' | ' + formatDisplayorder(ajson.Variables.forum_threadlist[i].displayorder) + ' | ' + formatAttachment(ajson.Variables.forum_threadlist[i].attachment) + ' | ' + ajson.Variables.forum_threadlist[i].views + ' | ' + ajson.Variables.forum_threadlist[i].replies + ' | ' + ajson.Variables.forum_threadlist[i].lastpost + ' | ' + getLastPostAuthor(ajson.Variables.forum_threadlist[i]) + ' | ' + getLastPost(ajson.Variables.forum_threadlist[i]) + ' | ' + getThreadAction(ajson.Variables.forum_threadlist[i]) + ' | ');
}
} else {
$('#threadlist, #cmd-getlestpage').remove();
$('#content-box').append(ajson.Message.messagestr);
}
} else {
$('#threadlist, #cmd-getlestpage').remove();
$('#content-box').append('这是一个重定向版块,没有任何内容。
');
$('#content-box').append('重定向地址:' + ajson.Variables.forum.redirect + '
');
}
if (pageinit == 1) {setHeader(ajson);}
});
}
//添加版块收藏
function addForumStar(fid, fName = forumName, webSpace = domain, index = 0) {
var obs_forumStar = GM_getValue("obs_forumStar");
var oldIndex = obs_forumStar.starlist.findIndex(function(a){return a.fid == fid});
var data = {
"fid": fid,
"index": index,
"forumName": fName,
"webSpace": webSpace
};
if(oldIndex != -1) {
obs_forumStar.starlist[oldIndex] = data;
} else {
obs_forumStar.starlist.push(data);
}
if (obs_forumStar != undefined) {GM_setValue("obs_forumStar", obs_forumStar);}
}
//设置版块头部
function setHeader(obj) {
pageinit = 0;
forumName = obj.Variables.forum.name;
$('#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('' + obj.Variables.threadtypes.types[val] + ' | ');
}
}
//获取最后回复元数据
function getLastPostJSON(obj) {
if(obj.reply == undefined) {
return "";
} else {
if(obj.reply[1] != undefined) {
if(obj.reply[0].pid > obj.reply[1].pid) {
return obj.reply[0];
} else {
return obj.reply[obj.reply.length-1];
}
} else {
return obj.reply[0];
}
}
}
//获取最后回复消息
function getLastPost(obj) {
var obj2 = getLastPostJSON(obj);
if(obj2 == "") {
return "";
} else {
return obj2.message;
}
}
//获取最后回复作者
function getLastPostAuthor(obj) {
var obj2 = getLastPostJSON(obj);
if(obj2 == "") {
return "";
} else {
return obj2.author;
}
}
//获取最后回复作者UID
function getLastPostAuthorId(obj) {
var obj2 = getLastPostJSON(obj);
if(obj2 == "") {
return "";
} else {
return obj2.authorid;
}
}
//获取帖子操作按钮
function getThreadAction(obj) {
var posturl;
var objLastPost = getLastPostJSON(obj);
if(objLastPost == "") {
posturl = "/forum.php?mod=redirect&tid=" + obj.tid + "&goto=lastpost#lastpost";
} else {
posturl = "/forum.php?mod=redirect&goto=findpost&ptid=" + obj.tid + "&pid=" + objLastPost.pid;
}
return '查 · 论';
}
//格式化附件参数
function formatAttachment(value) {
switch(value) {
case "0": return "";
case "1": return "文件";
case "2": return "图片";
default: return value;
}
}
//格式化置顶参数
function formatDisplayorder(value) {
switch(value) {
case "0": return "";
case "1": return "本版";
case "2": return "大区";
case "3": return "全局";
default: return value;
}
}
//格式化精华参数
function formatDigest(value) {
switch(value) {
case "0": return "";
case "1": return "精华I";
case "2": return "精华II";
case "3": return "精华III";
default: return value;
}
}
//格式化特殊参数
function formatSpecial(value) {
switch(value) {
case "0": return "";
case "1": return "投票";
case "2": return "商品";
case "3": return "悬赏";
case "4": return "活动";
case "5": return "辩论";
case "127": return "插件";
default: return value;
}
}
//时间戳转换
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;
}
//绑定“下一页”事件
$('#cmd-getlestpage').click(function () {
var lastpage = $('#cmd-getlestpage').data("last");
getThreadList(lastpage);
$('#cmd-getlestpage').data("last",lastpage+1);
});
//绑定“显示类型”事件
// -- 逻辑警告:此方法会删除目标的所有class --
$('#threadlist-view-switch').click(function () {
if($('#threadlist').hasClass('view-default')) {
$('#threadlist').attr("class","");
$('#threadlist').addClass('view-attribute');
$('#threadlist-view-switch').text("[显示类型:详细属性]");
} else {
$('#threadlist').attr("class","");
$('#threadlist').addClass('view-default');
$('#threadlist-view-switch').text("[显示类型:默认]");
}
});
//格式化代码函数
function formatJson (json, options) {
var reg = null,
formatted = '',
pad = 0,
PADDING = ' ';
options = options || {};
options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
if (typeof json !== 'string') {
json = JSON.stringify(json);
} else {
json = JSON.parse(json);
json = JSON.stringify(json);
}
reg = /([\{\}])/g;
json = json.replace(reg, '\r\n$1\r\n');
reg = /([\[\]])/g;
json = json.replace(reg, '\r\n$1\r\n');
reg = /(\,)/g;
json = json.replace(reg, '$1\r\n');
reg = /(\r\n\r\n)/g;
json = json.replace(reg, '\r\n');
reg = /\r\n\,/g;
json = json.replace(reg, ',');
if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
reg = /\:\r\n\{/g;
json = json.replace(reg, ':{');
reg = /\:\r\n\[/g;
json = json.replace(reg, ':[');
}
if (options.spaceAfterColon) {
reg = /\:/g;
json = json.replace(reg, ':');
}
(json.split('\r\n')).forEach(function (node, index) {
var i = 0,
indent = 0,
padding = '';
if (node.match(/\{$/) || node.match(/\[$/)) {
indent = 1;
} else if (node.match(/\}/) || node.match(/\]/)) {
if (pad !== 0) {
pad -= 1;
}
} else {
indent = 0;
}
for (i = 0; i < pad; i++) {
padding += PADDING;
}
formatted += padding + node + '\r\n';
pad += indent;
}
);
return formatted;
};