velzevur / Pinterest magic

// ==UserScript==
// @name         Pinterest magic
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Load and sort pinterest pages by repins
// @author       Velzevur
// @match        https://www.pinterest.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';


function pin_sort(ALL_PAGES_COUNT, MIN_REPINS){
	var iter = 0;
	function sort_and_show(){

		var list = document.getElementsByClassName("repinCountSmall");

		function extract_number(e){
			var num = 0;
			var str = e.innerText;
			var parts = str.split(" ");
			var numPart = parts[0];
			var KPart = numPart.split("k");
			if(KPart[1] === ""){
				num = Number(KPart[0]) * 1000;
			} else {
				num = Number(numPart);
			}
			return num;
		}
		var sorted = [].slice.call(list).sort(function(a, b) {
		   var compA = extract_number(a);
		   var compB = extract_number(b);
		   return (compA == compB) ? 0 : (compA > compB) ? -1 : 1;
		});
		var i = 0;
		var sortedLength = sorted.length;
		for(i = 0; i < sortedLength; i++){
			if(extract_number(sorted[i]) < MIN_REPINS) {
				sorted[i] = "";
			} else {
		    	while ((sorted[i] = sorted[i].parentElement) && !sorted[i].classList.contains("Pin"));
		    }
		}


		var Grid = document.getElementsByClassName("Grid")[0];
		Grid.innerHTML = '';

		for(i = 0; i < sortedLength; i++){
			if(sorted[i]){
			    Grid.appendChild(sorted[i]);
			}
		}
        alert("DONE!");
        document.getElementById("btn").disabled=false;
        document.getElementById("pages_info").innerText = "";
	}

	function load(){
		iter ++;
        var msg = "LOADING PAGE " + iter;
        document.getElementById("pages_info").innerText = msg;
		console.log(msg);
		if(iter >= ALL_PAGES_COUNT){
			console.log("PAGES ARE LOADED, SORTING!");
			clearInterval(loader);
			sort_and_show();
		}
		document.body.scrollTop = document.body.scrollHeight;

	}
	var loader = setInterval(load, 2000);
}
var mainDiv= document.createElement('div');
var hdr = document.createElement("H2");
var hdr_text = document.createTextNode("Filter");
hdr.appendChild(hdr_text);
mainDiv.appendChild(hdr);
var frm = document.createElement("FORM");

var pages_input = document.createElement("INPUT");
pages_input.setAttribute("type", "text");
pages_input.setAttribute("name", "pages_input");
pages_input.setAttribute("value", "20");
pages_input.style.width = '80px';
pages_input.setAttribute("id", "pages_input");
pages_input.style.marginBottom = "10px";

var pages_input_label = document.createElement("LABEL");
var pages_input_label_text = document.createTextNode("Pages to load");
pages_input_label.setAttribute("for", "pages_input");
pages_input_label.appendChild(pages_input_label_text);


var min_repins = document.createElement("INPUT");
min_repins.setAttribute("type", "text");
min_repins.setAttribute("name", "min_repins");
min_repins.setAttribute("value", "1000");
min_repins.style.width = '80px';
min_repins.setAttribute("id", "min_repins");

var min_repins_label = document.createElement("LABEL");
var min_repins_label_text = document.createTextNode("Minimal repins");
min_repins_label.setAttribute("for", "min_repins");
min_repins_label.appendChild(min_repins_label_text);
min_repins.setAttribute("id", "min_repins");

frm.appendChild(pages_input_label);
frm.appendChild(pages_input);
frm.appendChild(min_repins_label);
frm.appendChild(min_repins);

var btn = document.createElement("BUTTON");
var btn_txt = document.createTextNode("Load and filter!");
btn.setAttribute("id", "btn");
btn.appendChild(btn_txt);
btn.onclick = function(event) {
	event.preventDefault();
    document.getElementById("btn").disabled=true;
	pin_sort(Number(document.getElementById("pages_input").value), Number(document.getElementById("min_repins").value));
};
btn.style.marginTop = "10px";

frm.appendChild(btn);

mainDiv.appendChild(frm);


var info = document.createElement("P");
info.setAttribute("id", "pages_info");

mainDiv.appendChild(info);

mainDiv.style.position = 'fixed';
mainDiv.style.bottom = '20px';
mainDiv.style.left = '20px';
mainDiv.style.height = '150px';
mainDiv.style.width = '110px';
mainDiv.style.background= '#EFEFEF';
mainDiv.style.borderRadius = "20px";
//mainDiv.setAttribute("class", "democlass");
mainDiv.style.border = "thick solid #BD081C";
mainDiv.style.padding = "10px";
mainDiv.setAttribute("id", "main_div");
mainDiv.style.opacity = "0.2";
mainDiv.onmouseover = function(){document.getElementById("main_div").style.opacity = "1";};
mainDiv.onmouseout = function(){document.getElementById("main_div").style.opacity = "0.2";};

document.body.appendChild(mainDiv);

})();