Sorting connection profiles in the Secure Shell Chrome extension

34 views
Skip to first unread message

Nihad Perva

unread,
May 21, 2024, 9:10:30 AMMay 21
to chromium-hterm
i wanted to sort my list of profiles by name and found following blog of course its not working anymore, so i wrote a current example that works in the console when a profile is opened, i just wanted to leave this here if i need it again in the future or anybody else has the same desire and searches for it

(function() {
    console.log('Script execution started.');

    // Check if nassh_ is available
    if (typeof nassh_ === 'undefined') {
        console.error('nassh_ is not defined');
        return;
    }
    console.log('nassh_ is available.');

    // Access the preference manager
    var prefs = nassh_.prefs_;
    console.log('prefs object:', prefs);

    // Function to read storage and sort profiles
    prefs.readStorage().then(() => {
        console.log('Inside prefs.readStorage...');

        var list = prefs.get('profile-ids');
        if (!list) {
            console.error('No profile-ids found.');
            return;
        }
        console.log('Profile ids:', list);

        list = list.slice(0);
        var out = [];

        // Function to process each profile
        var doprofile = function() {
            if (list.length > 0) {
                var id = list.pop();
                var profile = prefs.getProfile(id);
                profile.readStorage().then(() => {
                    var description = profile.get('description') || '';
                    out.push({ k: id, v: description });
                    doprofile();
                }).catch((error) => {
                    console.error('Error reading profile storage for id:', id, error);
                });
            } else {
                console.log('Sorting profiles...');
                out.sort(function(a, b) {
                    if (a.v < b.v) return -1;
                    if (a.v > b.v) return 1;
                    return 0;
                });

                var sortedIds = out.map(item => item.k);
                prefs.set('profile-ids', sortedIds).then(() => {
                    console.log('Profiles sorted by description and saved successfully.');
                }).catch((error) => {
                    console.error('Error saving sorted profile-ids:', error);
                });
            }
        };

        // Start processing profiles
        console.log('Starting profile processing...');
        doprofile();
    }).catch((error) => {
        console.error('Error reading storage:', error);
    });

    console.log('After calling prefs.readStorage...');
    return 'Script execution completed.';
})();
Reply all
Reply to author
Forward
0 new messages