MiM / BLU update Blu-ray.com link

// ==UserScript==
// @name         BLU update Blu-ray.com link
// @description  Add link to Blu-ray.com when possible
// @version      1.2.1
// @author       MiM
// @copyright    2024
// @license      MIT
// @match        https://blutopia.cc/torrents/similar/*
// @match        https://blutopia.cc/torrents/*
// @match        https://blutopia.cc/requests/*
// @grant        GM_xmlhttpRequest
// @icon         https://blutopia.cc/favicon.ico
// @updateURL    https://openuserjs.org/meta/MiM/BLU_update_Blu-ray.com_link.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/MiM/BLU_update_Blu-ray.com_link.user.js
// @connect      blu-ray.com
// ==/UserScript==

const CACHE_TIME = 1000 * 60 * 60 * 24;

async function search_bluray_com(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;
}

function get_imdb_id() {
  let imdb_id = ''
  try {
    let imdb_url = document.getElementsByClassName("meta__imdb")[0].getElementsByTagName("a")[0].href;
    let url_regex = /^(.*)(tt\d+)([^\d]*)$/;
    imdb_id = imdb_url.match(url_regex)[2];
  }
  catch {}
  return imdb_id;
}

function remove_bd_meta() {
  try {
    window.localStorage.removeItem(`bluray_com-${imdb_id}`);
  }
  catch {}
  document.getElementsByClassName("meta__bluray")[0].remove();
  console.log("Failed to find Blu-ray.com link for this page.");
}

(async function () {
  'use strict';
  let bluray_com_meta = document.getElementsByClassName("meta__bluray")[0];
  let bluray_com_search_url_tag = bluray_com_meta.getElementsByTagName("a")[0];
  let bluray_com_search_url = bluray_com_search_url_tag.href;
  let imdb_id = get_imdb_id();
  var bluray_com_link = '';
  try {
    let updateCache = true;
    try {
      let oldTime = JSON.parse(window.localStorage.getItem(`bluray_com-${imdb_id}`)).time
      let difference = (new Date).getTime() - oldTime;
      updateCache = difference > CACHE_TIME
    }
    catch {}
    if (updateCache) {
      try {
        window.localStorage.removeItem(`bluray_com-${imdb_id}`);
      }
      catch {}
      let bluray_com_search = await search_bluray_com(bluray_com_search_url);
      if (bluray_com_search.status == 200) {
        let bluray_com_search_html = document.createElement('html');
        bluray_com_search_html.innerHTML = bluray_com_search.response;
        try {
          bluray_com_link = bluray_com_search_html.getElementsByClassName("alphaborder hoverlink")[0].href;
        }
        catch {
          bluray_com_link = ''
        }
      }
      else {
        try {
          window.localStorage.removeItem(`bluray_com-${imdb_id}`);
        }
        catch {}
      }
    }
    else {
      bluray_com_link = JSON.parse(window.localStorage.getItem(`bluray_com-${imdb_id}`)).Bluray_com_link
    }
    if (bluray_com_link != '') {
      document.getElementsByClassName("meta__bluray")[0].getElementsByTagName("a")[0].href = bluray_com_link;
    }
    else {
      remove_bd_meta();
    }
    window.localStorage.setItem(`bluray_com-${imdb_id}`, JSON.stringify({
      'time': (new Date).getTime(),
      'Bluray_com_link': bluray_com_link
    }));
  }
  catch {
    remove_bd_meta();
  }
})();