Danbooru Upvotes Sorter

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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