MiM / BLU Comparison Forums on Similar page

// ==UserScript==
// @name         BLU Comparison Forums on Similar page
// @description  Show comparison forums on similar page
// @version      1.0
// @author       MiM
// @copyright    2024
// @license      MIT
// @match        https://blutopia.cc/torrents/similar/*
// @grant        GM_xmlhttpRequest
// @icon         https://blutopia.cc/favicon.ico
// @updateURL    https://openuserjs.org/meta/MiM/BLU_Comparison_Forums_on_Similar_page.meta.js
// @downloadURL  https://openuserjs.org/install/MiM/BLU_Comparison_Forums_on_Similar_page.user.js
// ==/UserScript==

const url_regex = /.*\/(?<media_type>\d+)\.(?<tmdb>\d+)$/;
const this_url = window.location.href;
const url_regex_match = this_url.match(url_regex).groups;
const media_type = url_regex_match.media_type;
const media_tmdb = url_regex_match.tmdb;
const forum_type_mapping = {
    '1' : 'movie', // Movies
    '2' : 'series', // TV Series
    '3' : 'movie', // Fanres Movie
};
const forum_id_mapping = {
    '1' : 74, // Movies
    '2' : 75, // TV Series
    '3' : 76, // Fanres Movie
}
var url = '';


async function search_requests() {
  url = `https://blutopia.cc/forums/topics?search=%5B${forum_type_mapping[media_type]}%2F${media_tmdb}%5D&forumId=${forum_id_mapping[media_type]}`
  console.log(url);
  const headers = {
    "Content-Type": "application/html"
  };
  let resolver;
  let rejecter;
  const p = new Promise((resolveFn, rejectFn) => {
    resolver = resolveFn;
    rejecter = rejectFn;
  });
  const final = GM_xmlhttpRequest({
    method: "GET",
    url: url,
    headers: headers,
    onload: (response) => resolver(response),
    onerror: (response) => rejecter(response),
    ontimeout: (response) => rejecter(response)
  });
  return p;
}

async function createPanel() {
  const panel = document.createElement('section');
  panel.id = 'panel__requests';
  panel.classList.add('panel');

  let panel_type = "panel";
  let num_panels = document.getElementsByClassName(panel_type).length - 1;
  if (num_panels < 0) {
    panel_type = "panelV2";
    num_panels = document.getElementsByClassName(panel_type).length - 1;
  }
  let firstBlock = document.getElementsByClassName(panel_type)[num_panels];

  let forums_search = await search_requests();
  let forums_html = document.createElement('html');
  forums_html.innerHTML = forums_search.response;
  let panelV2_sections = forums_html.getElementsByClassName("panelV2");
  var forums_section = null;
  for (const req of panelV2_sections) {
    if (req.getElementsByClassName("panel__heading")[0].textContent == "Latest Topics") {
      req.getElementsByClassName("panel__heading")[0].textContent = 'Search More Comparisons - ';
      var comparison_search = document.createElement("a");
      var comparison_search_text = document.createTextNode(`[${forum_type_mapping[media_type]}/${media_tmdb}]`);
        comparison_search.appendChild(comparison_search_text);
        comparison_search.title = `[${forum_type_mapping[media_type]}/${media_tmdb}]`;
        comparison_search.href = url;
      req.getElementsByClassName("panel__heading")[0].appendChild(comparison_search);
      forums_section = req
    }
  }
  if (forums_section == null) {
    panel.innerHTML = `
    <div class="panelV2">
      <header class="panel__header">
        <h2 class="panel__heading">
           <i class="fa-solid fa-code-pull-request"></i>
           Requests
           &nbsp;&nbsp;&nbsp;
        </h2>
      </header>
      <div class="panel__body bbcode-rendered">
        <div class="panel__body ''">
        <span align=center>Searching requests failed.</span>
        </div>
      </div>
  </div>`;
  }
  else {
    panel.innerHTML = forums_section.innerHTML;
  }
  firstBlock.after(panel);
}

(async function () {
  'use strict';

  createPanel();
})();