parloti / Add Friends

// ==UserScript==
// @name Add Friends
// @description Gerenciar Apps do Google Play 
// @version 1.0
// @author parloti
// @license MIT
// @include https://play.google.com/apps/*
// @icon https://www.gstatic.com/android/market_images/web/favicon_v2.ico
// @match        http://portal.pixelfederation.com/en/profile*
// @grant        none
// @run-at	   document-end
// ==/UserScript==
"use strict";
//http://portal.pixelfederation.com/?a=profile/userProfile&profileId=4833401
//http://portal.pixelfederation.com/?a=profile/userProfile
var players=JSON.parse("["+(localStorage.players||"")+"]");
var friends=JSON.parse("["+(localStorage.friends||"")+"]");
function getProfiles(){
	var requestedTime=new Date().getTime();
    var player=players.shift()||7027252;
    while(player&&friends.indexOf(player)!==-1){
        //console.log(player,friends.indexOf(player));
        player=players.shift();
    }
    if(player===7027252){
        console.log(players);
    }
    //console.log(player);
    var xhr=new XMLHttpRequest();
	xhr.open("GET","http://portal.pixelfederation.com/en/profile/"+player,true);
	xhr.onload=function(b){
		if(b.currentTarget.status===200){
            var receivedTime=new Date().getTime();
            var response=b.currentTarget.response;
            //console.log(b.currentTarget.response);
            var parser = new DOMParser();
            var doc = parser.parseFromString(response, "text/html");   
            var anchors=doc.querySelectorAll(".player-list__item__detail__username");
            for(var i=0;i<anchors.length;i++){
                let anchor=anchors[i];
                if(anchor.getAttribute("href")){
                    var profile=anchor.getAttribute("href").match(/profile\/(\d+)/);
                    if(profile&&profile.length===2){
                        profile=profile[1];
                        if(players.indexOf(profile)===-1&&friends.indexOf(profile)===-1){
                            players.push(profile);
                        }
                    }
                    else{
                        console.log(profile);
                    }
                }
                else{
                    console.log(anchor);
                }
            }
            localStorage.players=players;
            setTimeout(getProfiles,(receivedTime-requestedTime)*(Math.random()*4+1+localStorage.interval));
            var friendship=doc.querySelector("#profileSettings .btn");
            if(friendship){
                if(friendship.getAttribute("href")){
                    var _friendship=friendship.getAttribute("href").match(/(addInvitedPlayer|removeFriendship|addFriendPlayerFromInvitedPlayer)\/(\d+)/);
                    if(_friendship&&_friendship.length===3){
                        let status=_friendship[1];
                        let friend=_friendship[2];
                        if(status!=="addInvitedPlayer"&&status!=="removeFriendship"&&status!=="addFriendPlayerFromInvitedPlayer"){
                            console.log(status);
                        }
                        if(status==="addInvitedPlayer"||status==="addFriendPlayerFromInvitedPlayer"){
                            setTimeout(addFriend,(receivedTime-requestedTime)*(Math.random()*4+1)+localStorage.interval,player,status);
                        }
                        if(status==="removeFriendship"&&friends.indexOf(profile)===-1){
                            friends.push(player);
                            localStorage.friends=friends;
                        }
                    }
                    else{
                        console.log(_friendship,friendship.getAttribute("href"));
                    }
                }
                else{
                    console.log(friendship);console.log(xhr);
                }
            }
            else{
                console.log(doc);
            }
        }
        else{
            console.log(b.currentTarget.status);
            if(players.indexOf(player)===-1){
                players.push(player);
            }
        }
    }
    xhr.onerror=function(a){
        console.log(a);
        if(players.indexOf(player)===-1){
            players.push(player);
        }
        getProfiles();
    }
	//xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xhr.send();
}
function addFriend(player,status){
	var xhr=new XMLHttpRequest();
	xhr.open("GET","http://portal.pixelfederation.com/en/profile/friendPlayers/"+status+"/"+player,true);
	xhr.onload=function(b){
		if(b.currentTarget.status!==200){
            console.log(b.currentTarget.status);
            if(players.indexOf(player)===-1){
                players.push(player);
            }
        }
        else if(b.currentTarget.status===200||b.currentTarget.readyState===4){
//console.log(b.currentTarget);
        }
	}
    xhr.onerror=function(a){
        console.log(a);
        if(players.indexOf(player)===-1){
                players.push(player);
            }
    }
	//xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xhr.send();
}
getProfiles();