Skip to content

Instantly share code, notes, and snippets.

@tofran
Last active September 29, 2016 22:00
Show Gist options
  • Save tofran/408d3bd7f481cc093424a121ac3f2323 to your computer and use it in GitHub Desktop.
Save tofran/408d3bd7f481cc093424a121ac3f2323 to your computer and use it in GitHub Desktop.
Sort facebook albuns by likes, extremelly buggy, but it kinda works (make sure the album is fully loaded). 💩 #PoopCodeAlert
// ==UserScript==
// @name Album sorter
// @namespace tofran.com
// @version 0.1
// @description Sort facebook albuns by likes, extremelly buggy, but it works (make sure the album is fully loaded)
// @author tofran
// @match https://www.facebook.com/*/photos/?tab=album&album_id=*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
(function() {
'use strict';
$( document ).ready(function() {
var btn = $("<button>Sort!</button>");
btn.on("click", sort);
$(".fbPhotoAlbumHeader").append(btn);
});
function sort(){
var photos = $("#album_photos_pagelet .fbPhotoCurationControlWrapper.fbPhotoStarGridElement");
var container = $(".fbStarGrid");
container.empty();
photos.sort(function(a, b) {
return parseLikesParent($(b)) - parseLikesParent($(a));
});
$(photos).each(function(index, each){
var likeCountBig = $("<span style='position: relative; color: red; font-size: 20px;' class='likeCountBig'>" + parseLikesParent($(each)) + "</span>");
$(container).append($(each).css('margin', 0).css('position', 'static').css('width', 206));
if(!$(each).find(".likeCountBig").length){
$(each).append(likeCountBig);
}
});
}
function parseLikes(likeString){
if(likeString === undefined || likeString === "") return 0;
if(likeString !== undefined && likeString.slice(-1) == "k"){
//@todo support other magnitudes. dont need it right now... to lasy to do it
likeString = likeString.slice(0, -1);
likeString = parseFloat(likeString)*1000;
return likeString;
}
return parseInt(likeString);
}
function parseLikesParent(parent){
return parseLikes(parent.find("._50f3").html());
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment