// ==UserScript==
// @name MCBBS Extender
// @namespace https://i.zapic.cc
// @version release-1.0.5
// @description MCBBS行为拓展/样式修复
// @author Zapic
// @match https://*.mcbbs.net/*
// @run-at document-body
// @downloadURL none
// ==/UserScript==
(function () {
if (typeof jQuery == "undefined") {
console.error("This page does NOT contain JQuery,MCBBS Extender will not work.");
return false;
}
var dlg = function (m) {
console.debug("[MCBBS Extender]" + m)
};
var jq = jQuery;
try {
var conf = JSON.parse(getcookie("MExt_config"));
} catch (e) {
dlg("Failed to load config\n" + e);
}
if (!conf) {
setcookie("MExt_config", JSON.stringify({
"fixCodeBlock": true,
"fixCodeCopy": true,
"fixTableLayout": true,
"queryMessage": true,
"queryMessageInterval": 60,
"rememberPage": true,
"animateGoToTopButton": true,
"pinnedTopBar": true,
"fixTopBarPopMenu": true,
"hoverPreviewTheme": true,
"hoverableMedal": true,
"ljyysSearch": true
}), 2147483647);
conf = JSON.parse(getcookie("MExt_config"));
showDialog("欢迎使用MCBBS Extender.
本脚本的设置按钮已经放进入了您的个人信息菜单里,如需调整设置请在个人信息菜单里寻找.","right","欢迎",function(){showMenu('user_info');jq("#MExt_config").css("background-color","#E91E63").css("color","#fff");setTimeout(function(){hideMenu('user_info_menu');jq("#MExt_config").css("background-color","").css("color","");},3000);});
dlg("Config init.");
}
jq(document).ready(function () {
jq("#user_info_menu .user_info_menu_btn").append("
MCBBS Extender 设置");
dlg("Appended Config button");
jq("head").append('');
dlg("Appended Config window style")
jq("#MExt_config").on("click", function () {
hideMenu("user_info_menu");
showDialog(
'',
"confirm",
"MCBBS Extender 设置",
function () {
var cf = {};
cf.fixCodeBlock = jq("#c_fixcodeblock").prop("checked");
cf.fixCodeCopy = jq("#c_fixcopycode").prop("checked");
cf.fixTableLayout = jq("#c_fixtablelayout").prop("checked");
cf.queryMessage = jq("#c_querymsg").prop("checked");
cf.queryMessageInterval = jq("#i_querymsginterval").val();
cf.rememberPage = jq("#c_rempage").prop("checked");
cf.animateGoToTopButton = jq("#c_fixgoupbtn").prop("checked");
cf.pinnedTopBar = jq("#c_fixtopbar").prop("checked");
cf.fixTopBarPopMenu = jq("#c_fixpopmenu").prop("checked");
cf.hoverPreviewTheme = jq("#c_hoverpreview").prop("checked");
cf.hoverableMedal = jq("#c_glassmedal").prop("checked");
cf.ljyysSearch = jq("#c_ljyyssearch").prop("checked");
setcookie("MExt_config", JSON.stringify(cf), 2147483647);
dlg("Config cookie set.");
setTimeout(function () {
showDialog("设置已保存,刷新生效", "right");
});
},
true,
function () {},
"MCBBS Extender v1.0.4"
);
jq("#c_fixcodeblock").prop("checked", conf.fixCodeBlock);
jq("#c_fixcopycode").prop("checked", conf.fixCodeCopy);
jq("#c_fixtablelayout").prop("checked", conf.fixTableLayout);
jq("#c_querymsg").prop("checked", conf.queryMessage);
jq("#i_querymsginterval").val(conf.queryMessageInterval);
jq("#c_rempage").prop("checked", conf.rememberPage);
jq("#c_fixgoupbtn").prop("checked", conf.animateGoToTopButton);
jq("#c_fixtopbar").prop("checked", conf.pinnedTopBar);
jq("#c_fixpopmenu").prop("checked", conf.fixTopBarPopMenu);
jq("#c_hoverpreview").prop("checked", conf.hoverPreviewTheme);
jq("#c_glassmedal").prop("checked", conf.hoverableMedal);
jq("#c_ljyyssearch").prop("checked", conf.ljyysSearch);
dlg("Config cookie loaded.");
});
dlg("Config button event attached.");
});
if (conf.fixCodeBlock) {
jq("head").append("");
dlg("Code block fix style appended.");
}
if (conf.fixTableLayout) {
document.onkeydown = (e) => {
if (e.shiftKey && e.keyCode == 70 && jq("#fixTableLayout").length == 0) {
dlg("Table layout fix actived");
showDialog("是否尝试修复此页表格?", "confirm", "MCBBS Extender", function () {
jq("head").append("");
})
}
};
dlg("Table layout fix event attached.");
}
if (conf.fixCodeCopy) {
copycode = function (obj) {
if (!obj) {
dlg("Code copy with invalid object.");
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, '代码已复制到剪贴板');
dlg("Code copied.");
};
dlg("Code copy fix actived.");
}
if (conf.queryMessage) {
var queryId = hash(new Date().getTime().toLocaleString(), 16);
dlg("Query id is " + queryId + ".");
if (!window.Html5notification) {
jq.getScript("data/cache/html5notification.js?xm6");
dlg("Html5notification added.");
}
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) && getcookie("MExt_ActiveQueryId") != queryId) {
setcookie("MExt_ActiveQueryId", queryId, 0);
dlg("Kick off inactive querier,start query.");
}
if(getcookie("MExt_ActiveQueryId") == queryId){
setcookie("MExt_LastQuery", nowtime, 0);
}
}, 1000);
dlg("Running checker actived.");
setInterval(function () {
dlg("Start to check message.");
localStorage.setItem('notifica-time', new Date().getTime());
jq.getJSON("/api/mobile/index.php?module=profile", function (data) {
var notices = data.Variables.notice;
var noticecontent = JSON.stringify(notices);
jq.get("/forum.php?mod=misc", function (d) {
var dom = jq(d);
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 = dom.filter("script[src*=html5notification]").nextUntil("div").last().text();
if (getcookie("MExt_ActiveQueryId") == queryId && noticecontent != getcookie("MExt_LastNoticeContent")) {
localStorage.setItem('notifica-time', 1);
eval(s);
dlg("Notification sent.");
}
setcookie("MExt_LastNoticeContent", noticecontent, 0);
});
});
}, conf.queryMessageInterval * 1000);
dlg("Start query messages.");
}
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();
};
}
dlg("Page remember actived.");
});
}
if (conf.animateGoToTopButton) {
jq("head").append("");
dlg("Animate go to top buttom style appended.");
showTopLink = function () {
var ft = $('ft');
if (ft) {
var scrolltop = $('scrolltop');
if (!scrolltop) {
return false;
}
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();
dlg("Animate go to top buttom actived.");
}
if (conf.pinnedTopBar) {
jq("head").append("");
dlg("Pinned top bar style appended.");
}
if (conf.fixTopBarPopMenu) {
jq("head").append("");
dlg("Pop menu fix style appended.");
var __extstyle = extstyle;
extstyle = function (style) {
__extstyle(style);
jq("#fixTopBarPopMenuWinter").remove();
if (style == "./template/mcbbs/style/winter") {
jq("head").append("");
}
}
var theme = getcookie('extstyle');
if (theme == "./template/mcbbs/style/winter") {
jq("head").append("");
}
dlg("Overwrite extstyle function");
}
if (conf.hoverPreviewTheme) {
jq(document).ready(function () {
jq(".sslct_btn").on("mouseenter",function(){
var that = this;
var timer = setTimeout(function(){
clearTimeout(timer);
var previewstyle = getcookie('extstyle');
jq(that).trigger('click');
setcookie('extstyle', previewstyle);
},300);
});
jq(".sslct_btn").on("mouseleave", function () {
extstyle(getcookie('extstyle'));
});
dlg("Hover preview theme event attached.");
});
}
if (conf.hoverableMedal) {
var rewriteMedal = function () {
jq('.md_ctrl:not([glassmedal])').attr("glassmedal","true").each(function (t, v) {
jq(v).children(0).children('img').each(function (b, n) {
var id = 'md' + new RegExp('\\_\\d*$').exec(jq(n).attr('id'))[0];
jq(v).append(
jq(
jq('').on('mouseover', function () {
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();
});
});
dlg("Hoverable medal rewrote.");
};
jq(document).ready(rewriteMedal).ready(
function(){
var __ajaxpost = ajaxpost;
ajaxpost = function (formid, showid, waitid, showidclass, submitbtn, recall) {
var relfunc = function(){
if (typeof recall == 'function') {
recall();
} else {
eval(recall);
}
setTimeout(rewriteMedal,1000);
}
__ajaxpost(formid,showid,waitid,showidclass,submitbtn,relfunc);
}
}
);
jq("head").append("");
dlg("Hoverable medal style appended.");
}
if(conf.ljyysSearch){
jq(document).ready(function(){
jq("#scbar_form [type*=hidden]").remove();
jq("#scbar_txt").attr("name","search").css("background-color","#00000000");
jq(".scbar_type_td").html("ljyys搜索").css("background","url(https://www.mcbbs.net/template/mcbbs/image/scbar_txt.png) -94px center no-repeat").css("width","62px").css("cursor","pointer");
jq("#scbar_form").attr("method","get").attr("action","//search.ljyys.xyz/search.php");
dlg("ljyys search actived");
});
}
})();