Firefox Local file viewer Firefox - sort alphabetically by default

auto-clicks on the Name button in Firefox until the file list is sorted alphabetically then stops

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         Firefox Local file viewer Firefox - sort alphabetically by default
// @namespace    http://tampermonkey.net/
// @version      2025-05-20
// @description  auto-clicks on the Name button in Firefox until the  file list is sorted alphabetically then stops
// @author       You
// @match        file:///*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

'use strict';

function clickUntilTableSortedAsc() {
    const table = document.querySelector('table[order][order-by]');
    const targetOrder = 'asc';
    const targetOrderBy = '0';

    // Stop if table has the desired attributes
    if (table &&
        table.getAttribute('order') === targetOrder &&
        table.getAttribute('order-by') === targetOrderBy) {
        console.log('Table is sorted in ascending order by column 0. Stopping clicks.');
        clearInterval(intervalId);
        return;
    }

    // Find the <a> tag with text exactly "Name"
    const nameLink = Array.from(document.querySelectorAll('a')).find(
        a => a.textContent.trim() === 'Name'
    );

    if (nameLink) {
        console.log('Clicking "Name" link...');
        nameLink.click();
    } else {
        console.log('"Name" link not found.');
    }
}

// Repeat every 0.1 seconds
const intervalId = setInterval(clickUntilTableSortedAsc, 100);