Danbooru Upvotes Sorter

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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