Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thebodzio/54d86d3999221e6ee8fe to your computer and use it in GitHub Desktop.
Save thebodzio/54d86d3999221e6ee8fe 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.
// ==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);
@thebodzio
Copy link
Author

Updated version, taking into account recent changes in Instructable's, that is: transferring the collections list via AJAX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment