Danbooru Upvotes Sorter

Automatically adds "order:upvotes" to Danbooru searches and redirects immediately

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

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

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         Danbooru Upvotes Sorter
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Automatically adds "order:upvotes" to Danbooru searches and redirects immediately
// @author       nevi
// @match        https://*.donmai.us/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    
    // Function to add order:upvotes to search and redirect if needed
    function addUpvotesOrdering() {
        // Get the search box input element
        const searchBox = document.querySelector('#tags, #search_tag_string, input[name="tags"]');
        if (!searchBox) return;
        
        // Get current URL parameters
        const urlParams = new URLSearchParams(window.location.search);
        const currentTags = urlParams.get('tags') || '';
        
        // Check if we're on a search page and if order:upvotes is not already in the search
        if (window.location.pathname.includes('/posts') && 
            !currentTags.includes('order:') && 
            currentTags.trim() !== '') {
            
            // Add order:upvotes to the search query
            const newTags = currentTags.trim() + ' order:upvotes';
            
            // Create new URL with updated search parameters
            urlParams.set('tags', newTags);
            const newUrl = window.location.pathname + '?' + urlParams.toString();
            
            // Redirect to the new URL immediately
            if (window.location.href !== newUrl) {
                window.location.href = newUrl;
                return; // Exit function after redirect
            }
        }
        
        // Add event listener to the search form submission
        const searchForm = searchBox.closest('form');
        if (searchForm) {
            searchForm.addEventListener('submit', function(e) {
                // Only modify if there's a search value and it doesn't already have an order parameter
                if (searchBox.value.trim() !== '' && !searchBox.value.includes('order:')) {
                    e.preventDefault(); // Prevent normal form submission
                    
                    // Add order:upvotes to the search term
                    const newSearch = searchBox.value.trim() + ' order:upvotes';
                    
                    // Create and navigate to the URL directly
                    const newUrlParams = new URLSearchParams();
                    newUrlParams.set('tags', newSearch);
                    const newUrl = '/posts?' + newUrlParams.toString();
                    window.location.href = newUrl;
                }
            });
        }
    }
    
    // Run the function when the page loads
    window.addEventListener('DOMContentLoaded', addUpvotesOrdering);
    
    // Also run it now in case the page is already loaded
    addUpvotesOrdering();
})();