MiM / BLU Requests on Similar

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

const only_unfilled = false;

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;

async function search_requests() {
  const url = `https://blutopia.cc/requests?tmdbId=${media_tmdb}&categories[0]=${media_type}${only_unfilled ? '&unfilled=1' : ''}`;
  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 requests_search = await search_requests();
  let requests_html = document.createElement('html');
  requests_html.innerHTML = requests_search.response;
  let panelV2_sections = requests_html.getElementsByClassName("panelV2");
  var requests_section = null;
  for (const req of panelV2_sections) {
    console.log(req.getElementsByClassName("panel__heading")[0].textContent);
    if (req.getElementsByClassName("panel__heading")[0].textContent == "Requests") {
      requests_section = req
    }
  }
  console.log(requests_section == null);
  console.log(requests_section);
  if (requests_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 = requests_section.innerHTML;
  }
  firstBlock.after(panel);
}

(async function () {
  'use strict';

  createPanel();
})();