MSPA link fixerer

Replaces MSPA indexed links to Homestuck with links to Homestuck.com.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

/* jshint esversion: 6 */
// ==UserScript==
// @name         MSPA link fixerer
// @version      1
// @description  Replaces MSPA indexed links to Homestuck with links to Homestuck.com.
// @author       EtchJetty
// @license MIT
// @match        http://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @namespace https://greasyfork.org/users/909980
// ==/UserScript==

(function () {
  "use strict";

  replaceLinks();

  // story6: 1900 (hs)
  // story5: 1892 (hs-beta)
  // story4: 218 (ps)
  // story3: n/a (midnight crew)
  // story2: 135 (bardquest)
  // story1: 1 (jb)

  function replaceLinks() {
    Array.from(document.getElementsByTagName("a")).forEach(function (a) {
      //  console.log(a.href);
      const regex = /(?:http.*mspaintadventures\.com\/.*|\?)s=\d.*p=(\d{1,6})/gm;
      const baseurl = `https://www.homestuck.com/`;
      const hssubst = baseurl + `story`;
      const hsbetasubst = baseurl + `beta`;
      const pssubst = baseurl + `problem-sleuth`;
      // const mcsubst = baseurl + `404`;
      const bqsubst = baseurl + `bard-quest`;
      const jbsubst = baseurl + `jailbreak`;

      // The substituted value will be contained in the result variable

      let m;

      const hrefex = a.href.toString().slice(a.href.toString().length - 4);
      if (hrefex.slice(0, -1) == "?s=") {
        //if this is just a story link, no page
        switch (parseInt(hrefex[hrefex.length - 1])) {
          case 6:
            a.href = hssubst;
            break;
          case 5:
            a.href = hsbetasubst;
            break;
          case 4:
            a.href = pssubst;
            break;
          case 3:
            a.href = "http://www.mspaintadventures.com/test_index.php?s=3"; // mc doesn't exist on homestuck.com as far as i know
            break;
          case 2:
            a.href = bqsubst;
            break;
          case 1:
            a.href = jbsubst;
            break;
        }
      } else {
        while ((m = regex.exec(a.href.toString())) !== null) {
          // This is necessary to avoid infinite loops with zero-width matches
          if (m.index === regex.lastIndex) {
            regex.lastIndex++;
          }

          var match = parseInt(m[1]); // get just the matched value
          switch (true) {
            case match > 1900:
              match = match - 1900;
              a.href = hssubst + "/" + match;
              break;

            case 1892 < match && match < 1900:
              match = match - 1892;
              a.href = hsbetasubst + "/" + match;
              break;

            case 218 < match && match < 1892:
              match = match - 218;
              a.href = pssubst + "/" + match;
              break;

            case 135 < match && match < 218:
              match = match - 135;
              a.href = bqsubst + "/" + match;
              break;

            case 1 < match && match < 135:
              match = match - 1;
              a.href = jbsubst + "/" + match;
              break;
          }
        }
      }
    });
  }
})();