YouTube Video AI Summary (Perplexity)

Adds a button next to the video title on YouTube to copy URL and open Perplexity AI summary.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Video AI Summary (Perplexity)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Adds a button next to the video title on YouTube to copy URL and open Perplexity AI summary.
// @author       pullso
// @supportURL   https://github.com/pullso/youtube-ai-summary-perplexity/issues
// @license      MIT
// @match        https://www.youtube.com/watch?v=*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    console.log('YouTube Perplexity Summary script loaded');

    // Inject CSS for styling
    const styles = `
        #summaryButton {
            background-color: #f0f0f0;
            color: #333;
            border: 1px solid #ccc;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.2s ease;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            vertical-align: middle;
            z-index: 1000;
            border-radius: 18px;
            margin-left: auto;
        }
        #summaryButton:hover {
            background-color: #e0e0e0;
        }
        #copyNotification {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: #333;
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            z-index: 10000;
            display: none;
        }
    `;
    const styleSheet = document.createElement('style');
    styleSheet.textContent = styles;
    document.head.appendChild(styleSheet);

    let buttonInjected = false;

    function injectSummaryButton() {
        if (buttonInjected) return;

        const titleElement = document.querySelector('#title h1')?.parentElement;
        if (!titleElement) {
            console.warn('Title container not found yet.');
            return;
        }

        // Check if the button already exists
        if (document.getElementById('summaryButton')) {
            buttonInjected = true;
            return;
        }

        // Create the button
        const summaryButton = document.createElement('button');
        summaryButton.id = 'summaryButton';
        summaryButton.textContent = 'Summarize Video';

        // Create notification element
        const notification = document.createElement('div');
        notification.id = 'copyNotification';
        notification.textContent = 'URL copied to clipboard!';
        document.body.appendChild(notification);

        // Add click handler
        summaryButton.addEventListener('click', () => {
            const videoUrl = window.location.href;
            GM_setClipboard(videoUrl);
            console.log('URL copied:', videoUrl);

            notification.style.display = 'block';
            setTimeout(() => notification.style.display = 'none', 2000);

            const perplexityUrl = `https://www.perplexity.ai/?q=${encodeURIComponent(videoUrl)}`;
            window.open(perplexityUrl, '_blank');
        });

        // Insert the button next to the title
        titleElement.appendChild(summaryButton);
        console.log('Button added next to the title.');

        buttonInjected = true;
    }

    // Observer for title changes — reset flag
    const titleObserver = new MutationObserver(() => {
        buttonInjected = false;
        injectSummaryButton();
    });

    // Observe changes in the title container
    const titleContainer = document.querySelector('#title');
    if (titleContainer) {
        titleObserver.observe(titleContainer, { childList: true, subtree: true });
    }

    // Observer for the main body in case #title doesn't exist yet
    const bodyObserver = new MutationObserver(() => {
        injectSummaryButton();
    });

    bodyObserver.observe(document.body, { childList: true, subtree: true });

    // Initial attempt
    injectSummaryButton();

})();