豆瓣电影rarbg种子预览器 douban_IMDb_torrent_search

use IMDb_id to search torrent on rarbg and render back to the movie page

// ==UserScript==
// @name         豆瓣电影rarbg种子预览器 douban_IMDb_torrent_search
// @namespace    https://github.com/ned42
// @version      0.1.1
// @description  use IMDb_id to search torrent on rarbg and render back to the movie page
// @author       ned42
// @match        https://movie.douban.com/subject/*
// @grant        GM.xmlHttpRequest
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==


(async function () {
  'use strict';
  const IMDb_tag = Array(...document.querySelectorAll("span.pl")).filter(node => node.textContent == 'IMDb:');
  let IMDb_id = IMDb_tag[0] ? IMDb_tag[0].nextSibling.textContent.trim() : '';
  if (IMDb_id == '') {
    console.log('IMDb_id not found');
    return;
  }

  // support multi site with format
  const search_site = [
    {
      "host": "https://rarbgunblocked.org",
      "url": `/torrents.php?search=${IMDb_id}&order=size&by=DESC`,
      "target_tb": ".lista2t tbody tr",
      "info_index": [1, 3, 4, 5],
    }];

  // fetch url with DOM response
  const my_fetch = (url) => {
    return new Promise((resolve, reject) => {
      GM.xmlHttpRequest({
        method: "GET",
        url: url,
        onload: (response) => {
          resolve(new DOMParser().parseFromString(response.responseText, "text/html"));
        }
      });
    });
  };

  // html table code
  const table_render = (res_list) => {
    let table_content = "";
    res_list.forEach((node) => {
      table_content += `<tr><td>${node.seeder}-${node.leecher}</td><td style="display: inline-flex;"><a style="width:550px;overflow: hidden;text-overflow:ellipsis;white-space:nowrap;" href="${node.link}">${node.name}</a></td><td>${node.size}</td></tr>`;
    });
    return `<div id="megF"><div style="width:700px;"><table>${table_content}</table></div></div>`;
  };

  // fetch site and get torrent info
  const table_scrap = search_site.map(async (site) => {
    return await my_fetch(site.host + site.url).then((fetch_dom) => {
      let scrap_res = [];
      let t_table = {};
      let tds, link;
      for (let o_table of fetch_dom.querySelectorAll(site.target_tb)) {
        if (o_table.querySelectorAll('a').length != 5) {
          t_table = {}; // table wrapper for rendering
          tds = o_table.querySelectorAll('td'); // original table data on rarbg
          t_table.name = tds[site.info_index[0]].querySelector('a[href^="/torrent/"]').innerText;
          link = site.host + tds[site.info_index[0]].querySelector('a[href^="/torrent/"]').getAttribute('href');
          t_table.link = link;
          t_table.size = tds[site.info_index[1]].innerText;
          t_table.seeder = tds[site.info_index[2]].innerText;
          t_table.leecher = tds[site.info_index[3]].innerText;
          scrap_res.push(t_table);
        }
      }
      return scrap_res;
    });
  });

  // main
  let res_list = await Promise.all(table_scrap);
  res_list = [].concat(...res_list);
  if (res_list.length > 0) {
    document.querySelector(".subjectwrap").innerHTML += table_render(res_list);
    GM_addStyle(`
          megF, td, th {
              font: 12px Hiragino Sans,Arial,sans-serif;
                  font-size: 12px;
                  line-height: normal;
              line-height: 1.62;
          }
      `);
  }
})();