nastykast / Jump to last read by default

// ==UserScript==
// @name        Jump to last read by default
// @match       http*://redacted.sh/forums.php
// @match       http*://passthepopcorn.me/forums.php
// @match       http*://gazellegames.net/forums.php
// @match       http*://orpheus.network/forums.php
// @grant       none
// @version     1.2
// @author      nastykast
// @license     MIT
// @updateURL   https://openuserjs.org/meta/nastykast/Jump_to_last_read_by_default.meta.js
// ==/UserScript==

(function () {
  'use strict';
  let last_read_class = "last_read";
  switch (window.location.host) {
    case "passthepopcorn.me":
      last_read_class = "forum-topic__go-to-last-read";
      break;
  }
  let topics = document.getElementsByClassName("last_topic");
  topics = [...topics].map(function (topic) {
    return topic.parentElement;
  });

  topics.forEach(topic => {
    const last_read_array = topic.getElementsByClassName(last_read_class);
    if (last_read_array.length == 0) {
      return;
    }
    let last_read = last_read_array[0];
    if (last_read.tagName != "A") {
      last_read = last_read.getElementsByTagName("A")[0];
    }
    let thread_title = topic.getElementsByClassName("last_topic")[0].firstElementChild;
    if (thread_title.tagName != "A") {
      thread_title = thread_title.getElementsByTagName("A")[0];
    }
    thread_title.href = last_read.href;
  });
})();