// ==UserScript==
// @name m.newsmth.net cross link
// @description add cross links between mobile, www2 and nForum views
// @include https://*.newsmth.net/*
// @version 1.2
// @namespace https://gf.qytechs.cn/users/5696
// @grant none
// @require https://code.jquery.com/jquery-3.5.1.min.js
// ==/UserScript==
function buildMobileArticleBoardLink(board, page) {
page = (page !== null) ? '?p=' + page : '';
return '<a href="https://m.newsmth.net/board/' + board + '/0' + page + '">手机版</a>';
}
function buildWww2ArticleBoardLink(board) {
return '<a href="https://www.newsmth.net/bbsdoc.php?board=' + board + '&ftype=0">www2</a>';
}
function buildWww2BoardSearch(board) {
return '<a href="https://www.newsmth.net/bbsbfind.php?board=' + board + '">搜索</a>';
}
function buildMobileArticleLink(board, tid) {
return '<a href="https://m.newsmth.net/article/' + board + '/single/' + tid + '/0">手机版</a>';
}
function buildWww2ArticleLink(bid, tid) {
return '<a href="https://www.newsmth.net/bbscon.php?bid=' + bid + '&id=' + tid + '">www2</a>';
}
function buildMobileThreadLink(board, gid, start, page) {
if (start !== null) {
return '<a href="https://m.newsmth.net/article/' + board + '/' + gid + '/?s=' + start + '">手机版</a>';
}
page = (page !== null) ? '/?p=' + page : '';
return '<a href="https://m.newsmth.net/article/' + board + '/' + gid + page + '">手机版</a>';
}
function buildWww2ThreadLink(board, gid, start, page) {
start = (start !== null) ? '&start=' + start : '';
page = (page !== null) ? '&pno=' + page : '';
return '<a href="https://www.newsmth.net/bbstcon.php?board=' + board + '&gid=' + gid + start + page + '">www2</a>';
}
function buildNforumThreadLink(board, tid, page) {
page = (page !== null && !isNaN(page)) ? '?p=' + page : '';
return '<a href="https://www.newsmth.net/nForum/#!article/' + board + '/' + tid + page + '">nForum</a>';
}
function buildMobileThreadBoardLink(board, page) {
page = (page !== null) ? '/?p=' + page : '';
return '<a href="https://m.newsmth.net/board/' + board + page + '">手机版</a>';
}
function buildWww2ThreadBoardLink(board) {
return '<a href="https://www.newsmth.net/bbsdoc.php?board=' + board + '&ftype=6">www2</a>';
}
function buildNforumThreadBoardLink(board, page) {
page = (page !== null) ? '?p=' + page : '';
return '<a href="https://www.newsmth.net/nForum/#!board/' + board + page + '">nForum</a>';
}
function patchMobile() {
var e = $(".sp.hl.f");
if (e) {
if ( e.text() == "您没有绑定手机号码,没有发表文章的权限"
|| e.text() == "您无权阅读此版面"
) {
$form = $('<form id="loginform" action="/user/login" method="post"></form>');
$form.append('用户: <input id="userid" name="id" type="input" value=""><br>密码: <input id="passwd" name="passwd" type="password"><br>');
$form.append('<input id="refresh" type="button" value="刷新">');
e.append($form);
$("#refresh").click(function() {
$("#refresh").attr("value", "正在登陆");
$.ajax({
url: "https://m.newsmth.net/user/login",
method: "POST",
data: $('form').serialize(),
complete: function() { window.location.href = window.location.href+(window.location.href.includes("?")?"&":"?") + "reload="+Date.parse(new Date()); },
error: function(xhr,status,error) { console.log(status); alert(error); },
success: function(doc, status, xhr) { $("#refresh").attr("value", "登陆成功"); }
});
});
return
}
}
if(!$("#id").length) {
if(!$(".menu.nav").children("a[href*='logout']").length) {
$form = $('<form id="loginform" action="/user/login" method="post"></form>');
$form.append('用户: <input id="userid" name="id" type="input" value="">密码: <input id="passwd" name="passwd" type="password">');
$form.append('<input id="refresh" type="button" value="刷新">');
$(".menu.nav").prepend($form);
$("#refresh").click(function() {
$("#refresh").attr("value", "正在登陆");
$.ajax({
url: "https://m.newsmth.net/user/login",
method: "POST",
data: $('form').serialize(),
complete: function() { window.location.href = window.location.href+(window.location.href.includes("?")?"&":"?") + "reload="+Date.parse(new Date());},
error: function(xhr,status,error) { console.log(status); alert(error); },
success: function(doc, status, xhr) { $("#refresh").attr("value", "登陆成功"); }
});
});
}
}
e = document.getElementsByClassName('menu nav') [0];
if (e.children.length == 9) {
var str = '';
if (e.children[4].innerHTML.length > 2)
str += e.children[4].outerHTML + '|';
if (e.children[5].innerHTML.length > 2)
str += e.children[5].outerHTML + '|';
if (e.children[6].innerHTML.length > 2)
str += e.children[6].outerHTML;
if (str.length > 0)
document.getElementsByClassName('sec nav') [0].innerHTML += '||' + str;
}
var el = document.createElement('div');
el.innerHTML = '<form action="/go" method="get"><span class="f">选择讨论区</span><br><input name="name" type="text"> <input value="GO" class="btn" type="submit"></form>';
el.classList.add("sec");
el.classList.add("sp");
document.getElementById('m_main').appendChild(el);
s = document.getElementsByTagName('script');
for (var i = 1; i < s.length; i++) {
s[i].parentNode.removeChild(s[i]);
}
var slist = document.getElementsByClassName('slist sec') [1];
var linklist = {
links: [],
ptr: -1
};
var httpRequest = new XMLHttpRequest();
httpRequest.responseType = 'document';
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
if(linklist.ptr < 0)
{
linklist.links = httpRequest.response.getElementsByClassName('nav sec') [0].children;
linklist.ptr++;
}
else
{
var slist2 = httpRequest.response.getElementsByClassName('slist sec') [0];
slist.parentNode.insertBefore(slist2, slist.nextSibling);
slist = slist2;
}
if (linklist.ptr >= linklist.links.length)
{
return;
}
linklist.ptr++;
var url = "https://m.newsmth.net" + linklist.links[linklist.ptr].getAttribute('href');
httpRequest.open('GET', url, true);
httpRequest.send();
}
};
var url = "https://m.newsmth.net/hot";
httpRequest.open('GET', url, true);
httpRequest.send();
}
function patchMobileThreadBoard() {
var match = /https?:\/\/m\.newsmth\.net\/board\/(\w+)(\/?\?p=(\d+))?/.exec(document.URL);
var board = match[1];
var page = match[3];
var nav = document.getElementsByClassName('nav sec') [0];
nav.innerHTML = nav.innerHTML + '||' + buildWww2ThreadBoardLink(board) + '||' + buildNforumThreadBoardLink(board, page) + '||' + buildWww2BoardSearch(board);
//list
list = document.getElementsByClassName('list sec') [0].children;
for (var i = 0; i < list.length; i++) {
li = list[i];
t = li.children[0];
a = li.children[1];
d = a.textContent.substring(0, 10);
var ta1 = t.childNodes[0];
var ta2 = document.createElement('a');
var ta3 = a.children[0];
var ta4 = a.children[1];
var span = document.createElement('span');
ta1.title = li.textContent;
if(d.match(/^\d{4}-\d{2}-\d{2}$/) && Date.now()-Date.parse(d) > 7*24*60*60*1000) {
ta1.text = "(坟)"+ta1.text;
}
ta2.text = t.childNodes[1].textContent;
ta2.href = ta1.href + '?p=' + Math.floor(parseInt(/\((\d+)\)/.exec(t.childNodes[1].textContent) [1]) / 10 + 1);
t.replaceChild(ta2, t.childNodes[1]);
t.appendChild(span);
span.appendChild(ta3);
span.appendChild(ta4);
li.removeChild(a);
}
}
function patchMobileThread() {
var match = /https?:\/\/m\.newsmth\.net\/article\/(\w+)\/(\d+)(\?p=(\d+))?/.exec(document.URL);
var board = match[1];
var gid = match[2];
var page = match[4];
var start = 0;
$('.nav.hl').each(function() {
$(this).find("a[href*='/forward/']").each(function() {
match = /\/\w+\/forward\/(\d+)/.exec($(this).attr("href"));
start = match[1];
$(this).append('|' + buildMobileArticleLink(board, start).replace('手机版', '单文'))
});
});
$(".nav.sec").first().append('||' + buildWww2ThreadLink(board, gid, start, Math.ceil(parseInt(page, 0) / 2)) + '||' + buildNforumThreadLink(board, gid, page) + '||' + buildWww2BoardSearch(board));
}
function patchMobileArticle() {
var match = /https?:\/\/m\.newsmth\.net\/article\/(\w+)\/(\d+)\?s=(\d+)/.exec(document.getElementsByClassName('sec nav') [0].childNodes[2].href);
var board = match[1];
var gid = match[2];
var start = match[3];
var nav = document.getElementsByClassName('nav sec') [0];
nav.innerHTML = nav.innerHTML + '||' + buildWww2ThreadLink(board, gid, start) + '||' + buildNforumThreadLink(board, gid) + '||' + buildWww2BoardSearch(board);
}
function patchMobileArticleBoard() {
var match = /https?:\/\/m\.newsmth\.net\/board\/(\w+)\/0(\?p=(\d+))?/.exec(document.URL);
var board = match[1];
var page = match[3];
var nav = document.getElementsByClassName('nav sec') [0];
nav.innerHTML = nav.innerHTML + '||' + buildWww2ArticleBoardLink(board) + '||' + buildNforumThreadBoardLink(board) + '||' + buildWww2BoardSearch(board);
}
function patchMobilePost() {
var btns = document.getElementsByClassName('btn') [0].parentElement;
var button = document.createElement('input');
button.type = 'button';
button.value = 'IMG';
button.className = 'btn';
button.onclick = function () {
var textarea = document.getElementsByName('content') [0];
var newvalue = textarea.value.substring(0, textarea.selectionStart);
newvalue += '[img=';
newvalue += textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
newvalue += '][/img]'
newvalue += textarea.value.substring(textarea.selectionEnd);
textarea.value = newvalue;
}
var button2 = button.cloneNode();
button2.value = 'SWF';
button2.onclick = function () {
var textarea = document.getElementsByName('content') [0];
var newvalue = textarea.value.substring(0, textarea.selectionStart);
newvalue += '[swf=';
newvalue += textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
newvalue += '][/swf]'
newvalue += textarea.value.substring(textarea.selectionEnd);
textarea.value = newvalue;
}
btns.appendChild(document.createTextNode(' '));
btns.appendChild(button);
btns.appendChild(document.createTextNode(' '));
btns.appendChild(button2);
}
function patchWww2Thread() {
var match = /https?:\/\/www\.newsmth\.net\/bbstcon\.php\?board=(\w+)&gid=(\d+)(&start=\d+)?(&pno=(\d+))?/.exec(document.URL);
var board = match[1];
var gid = match[2];
var page = match[5];
var nav = document.getElementsByClassName('tnav smaller') [0].childNodes[0];
nav.innerHTML = '[' + buildMobileThreadLink(board, gid, null, parseInt(page) * 2) + '][' + buildNforumThreadLink(board, gid, parseInt(page) * 2) + ']' + nav.innerHTML;
}
function patchWww2ThreadBoard() {
var match = /https?:\/\/www\.newsmth\.net\/bbsdoc\.php\?board=(\w+)/.exec(document.URL);
var board = match[1];
var nav = document.getElementsByClassName('docTab smaller') [0];
nav.innerHTML += buildNforumThreadBoardLink(board) + buildMobileThreadBoardLink(board);
}
function patchWww2Article() {
var nav = document.getElementsByClassName('oper smaller') [0];
var match = /bbstcon\.php\?board=(\w+)&gid=(\d+)/.exec(nav.childNodes[1].href);
var board = match[1];
var gid = match[2];
match = /https?:\/\/www\.newsmth\.net\/bbscon\.php\?bid=\d+&id=(\d+)/.exec(document.URL);
var start = match[1];
nav.innerHTML = nav.innerHTML.replace('<br>', '<br>[' + buildMobileArticleLink(board, start) + '][' + buildNforumThreadLink(board, gid) + ']');
}
function patchWww2ArticleBoard() {
var match = /https?:\/\/www\.newsmth\.net\/bbsdoc\.php\?board=(\w+)/.exec(document.URL);
var board = match[1];
var nav = document.getElementsByClassName('docTab smaller') [0];
nav.innerHTML += buildNforumThreadBoardLink(board) + buildMobileArticleBoardLink(board);
}
function patchNforumThread() {
var match = /https?:\/\/www\.newsmth\.net\/nForum\/#!article\/(\w+)\/(\d+)(\?p=(\d+))?/.exec(document.URL);
var board = match[1];
var gid = match[2];
var page = match[4];
var nav = document.getElementById('notice');
nav.innerHTML += '  【' + buildMobileThreadLink(board, gid, null, page).replace('href', 'onclick="javascript: window.location=this.href" href') + '】【' + buildWww2ThreadLink(board, gid, null, Math.ceil(parseInt(page, 0) / 2)).replace('href', 'onclick="javascript: window.location=this.href" href') + '】';
}
function patchNforumThreadBoard() {
var match = /https?:\/\/www\.newsmth\.net\/nForum\/#!board\/(\w+)(\/?\?p=(\d+))?/.exec(document.URL);
var board = match[1];
var page = match[3];
var nav = document.getElementById('notice');
nav.innerHTML += '  【' + buildMobileThreadBoardLink(board, page).replace('href', 'onclick="javascript: window.location=this.href" href') + '】【' + buildWww2ThreadBoardLink(board).replace('href', 'onclick="javascript: window.location=this.href" href') + '】';
}
function patchMobileHot() {
var nav = document.getElementsByClassName('nav sec') [0];
var slist = document.getElementsByClassName('slist sec') [0];
var linklist = {
links: nav.children,
ptr: 0
};
var httpRequest = new XMLHttpRequest();
httpRequest.responseType = 'document';
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
var slist2 = httpRequest.response.getElementsByClassName('slist sec') [0];
slist.parentNode.insertBefore(slist2, slist.nextSibling);
slist = slist2;
if (linklist.ptr >= linklist.links.length) {
return;
}
linklist.ptr++;
var url = "https://m.newsmth.net" + linklist.links[linklist.ptr].getAttribute('href');
httpRequest.open('GET', url, true);
httpRequest.send();
}
};
linklist.ptr++;
var url = "https://m.newsmth.net" + linklist.links[linklist.ptr].getAttribute('href');
httpRequest.open('GET', url, true);
httpRequest.send();
}
if (document.URL.match(/^https?:\/\/m\.newsmth\.net\/\W?/)) {
patchMobile();
}
if($(".menu.sp").text().includes("发生错误")) {
// do nothing
} else if (document.URL.match(/https?:\/\/wap\.newsmth\.net\/index/)) {
window.location.href = "https://m.newsmth.net/index";
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/article\/\w+\/(\d+)/)) {
patchMobileThread();
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/board\/\w+\/0(\?p=(\d+))?/)) {
patchMobileArticleBoard();
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/board\/\w+(p=(\d+))?/)) {
patchMobileThreadBoard();
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/article\/\w+\/single\/(\d+)/)) {
patchMobileArticle();
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/article\/\w+\/post/)) {
patchMobilePost();
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/article\/\w+\/edit/)) {
patchMobilePost();
} else if (document.URL.match(/https?:\/\/www\.newsmth\.net\/bbstcon\.php\?board=(\w+)&gid=(\d+)/)) {
patchWww2Thread();
} else if (document.URL.match(/https?:\/\/www\.newsmth\.net\/bbsdoc\.php\?board=(\w+)&ftype=6/)) {
patchWww2ThreadBoard();
} else if (document.URL.match(/https?:\/\/www\.newsmth\.net\/bbsdoc\.php\?board=(\w+)/)) {
patchWww2ArticleBoard();
} else if (document.URL.match(/https?:\/\/www\.newsmth\.net\/bbscon\.php\?bid=\d+&id=(\d+)/)) {
patchWww2Article();
} else if (document.URL.match(/https?:\/\/www\.newsmth\.net\/nForum\/#!article\/(\w+)\/(\d+)/)) {
patchNforumThread();
} else if (document.URL.match(/https?:\/\/www\.newsmth\.net\/nForum\/#!board\/(\w+)/)) {
patchNforumThreadBoard();
} else if (document.URL.match(/https?:\/\/m\.newsmth\.net\/hot/)) {
patchMobileHot();
}