Skip to content

Instantly share code, notes, and snippets.

@MKo-xx
Created April 16, 2011 12:16
Show Gist options
  • Save MKo-xx/923077 to your computer and use it in GitHub Desktop.
Save MKo-xx/923077 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name SortBy
// @namespace http://autosysprogs.blogspot.com/
// @description Sort by most mutual friends
// @include http://www.facebook.com/find-friends/*
// @author Mkrtich Soghomonyan
// ==/UserScript==
window.addEventListener(
'load',
function() {
// ----------------------------------------
// ----------------------------------------
// Add sort function
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.innerHTML ='function sortByNum(a, b) { return b.num - a.num; } \
function sortPeopleByMostMutualFriends() { \
var allDivs, thisDiv, firstDiv, changeDiv; \
allDivs = document.evaluate("//div[@class=\'friendBrowserUnit\']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); \
var people = new Array(); \
for (var j = 0; j < allDivs.snapshotLength; j++) { \
var mutual = allDivs.snapshotItem(j).innerHTML.replace(/([0-9]*) mutual friend.*/g, "===========$1").replace(/^.*===========/, ""); \
if (!mutual.match(/^[0-9]*$/)) { mutual = 0; }\
people.push({num: mutual, val: allDivs.snapshotItem(j).cloneNode(true)}); \
} \
people.sort(sortByNum); \
var parent = new Array(); \
for (var i = 0; i < allDivs.snapshotLength; i++) { \
thisDiv = allDivs.snapshotItem(i); \
changeDiv = document.getElementById(thisDiv.id); \
if (changeDiv) { \
parent.push(changeDiv.parentNode); \
changeDiv.parentNode.removeChild(changeDiv); \
} \
} \
for (var i = 0; i < allDivs.snapshotLength; i++) { \
parent[i].appendChild(people[i].val);\
} \
}';
document.getElementsByTagName("head")[0].appendChild(scriptElement);
// ----------------------------------------
// ----------------------------------------
// Add sort button above the hometown check boxes.
// Get the hometown table.
var targetDiv = document.getElementById('checkboxes_hometown');
if (null == targetDiv) {
// Current page is different from find-friends page.
return;
}
// Create a div to surround the sort button.
var newDiv = document.createElement('div');
newDiv.setAttribute('id', 'sortPeopleByMostMutualFriends');
// Create the button and set its attributes.
var sortButton = document.createElement('input');
sortButton.name = 'sortButton';
sortButton.type = 'button';
sortButton.value = 'Sort by mutual friends';
sortButton.setAttribute("onclick", "sortPeopleByMostMutualFriends();");
// Append the button to the div.
newDiv.appendChild(sortButton);
// Append before hometown table.
targetDiv.parentNode.insertBefore(newDiv, targetDiv);
},
true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment