Debrid Media Manager

Add stream buttons to IMDB and MDBList pages

目前為 2023-09-02 提交的版本,檢視 最新版本

// ==UserScript==
// @name Debrid Media Manager
// @namespace https://debridmediamanager.com
// @version 1.0.4
// @description Add stream buttons to IMDB and MDBList pages
// @author Ben Adrian Sarmiento <[email protected]>
// @license MIT
// @match https://www.imdb.com/title/*
// @match https://www.imdb.com/list/ls*
// @match https://www.imdb.com/search/title/*
// @match https://mdblist.com/*
// @grant none
// ==/UserScript==

(function () {
	"use strict";
	const DMM_HOST = "https://debridmediamanager.com";
	const SEARCH_BTN_LABEL = "DMM🔎";
	const TV_BTN_LABEL = "TV?";

	function createButton(text, url) {
		const button = document.createElement("button");
		button.textContent = text;
		button.style.marginLeft = "10px";
		button.onclick = (event) => {
			event.preventDefault();
			window.open(url, "_blank");
		};
		return button;
	}

	function addButtonToElement(element, text, url) {
		const button = createButton(text, url);
		element.appendChild(button);
	}

	// IMDB functions
	function addButtonsToIMDBSingleTitle() {
		const scriptTag = document.getElementById("__NEXT_DATA__");
		const jsonData = JSON.parse(scriptTag.innerHTML);
		let link = document.querySelector('link[rel="canonical"]').href;
		let imdbId = link.split("/").slice(-2, -1)[0];
		let episodeSpan = document.querySelector("h3.ipc-title__text > span");
		const isTV = episodeSpan && episodeSpan.innerText === "Episodes";

		const targetElement = document.querySelector(
			"section.ipc-page-background h1 > span"
		);
		if (targetElement) {
			const searchUrl = `${DMM_HOST}/${isTV ? `show` : `movie`}/${imdbId}`;
			addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
		}
	}

	function addButtonsToIMDBList() {
		const items = document.querySelectorAll("div.lister-item-content > h3");

		items.forEach((item) => {
			const [rawTitle, yearRange] = item.innerText.split(" (");
			const title = rawTitle.replace(/^\d+\.\s*/, "").replace("&", "");

			let isTV = false;
			if (yearRange.includes("-") || yearRange.includes("–")) isTV = true;

			let link = item.querySelector('a[href^="/title/"]').href;
			let imdbId = link.split("/").slice(-2, -1)[0];

      const searchUrl = `${DMM_HOST}/${isTV ? `show` : `movie`}/${imdbId}`;

			addButtonToElement(item, SEARCH_BTN_LABEL, searchUrl);

      let cert = item.parentElement.querySelector("span.certificate")?.innerText;
      if (isTV || (typeof cert === "string" && cert !== "Not Rated" && !cert.includes("TV"))) {
        // do nothing...
      } else {
        addButtonToElement(item, TV_BTN_LABEL, searchUrl.replace('/movie/', '/show/'));
      }
		});
	}

	// MDBList functions
	function addButtonsToMDBListSingleTitle() {
		const targetElement = document.querySelector(
			"#content-desktop-2 > div > div:nth-child(1) > h3"
		);
		if (targetElement) {
			const searchUrl = `${DMM_HOST}${window.location.pathname}`;
			addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
		}
	}

	function addButtonsToMDBListSearchResults() {
		const items = document.querySelectorAll("div.ui.centered.cards > div");
		items.forEach((item) => {
			const targetElement = item.querySelector("div.header");
			if (targetElement) {
        const url = targetElement.parentElement.querySelector("a").href.replace('https://mdblist.com/', '');
				const searchUrl = `${DMM_HOST}/${url}`;
				addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
			}
		});
	}

	const hostname = window.location.hostname;

	if (hostname === "www.imdb.com") {
		const isIMDBSingleTitlePage = /^\/title\//.test(location.pathname);
		const isIMDBListPage =
			/^\/search\//.test(location.pathname) ||
			/^\/list\/ls/.test(location.pathname);

		if (isIMDBSingleTitlePage) {
			addButtonsToIMDBSingleTitle();
		} else if (isIMDBListPage) {
			addButtonsToIMDBList();
		}
	} else if (hostname === "mdblist.com") {
		const isMDBListSingleTitlePage = /^\/(movie|show)\//.test(
			location.pathname
		);

		if (isMDBListSingleTitlePage) {
			addButtonsToMDBListSingleTitle();
		} else {
			addButtonsToMDBListSearchResults();
		}
	}
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址