Facebook User List Maker

Save and Load Friend Lists for Facebook Lists

As of 02.12.2017. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Facebook User List Maker
// @namespace    facebookuserlistmaker
// @version      1.0.1
// @author       Tophness
// @match        http://www.facebook.com/*friends*
// @match        https://www.facebook.com/*friends*
// @match        http://www.facebook.com/bookmarks/lists*
// @match        https://www.facebook.com/bookmarks/lists*
// @description  Save and Load Friend Lists for Facebook Lists
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @grant   GM_getValue
// @grant   GM_setValue
// @grant   GM_deleteValue
// @grant   GM_listValues
// @run-at       document-idle
// ==/UserScript==

var friendsSelector = 'div[id$="_friends"]';
var storySelector = 'div[id^="friends_"]';
var storySelector2 = 'div[data-testid="friend_list_item"]';
var users = [];
var mutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

function includes(k) {
  for(var i=0; i < this.length; i++){
    if( this[i].name === k || ( this[i].name !== this[i] && k !== k ) ){
      return true;
    }
  }
  return false;
}

function block(story) {
  if (!story) {
    return;
  }
  if (story.getElementsByTagName('a').length) {
    var links = story.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
      if(links[i].outerHTML.indexOf('data-hovercard') != -1){
      	var uname = links[i].innerText;
        var uid = links[i].getAttribute('data-hovercard');
        uid = uid.substring(uid.indexOf('id=') + 3);
        uid = uid.substring(0, uid.indexOf('&'));
        if(uname != ""){
            var newuser = {};
            newuser.name = uname;
            newuser.id = uid;
            if(!users.includes(newuser.name)){
                users.push(newuser);
            }
        }
      }
    }
  }
}

function process() {
  if(document.forms.save.dosave.value == "true" && document.forms.save.listname.value != ""){
  var stories = document.querySelector(friendsSelector);
  if (!stories) {
    return;
  }
  var story = stories.querySelectorAll(storySelector);
  if (!story.length) {
      story = stories.querySelectorAll(storySelector2);
      if (!story.length) {
          return;
      }
  }
  for (var i2 = 0; i2 < story.length; i2++) {
    block(story[i2]);
  }
      GM_setValue(document.forms.save.listname.value, JSON.stringify(users));
  }
}

function waitForEl(selector, callback, timer=100){
    var poller1 = setInterval(function(){
        $jObject = jQuery(selector);
        if($jObject.length < 1){
            return;
        }
        clearInterval(poller1);
        callback($jObject);
    },timer);
}

function uToken(username, id){
    var newdiv = document.createElement('span');
    newdiv.className = "removable uiToken";
    var newdiv2 = document.createElement('span');
    newdiv2.className = "uiTokenText";
    newdiv2.innerHTML = username;
    var newdiv3 = document.createElement('input');
    newdiv3.name="members[]";
    newdiv3.autocomplete="off";
    newdiv3.type="hidden";
    newdiv3.value = id;
    var newdiv4 = document.createElement('input');
    newdiv4.value=username;
    newdiv4.name="text_members[]";
    newdiv4.autocomplete="off";
    newdiv4.type="hidden";
    var newdiv5 = document.createElement('a');
    newdiv5.href="#";
    newdiv5.aria_label="Remove " + username;
    newdiv5.className="remove uiCloseButton uiCloseButtonSmall";
    newdiv.appendChild(newdiv4);
    newdiv.appendChild(newdiv3);
    newdiv.appendChild(newdiv2);
    document.getElementById('fbFriendListTokenizer').getElementsByClassName('tokenarea')[0].appendChild(newdiv);
}

function listpaste(){
    if(document.getElementById('createListMembers')){
        if(document.getElementById('fbFriendListTokenizer').getElementsByClassName('tokenarea')[0]){
            document.getElementById('fbFriendListTokenizer').getElementsByClassName('tokenarea')[0].className = "tokenarea";
        }
        for (var i = 0; i < users.length; i++) {
            uToken(users[i].name, users[i].id);
        }
    }
    else{
        setTimeout(listpaste, 1000);
    }
}


function observebody(){
    if(document.forms.save.dosave.value == "true"){
        var listname = document.forms.save.listname.value.toLowerCase().replace(/[^a-zA-Z0-9]+/g, "");
        users = JSON.parse(GM_getValue(listname, "[]"));
        if(!users){
            console.warn("error");
        }
        users.includes = includes;
        process();
    }
    else{
        setTimeout(observebody, 1000);
    }
}

function loadbegin(){
    if(document.forms.load.doload.value == "true"){
        users = JSON.parse(GM_getValue(document.forms.load.listnames.value, "[]"));
        if(!users){
            console.warn("error");
        }
        users.includes = includes;
        if(contentarea.getElementsByClassName('uiHeaderActions')[0].childNodes[0]){
            contentarea.getElementsByClassName('uiHeaderActions')[0].childNodes[0].addEventListener('click', listpaste, false);
            contentarea.getElementsByClassName('uiHeaderActions')[0].childNodes[0].click();
        }
    }
    else if(document.forms.load.dodelete.value == "true"){
        var select = document.forms.load.listnames;
        GM_deleteValue(select.value);
        dvalue = select.selectedIndex;
        select.removeChild(select[dvalue]);
    }
    else{
        setTimeout(loadbegin, 1000);
    }
}

if(location.href.indexOf('bookmarks/lists/') != -1){
    var contentarea = document.getElementById('contentArea');
    waitForEl(contentarea, function() {
        var loadbut = document.createElement('div');
        loadbut.innerHTML = '<form name="load"><input type=button onclick="document.forms.load.doload.value=true;" value="Load List"><input type=hidden name=doload value="false"><input type=hidden name=dodelete value="false"><input type=button onclick="document.forms.load.dodelete.value=true;" value="Delete List"></form>';
        var x = document.createElement("select");
        x.name="listnames";
        var alllists = GM_listValues();
        for (var i = 0; i < alllists.length; i++) {
            var option = document.createElement("option");
            option.text = alllists[i];
            x.add(option);
        }
        contentarea.insertAdjacentElement('afterbegin', loadbut);
        document.forms.load.appendChild(x);
        loadbegin();
    });
}
else if(location.href.indexOf('/friends') != -1){
    waitForEl(document.querySelector(friendsSelector), function() {
        var savebut = document.createElement('div');
        savebut.innerHTML = '<form name="save"><input type=button onclick="document.forms.save.dosave.value=true;" value="Save List"><input type=text name=listname><input type=hidden name=dosave value="false"></form>';
        document.querySelector(friendsSelector).insertAdjacentElement('afterbegin', savebut);
        var observer = new mutationObserver(process);
        observer.observe(document.querySelector('body'), {
            'childList': true,
            'subtree': true
        });
        observebody();
    },500);
}