豆瓣直达片源网

在豆瓣电影页面新增一个按钮直达片源网搜索结果

目前為 2024-02-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name         豆瓣直达片源网
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  在豆瓣电影页面新增一个按钮直达片源网搜索结果
// @author       JSSM
// @match        *://movie.douban.com/subject/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    //感谢chatgpt帮我完成100%的代码
    // Find the #info div
    const infoDiv = document.getElementById('info');

    // Get the IMDb ID by looking for a span with class 'pl' followed by a text node containing the ID
    let imdbIdElement = null;
    const plSpans = infoDiv.querySelectorAll('.pl');
    for (let i = 0; i < plSpans.length; i++) {
        const currentSpan = plSpans[i];
        if (currentSpan.textContent.includes('IMDb:')) {
            const nextNode = currentSpan.nextSibling;
            if (nextNode && nextNode.nodeType === Node.TEXT_NODE) {
                const imdbIdCandidate = nextNode.textContent.trim();
                if (imdbIdCandidate.startsWith('tt')) {
                    imdbIdElement = { span: currentSpan, idNode: nextNode };
                    break;
                }
            }
        }
    }

    if (imdbIdElement) {
        const imdbId = imdbIdElement.idNode.textContent.trim();

        // Output IMDb ID to the console
        console.log('IMDb ID:', imdbId);

        // Create the search link
        const searchUrl = 'https://pianyuan.org/search?q=' + encodeURIComponent(imdbId);

        // Create the new span with "片源网:"
        const newLineBreak = document.createElement('br');
        const newSpan = document.createElement('span');
        newSpan.classList.add('pl');
        newSpan.innerText = '片源网: ';

        // Create the clickable link inside the new span
        const newLink = document.createElement('a');
        newLink.href = searchUrl;
        newLink.target = '_blank';
        newLink.rel = 'noopener noreferrer';
        newLink.innerText = '一键跳转'; // or any other desired text for the link

        // Append the link to the new span
        newSpan.appendChild(newLink);

        // Insert the new span after the IMDb ID text node
        imdbIdElement.span.parentNode.insertBefore(newLineBreak, imdbIdElement.idNode.nextSibling); // Assuming you still want a line break here
        imdbIdElement.span.parentNode.insertBefore(newSpan, newLineBreak.nextSibling);
    } else {
        console.error('Could not find the IMDb ID element in the page.');
    }

})();