m.newsmth.net cross link

add cross links between mobile, www2 and nForum views

当前为 2020-07-10 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name       m.newsmth.net cross link
// @description  add cross links between mobile, www2 and nForum views
// @include      https://*.newsmth.net/*
// @version 1.1
// @namespace https://greasyfork.org/users/5696
// @grant        none
// ==/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 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() {
  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;
  }
  
  // el = document.getElementById('ad_container').getElementsByClassName('sec sp')[0];
  var el = document.createElement('div');
  el.innerHTML = '<form action="/go" method="get"><span class="f">选择讨论区</span><br><input name="name" type="text">&nbsp;<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);
  //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 articles = document.getElementsByClassName('nav hl');
  var nav = document.getElementsByClassName('nav sec') [0];
  for (var i = 0; i < articles.length; i++) {
    match = /\/article\/\w+\/post\/(\d+)/.exec(articles[i].childNodes[1].childNodes[0].href);
    var start = match[1];
    if (i == 0) {
      nav.innerHTML = nav.innerHTML + '||' + buildWww2ThreadLink(board, gid, start, Math.ceil(parseInt(page, 0) / 2)) + '||' + buildNforumThreadLink(board, gid, page);
    }
    articles[i].childNodes[1].innerHTML += '|' + buildMobileArticleLink(board, start).replace('手机版', '单文');
  }
}
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);
}
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);
}
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 += '&ensp;&ensp;【' + 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 += '&ensp;&ensp;【' + 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\/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();
}
if (document.URL.match(/^https?:\/\/m\.newsmth\.net\/\W?/)) {
  patchMobile();
}