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.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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.

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

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