Forked from thebodzio/instructables.sort.add.instructable.to.collection.alphabetically.user.js
Created
January 28, 2016 15:05
-
-
Save ddpruitt/e22a58af2638fa10933f to your computer and use it in GitHub Desktop.
Simple user script (Greasemonkey ready) to sort alphabetically list of collections shown when “Add to Collection” button is clicked by the instructable on www.instructables.com. By default this list is sorted by collection's creation date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Instructables: sort “Add Instructable to Collection” list alphabetically | |
// @namespace thebodzio | |
// @include /^https?://www.instructables.com/id/.*/ | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
// Returns the UL element holding all the LIs with collections names | |
// or "null" if it's not found | |
var getCollectionsContainer = function () { | |
var collectionsContainer = | |
document.getElementById('CollectionChooser'); | |
if (collectionsContainer != null) { | |
collectionsContainer = collectionsContainer | |
.getElementsByClassName('dropdown-menu-options')[0]; | |
} | |
return collectionsContainer; | |
}; | |
// Sorts collections contained in collectionsContainer element | |
var sortCollections = function (collectionsContainer) { | |
// Don't bother if there's no container | |
if (collectionsContainer) { | |
// Collection of all LIs | |
var items = collectionsContainer.children; | |
// Returns collection name extracted from LI's innerHTML | |
var itemName = function (item) { | |
return item.firstElementChild.firstChild.textContent; | |
}; | |
// Array in which LIs are to be sorted in proper order | |
var sortedItems = []; | |
// Filling array with LI references | |
for (var i = 0; i < items.length; i++) { | |
sortedItems.push(items[i]); | |
} | |
// Sorting LIs in newly initialized array | |
sortedItems.sort(function (itemA, itemB) { | |
return itemName(itemA).localeCompare(itemName(itemB)); | |
}); | |
// Placing LIs in proper order within the UL container | |
for (var i = 0; i < sortedItems.length; i++) { | |
collectionsContainer.appendChild(sortedItems[i]); | |
} | |
} | |
}; | |
// Polls for collections container and once it's found it sorts its elements. | |
// Number of milliseconds between polls is given as delay parameter. | |
var pollForCollectionsContainer = function (delay) { | |
var collectionsContainer = getCollectionsContainer(); | |
if (collectionsContainer == null) { | |
setTimeout(pollForCollectionsContainer, delay); | |
} else { | |
sortCollections(collectionsContainer); | |
} | |
}; | |
// If necessary, delay can be made longer to lessen the strain on the CPU | |
pollForCollectionsContainer(1e3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment