e621 Deleted Post Source Opener

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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.');
    }
})();