Skip to content

Instantly share code, notes, and snippets.

@uamv
Last active April 17, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uamv/25b1c59f32251320048e to your computer and use it in GitHub Desktop.
Save uamv/25b1c59f32251320048e to your computer and use it in GitHub Desktop.
Adds a button to Pinterest profiles allowing the alphabetizing of boards.
// Using an object literal for a jQuery feature
var alphaBoards = {
init: function( settings ) {
alphaBoards.setup();
if ( $('.ownProfile').length ) {
alphaBoards.config = {
parentSelector: ".UserBoards.ownProfile div:first-child .GridItems",
childSelector: "div.item",
keySelector: "div a.boardLinkWrapper",
boardCount: parseInt( $('.BoardCount span.value').text() )
}
} else {
alphaBoards.config = {
parentSelector: ".UserBoards div:first-child .GridItems,.FollowingSwitcher + div .GridItems",
childSelector: "div.item",
keySelector: "div a.boardLinkWrapper",
boardCount: parseInt( $('.BoardCount span.value').text() )
}
}
// Allow overriding the default config
$.extend( alphaBoards.config, settings );
alphaBoards.clickResponse();
},
setup: function() {
if ( ! $('.alphaButtonWrapper').length ) {
$('.FooterButtons').prepend('<div class="buttonInoutWrapper alphaButtonWrapper" style="margin-bottom:8px;"><button type="button" class="Button DropdownButton Module footerIcon alphaButton borderless" id="alphaBoards" style="padding:8px 2px;"><span style="display:block;width:28px;height:16px;font-size:12px;font-weight:bold;color:#8e8e8e;">A-Z</span><span class="accessibilityText">Alphabetize Boards</span></button></div>');
}
},
clickResponse: function() {
$('#alphaBoards').click( function() {
// Use board count to assign function counter
var n = alphaBoards.config.boardCount/40;
// Scroll to bottom repeatedly to ensure ajax loading of all boards
for( var i = 0; i < n; i++ ) {
setTimeout( function() { $(document).scrollTop($(document).height()); }, i*1000 );
}
// Call alphabetical sort after all boards have had time to load
if ( $('.ownProfile').length ) {
setTimeout( alphaBoards.sortOwnBoards(), (n*1000)+50);
} else {
setTimeout( alphaBoards.sortOthersBoards(), (n*1000)+50);
}
// Scroll to top after alphabetizing
setTimeout( function() { window.scrollTo(0,0); }, (n*1000)+100 );
});
},
sortOwnBoards: function() {
// Capture 'Create a board'
var creatorBoard = $(alphaBoards.config.parentSelector).children('div.item').first();
// Capture all boards
var items = $(alphaBoards.config.parentSelector).children(alphaBoards.config.childSelector).not(':first').sort(function(a, b) {
// Set first sort element to board name
var vA = $(alphaBoards.config.keySelector, a).attr('href').split("/");
var vA = vA[2];
// Set second sort element to board name
var vB = $(alphaBoards.config.keySelector, b).attr('href').split("/");
var vB = vB[2];
// Compare sort elements and order array
return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});
// Replace html with sorted boards
$(alphaBoards.config.parentSelector).html(items);
// Prepend 'Create a board'
$(alphaBoards.config.parentSelector).prepend(creatorBoard);
},
sortOthersBoards: function() {
var items = $(alphaBoards.config.parentSelector).children(alphaBoards.config.childSelector).sort(function(a, b) {
var vA = $(alphaBoards.config.keySelector, a).attr('href').split("/");
var vA = vA[2];
var vB = $(alphaBoards.config.keySelector, b).attr('href').split("/");
var vB = vB[2];
return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});
$(alphaBoards.config.parentSelector).html(items);
}
};
$( document ).ready( alphaBoards.init );
@ksartnl
Copy link

ksartnl commented Jan 4, 2017

How does this gist work? I tried pasting the code into the Chrome console but I get:

Uncaught TypeError: Cannot read property 'ready' of undefined at <anonymous>:96:14.

$ exists and return a function, so jQuery is present. What can it be, that raises the error?

@Felina-Lain
Copy link

Felina-Lain commented Apr 17, 2017

Is this supposed to work in GreaseMonkey or TemperMonkey? I get an "Invalid js script" error...
And when trying to use it in the Chrome console I get the same error as Ksartnl...

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