e621 Search Sorter

Adds a post sorter under the search bar

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         e621 Search Sorter
// @namespace    e621-search-sorter
// @version      1.2
// @description  Adds a post sorter under the search bar
// @match        https://e621.net/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  function waitForSearch() {
    const searchSection = document.querySelector('.post-search');
    if (!searchSection) {
      requestAnimationFrame(waitForSearch);
      return;
    }

    if (document.getElementById('tm-sorter-container')) return;

    // scoped styles
    if (!document.getElementById('tm-sorter-style')) {
      const style = document.createElement('style');
      style.id = 'tm-sorter-style';
      style.textContent = `
        .tm-sorter-select,
        .tm-sorter-button {
          font-family: "Segoe UI Supro", "Segoe UI", system-ui, sans-serif;
          border-radius: 0px;
        }
      `;
      document.head.appendChild(style);
    }

    const container = document.createElement('div');
    container.id = 'tm-sorter-container';
    container.style.marginTop = '8px';
    container.style.display = 'flex';
    container.style.gap = '6px';
    container.style.alignItems = 'center';

    const metricSelect = document.createElement('select');
    metricSelect.className = 'tm-sorter-select';
    metricSelect.innerHTML = `
      <option value="score">Score</option>
      <option value="favcount">Favorites</option>
      <option value="comment_count">Comments</option>
    `;

    const directionSelect = document.createElement('select');
    directionSelect.className = 'tm-sorter-select';
    directionSelect.innerHTML = `
      <option value="desc">Descending</option>
      <option value="asc">Ascending</option>
    `;

    const applyButton = document.createElement('button');
    applyButton.className = 'tm-sorter-button';
    applyButton.type = 'button';
    applyButton.textContent = 'Apply';

    applyButton.addEventListener('click', () => {
      const textarea = document.getElementById('tags');
      if (!textarea) return;

      const metric = metricSelect.value;
      const direction = directionSelect.value;

      if (direction === 'asc') {
        const ok = confirm(
          '\nascending warning!!\n\n' +
          'this could possibly expose u to content u don\'t want to see...\n\n' +
          'are u sure u want to proceed??'
        );
        if (!ok) return;
      }

      let orderTag = `order:${metric}`;
      if (direction === 'asc') {
        orderTag += '_asc';
      }

      let tags = textarea.value.trim();

      tags = tags
        .split(/\s+/)
        .filter(t => !t.startsWith('order:'))
        .join(' ');

      textarea.value = (tags + ' ' + orderTag).trim();

      const form = textarea.closest('form');
      if (form) form.submit();
    });

    container.appendChild(metricSelect);
    container.appendChild(directionSelect);
    container.appendChild(applyButton);

    searchSection.appendChild(container);
  }

  waitForSearch();
})();