e621 Deleted Post Source Opener

Opens the source or the original post link if the post is deleted (and not a duplicate).

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         e621 Deleted Post Source Opener
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Opens the source or the original post link if the post is deleted (and not a duplicate).
// @author       Gemini 3
// @match        https://e621.net/posts/*
// @grant        GM_openInTab
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // 1. Проверка на удаление
    const imageContainer = document.querySelector('#image-container');
    const deleteNotice = document.querySelector('.notice-deleted');

    let isDeleted = false;
    if (deleteNotice) {
        isDeleted = true;
    } else if (imageContainer && imageContainer.getAttribute('data-flags')) {
        isDeleted = imageContainer.getAttribute('data-flags').includes('deleted');
    }

    if (!isDeleted) {
        console.log('e621 Deleted Opener: Post is not deleted.');
        return;
    }

    // 2. Проверка причины: не открываем, если это дубликат
    if (deleteNotice) {
        const noticeText = deleteNotice.innerText.toLowerCase();
        if (noticeText.includes('duplicate')) {
             console.log('e621 Deleted Opener: Post is a duplicate. Stopping.');
             return;
        }
    }

    let urlToOpen = null;

    // 3. Пытаемся найти внешнюю ссылку в сайдбаре (Source)
    const externalSourceLinks = document.querySelectorAll('li.source-links .source-link a');
    if (externalSourceLinks.length > 0) {
        urlToOpen = externalSourceLinks[0].href;
        console.log('e621 Deleted Opener: Found external source link.');
    } 
    
    // 4. Если внешнего источника нет (или он не ссылка), ищем внутреннюю ссылку в тексте причины удаления
    if (!urlToOpen && deleteNotice) {
        // Ищем ссылку на другой пост (класс .dtext-post-id-link обычно используется для "post #12345")
        const internalPostLink = deleteNotice.querySelector('a.dtext-post-id-link, a[href^="/posts/"]');
        if (internalPostLink) {
            urlToOpen = internalPostLink.href;
            console.log('e621 Deleted Opener: No external source. Found internal post link in deletion notice.');
        }
    }

    // 5. Открытие ссылки
    if (urlToOpen) {
        console.log(`e621 Deleted Opener: Target found: ${urlToOpen}`);
        setTimeout(() => {
            GM_openInTab(urlToOpen, { active: true });
        }, 500);
    } else {
        console.log('e621 Deleted Opener: No valid source or internal link found.');
    }
})();