Mega Quick Sort Buttons (UI Engine Bound, 2026)

Adds quick Name and Size sort buttons next to Mega’s own sort control.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Mega Quick Sort Buttons (UI Engine Bound, 2026)
// @name:en      Mega Quick Sort Buttons (UI Engine Bound, 2026)
// @namespace    https://greasyfork.org/en/users/1561012-scriptnr
// @version      1.0.0
// @description  Adds quick Name and Size sort buttons next to Mega’s own sort control.
// @description:en Adds quick Name and Size sort buttons next to Mega’s own sort control.
// @author       ScriptNR
// @match        https://mega.nz/*
// @match        https://mega.io/*
// @icon         https://www.google.com/s2/favicons?domain=mega.nz
// @grant        none
// @license      MIT

// ==/UserScript==

(function () {
  'use strict';

  function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }

  function getSortButton() {
    return document.querySelector('.sort-direction-button');
  }

  function getSortDropdownTrigger() {
    return [...document.querySelectorAll("button,div")]
      .find(e => e.textContent && e.textContent.match(/type|last modified|date added|name/i));
  }

  function findMenuItem(label) {
    return [...document.querySelectorAll("button,div,span")]
      .find(e => e.textContent && e.textContent.trim().toLowerCase() === label.toLowerCase());
  }

  async function sortBy(label) {
    const dropdown = getSortDropdownTrigger();
    if (!dropdown) { alert("Mega sort menu not found"); return; }

    dropdown.click();
    await sleep(120);

    const option = findMenuItem(label);
    if (!option) { alert("Sort option not found: " + label); return; }

    option.click();
    await sleep(60);

    const dir = getSortButton();
    if (dir) dir.click(); // forces Mega dispatcher refresh
  }

  function makeBtn(txt, fn) {
    const b = document.createElement("button");
    b.textContent = txt;
    b.style = "padding:8px 12px;margin-bottom:6px;border-radius:8px;border:none;background:#d32f2f;color:white;font-weight:600;cursor:pointer;width:100%";
    b.onclick = fn;
    return b;
  }

  function inject() {
    if (document.getElementById("megaQuickSortPanel")) return;
    if (!getSortButton()) return; // wait for real Mega UI

    const panel = document.createElement("div");
    panel.id = "megaQuickSortPanel";
    panel.style = `
      position:fixed;
      bottom:20px;
      right:20px;
      z-index:999999;
      background:rgba(20,20,20,0.9);
      padding:10px;
      border-radius:10px;
      min-width:120px;
      box-shadow:0 4px 14px rgba(0,0,0,.4);
      font-family:system-ui;
    `;

    const t = document.createElement("div");
    t.textContent = "MEGA SORT";
    t.style = "color:#fff;font-size:11px;text-align:center;margin-bottom:8px;opacity:.7";

    panel.appendChild(t);
    panel.appendChild(makeBtn("Name", () => sortBy("Name")));
    panel.appendChild(makeBtn("Size", () => sortBy("Size")));

    document.body.appendChild(panel);
  }

  new MutationObserver(inject).observe(document.body, { childList:true, subtree:true });
  inject();

})();