Benissou / JVote

// ==UserScript==
// @name         JVote
// @author       Benissou
// @description  Evaluez les messages du forum en un clique!
// @include      http://www.jeuxvideo.com/forums/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.8.0/jquery.modal.min.js
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_notification
// @grant        GM_xmlhttpRequest
// @grant        GM_getResourceText
// @grant        GM_getResourceURL
// @version      0.1.5
// @icon         http://image.noelshack.com/fichiers/2016/50/1481684643-benissou.gif 
// @copyright    2016+, Benissou
// ==/UserScript==


var GetDataURL = "https://jvote.000webhostapp.com/GetVote.php";
var SendDataURL = "https://jvote.000webhostapp.com/";

var voteData = "";
var s = "";


	var up = '<span class="plus-commentaire">0</span><span class="icon-arrow-up-entypo pointer vote-commentaire" data-type="plus"></span><span class="moins-commentaire">0</span><span class="icon-arrow-down-entypo pointer vote-commentaire" data-type="moins"></span>';

	$(up).appendTo( ".bloc-options-msg" );

	var classVoteUp = document.getElementsByClassName("icon-arrow-up-entypo pointer vote-commentaire");
	var classVoteDn = document.getElementsByClassName("icon-arrow-down-entypo pointer vote-commentaire");

	for (var i = 0; i <  classVoteUp.length; i++) {
     classVoteUp[i].addEventListener('click', Clicks, false);
	}
	for (var i = 0; i <  classVoteDn.length; i++) {
     classVoteDn[i].addEventListener('click', Clicks, false);
	}
	
	function Clicks() 
	{
			
		var userPseudo = document.getElementsByClassName('account-pseudo')[0].innerHTML;		
	
		if (userPseudo != "Mon compte")
		{
			var vote = this.getAttribute("data-type");	      
			var item = 	$(this).parents('div.bloc-message-forum');
			var post = $(this).parents('.bloc-message-forum').attr("data-id");
	
			$.ajax({
				url:     SendDataURL,
				data:    {pseudo: userPseudo, post: post, vote: vote},
				type:    'POST',
				success: votePostSuccess,
				error:   votePostError
			});
			//alert(userPseudo + ","+ post +  "," + vote);	
		
			if(vote == "plus"){
				var $number = $(this).prev();
				$number.html((parseInt($number.html(),10) || 0) + 1);
			}
			else{
				var $number = $(this).prev();
				$number.html((parseInt($number.html(),10) || 0) + 1);
			}
	        // Called when the POST is successful
			function votePostSuccess(response) {
			
			}

			// Called when the POST fails for some reason (HTTP errors)
			function votePostError(xhr, statusText, err) {
			}	
		}
		
	}	
	
	
	GM_xmlhttpRequest({
		method: "GET",
		url: GetDataURL,
		onload:  parseAJAX_ResponseHTML
	});

	function parseAJAX_ResponseHTML (respObject) {
    var parser      = new DOMParser ();
    var responseDoc = parser.parseFromString (respObject.responseText, "text/html");
	var data = responseDoc.getElementsByTagName('pre');
	MainFunction(responseDoc);//Sent it when it loads only


}

function MainFunction(data){
	

	    console.log (data);
	var ups = 5;
	var downs = 5;
	
	
	var Messages = document.getElementsByClassName("bloc-message-forum");
	var MessagesID = [];
	var i;
	for (i = 0; i < Messages.length; i++) {
  //  x[i].style.backgroundColor = "black";
    MessagesID.push(Messages[i].getAttribute('data-id'));
	
	}


	var UPMessages = data.getElementsByClassName("UP");
	var UPMessagesID = [];
	for (i = 0; i < UPMessages.length; i++) {
  //  x[i].style.backgroundColor = "black";
    UPMessagesID.push(UPMessages[i].getAttribute('id'));
	
	}
	
	var DNMessages = data.getElementsByClassName("DOWN");
	var DNMessagesID = [];
	for (i = 0; i < DNMessages.length; i++) {
  //  x[i].style.backgroundColor = "black";
    DNMessagesID.push(DNMessages[i].getAttribute('id'));
	
	}

	
	


console.log(MessagesID);
console.log(UPMessagesID);	
console.log(DNMessagesID);	
	

	
	
		
	function countInArray(array, what) {
    var count = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i] === what) {
            count++;
        }
    }
    return count;
}

	for (i = 0; i < MessagesID.length; i++) {		
		document.getElementsByClassName("plus-commentaire")[i].innerHTML = countInArray(UPMessagesID, MessagesID[i]);	
	}
	for (i = 0; i < MessagesID.length; i++) {		
		document.getElementsByClassName("moins-commentaire")[i].innerHTML = countInArray(DNMessagesID, MessagesID[i]);	
	}	
	

	
	
	
	
	}