// ==UserScript==
// @name updateEditorProgress
// @namespace http://tampermonkey.net/
// @version 2018-06-30.1
// @description for each discord editor in your region's channel, this script will open the associated editor's profile, capture relevant information, and download that information into
// @description comma separated value (CSV) file. The contents of the file can then be programmaticaly added to a google sheet (using a separate script)
// @description With the script installed, open any WME editor profile page to execute the script.
// @author ramblinwreck_81
// @match https://www.waze.com/user/editor*
// @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js?version=229392
// @grant none
// ==/UserScript==
(function() {
'use strict';
function bootstrap(tries) {
//console.log(tries);
tries = tries || 1;
// console.log(tries);
// console.log(W,W.Map,W.Model,$);
if (W && W.Map &&
W.Model &&
$ ) {
// console.log('passed load check');
init();
} else if (tries < 100) {
setTimeout(function () {
tries += 1;
bootstrap(tries);}, 200);
}
}
function init()
{
console.log("initializing editor progress script");
createUploadElements();
function createUploadElements() {
var y=document.createElement("div");
y.setAttribute("id", "csv-info");
document.getElementsByClassName("user-headline")[0].appendChild(y);
var x = document.createElement("INPUT");
x.setAttribute("type", "file");
x.setAttribute("id","fileUpload");
document.getElementById("csv-info").appendChild(x);
var z=document.createElement("button");
z.setAttribute("type", "button");
z.setAttribute("value", "Upload");
z.setAttribute("id","import-csv");
var aa=document.createTextNode("Upload");
z.appendChild(aa);
document.getElementById("csv-info").appendChild(z);
document.getElementById("import-csv").style.height="20px";
document.getElementById("import-csv").style.width="100px";
} // end of createUploadElements
document.getElementById("import-csv").addEventListener("click",editorProgress,false);
}
bootstrap();
function editorProgress() {
console.log('here');
var openedWindow=[];
var count=0;
var editorNames=[];
var testURLs=[];
var updatedEditorInfo=[];
var indivEditorInfo=[" ", " ", " ", " "];
var m=0;
var editorProfileURL=[];
var windowLoaded=[];
var timer=[];
var slackEditors="";
var blnKeepWaiting=true;
var fileTimer;
var holdOn;
var pageVisits=0;
var blnDataCollected=false;
var blnGotADate=[false,false,false,false];
var numEditors = 0;
var blnAllEditors=false;
var howLong=0;
var noEditorPage = [];
var seEditors = [];
upload();
function upload()
{
var fileUpload = document.getElementById("fileUpload");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
if (regex.test(fileUpload.value.toLowerCase())) {
if (typeof (FileReader) != "undefined") {
var reader = new FileReader();
reader.onload = function (e) {
if (fileTimer !==undefined) {
clearInterval(fileTimer);
slackEditors=reader.result;
editorStats();
}
blnKeepWaiting=false;
};
reader.readAsText(fileUpload.files[0]);
fileTimer=setInterval(checkAgain,1000);
} else {
alert("This browser does not support HTML5.");
}
} else {
alert("Please upload a valid CSV file.");
}
} // end of upload
function checkAgain()
{
if (blnKeepWaiting===false) {
} else {
console.log("still waiting for file upload...");
}
} //end of checkAgain
function slowItDown()
{
alert ("slow it down");
if (editorProfileURL.length <4) {
alert("almost done");
}
if (pageVisits===3 || editorProfileURL.length<4) {
clearInterval(holdOn);
editorStats();
}
} // end of slowItDown
function closeEm(element,index,array)
{
openedWindow[index].close();
}
function controlOpenWindows()
{
if (count>0) {
testURLs.forEach(closeEm);
openedWindow=[];
}
testURLs=[];
if (editorProfileURL.length>=4) {
testURLs[0]= editorProfileURL.shift();
testURLs[1] =editorProfileURL.shift();
testURLs[2]=editorProfileURL.shift();
testURLs[3]=editorProfileURL.shift();
// blnFirst=true;
} else if (editorProfileURL.length>0){
for (var n=0; n<editorProfileURL.length; n++) {
testURLs[n]=editorProfileURL.shift();
}
} else {
}
function openWindow(element1, index1, array1)
{
pageVisits=0;
openedWindow[index1]= window.open(element1);
for (var j=0;j<openedWindow[index1].document.styleSheets.length; j++) {
openedWindow[index1].document.styleSheets[j].disabled=true;
}
} //end of openWindow function
if (blnDataCollected===false) {
testURLs.forEach(openWindow);
openedWindow.forEach(loaded);
}
if (editorProfileURL.length===0) {
blnDataCollected=true;
}
} //end of controlOpenWindows()
function loaded(element, index, array)
{
element.addEventListener("DOMContentLoaded", function() {getSomething(index);}, false);
timer[index]=setInterval(function() {windowReady(index);},3000);
} // end of loaded
function getSomething(param)
{
windowLoaded[param]=true;
console.log("fired");
} // end of getSomething
function windowReady(param)
{
if (windowLoaded[param]===true) {
clearInterval(timer[param]);
grabInfo(param);
}
} // end of windowReady
function grabInfo(editorNumber)
{
var firstEdit;
var rank;
var editCount;
var lastEditDaysAgo;
var reg=/The page you were looking for doesn*/g;
//[0,1,2,3].forEach(findIt);
findIt(editorNumber);
function findIt(param)
{
var editorString="";
var dateOnly="";
var remarks="None";
var blnNoBother=false;
var editorExists = true;
if(openedWindow[param].document.getElementsByTagName('p').length>0) {
var oops = "working on it right now.";
for(var n=0; n<openedWindow[param].document.getElementsByTagName('p').length; n++) {
if (openedWindow[param].document.getElementsByTagName('p')[n].innerHTML.includes(oops)) {
editorExists = false;
blnNoBother = true;
}
}
}
if (editorExists) {
if (openedWindow[param].document.getElementsByClassName("container not-found").length>0) {
if(openedWindow[param].document.getElementsByClassName("container not-found")[0].innerHTML.match(reg)) {
//editorString= editorNames[count+param] + ",,,,,No Editor profile page,";
blnNoBother=true;
}
} else {
//troubleshooting
if(openedWindow[param].document.getElementsByClassName("user-last-edit")[0].innerHTML == undefined) {
console.log('no page for editor ' + editorNames[count+param]);
}
firstEdit=openedWindow[param].document.getElementsByClassName("user-last-edit")[0].innerHTML.trim();
var dateString=firstEdit.slice(14);
var f= new Date(Date.parse(dateString));
var g=f.toLocaleDateString();
firstEdit = g;
rank=openedWindow[param].document.getElementsByClassName("user-rank")[0].innerHTML.trim();
rank=rank.slice(6);
editCount=openedWindow[param].document.getElementsByClassName("user-stats-value")[1].innerHTML.trim();
var numEdits=parseInt(editCount);
if (numEdits>0) {
if(openedWindow[param].document.getElementsByClassName("transaction-header-time")[0] !==undefined) {
lastEditDaysAgo=openedWindow[param].document.getElementsByClassName("transaction-header-time")[0].innerHTML.trim();
lastEditDaysAgo=findNumber(lastEditDaysAgo);
dateOnly=dateInPast(lastEditDaysAgo);
} else {
var dayTimer=setInterval(function() {
if(openedWindow[param].document.getElementsByClassName("transaction-header-time")[0] !==undefined) {
blnGotADate[param]=true;
}
},2000);
if (blnGotADate[param]===true) {
if (openedWindow[param].document.getElementsByClassName("transaction-header-time")[0] !==undefined) {
clearInterval(dayTimer);
lastEditDaysAgo=openedWindow[param].document.getElementsByClassName("transaction-header-time")[0].innerHTML.trim();
lastEditDaysAgo=findNumber(lastEditDaysAgo);
dateOnly=dateInPast(lastEditDaysAgo);
blnGotADate[param]=false;
} else {
blnGotADate[param]=false;
}
}
}
} else {
dateOnly="";
remarks="Never Edited";
blnNoBother=true;
}
editorString= seEditors[count+param] + "," + firstEdit + "," + dateOnly + "," + rank + "," + editCount + "," + remarks + ",";
}
} else {
noEditorPage.push(seEditors[count+param]);
editorExists = true;
}
if ((count+param) <= indivEditorInfo.length) {
if (blnNoBother===false) {
indivEditorInfo[count+ param]=(editorString);
numEditors += 1;
} else {
blnNoBother=false;
}
} else {
alert("attempting to add element to indivEditorInfo that is out of the array's range...");
}
firstEdit="";
lastEditDaysAgo="";
dateOnly="";
remarks="None";
rank="";
editCount="";
blnNoBother=false;
console.log(indivEditorInfo);
var limit;
if (pageVisits===3 && editorProfileURL.length>1) {
var blnRunNoMore=false;
var placeholder="";
if (editorProfileURL.length>=4) {
limit=4;
} else {
limit=editorProfileURL.length;
}
if (blnRunNoMore===false) {
for (var i=0; i<limit; i++) {
indivEditorInfo.push(placeholder);
if (editorProfileURL.length<=4) {
blnRunNoMore = true;
}
}
}
count +=4;
}
if(pageVisits === testURLs.length-1) {
var testTimer=setTimeout(editorStats,500);
}
pageVisits += 1;
} // end of findIt()
} // end of grabInfo()
function waitDate() {
}
function editorPages()
{
window.focus();
seEditors=slackEditors.split(','); // split discord csv into editors -- in discord, each element is an editor
// var troublesomeEditors = [];
// var addTroublesomeEditors = prompt ("If you would like to add additional editors, add them now with each separated by a comma. ", "");
// if (addTroublesomeEditors !== null || addTroublesomeEditors !== "") {
// console.log('trimming things');
// troublesomeEditors = addTroublesomeEditors.split(",");
// if(troublesomeEditors[0].length === 0) {
// troublesomeEditors.shift();
// }
// troublesomeEditors.forEach(function (element, index, array) {
// var str = array[index];
// array[index] = str.trim();
// });
// console.log('troublesome editor array length is ' + troublesomeEditors.length);
// }
// console.log('seEditor array length is ' + seEditors.length);
// console.log('seEditor array: ' + seEditors);
// troublesomeEditors.forEach(function(element, index, array) {
// if (seEditors.indexOf(troublesomeEditors[index]) >= 0) {
// troublesomeEditors[index] = 'dup';
// }
// });
// function trblLessDups(element, index, array) {
// return array[index] != 'dup';
// }
// var filteredTrouble = troublesomeEditors.filter(trblLessDups);
// if(filteredTrouble.length>0) {
// var combined = seEditors.concat(filteredTrouble);
// seEditors = combined;
// console.log('troubling and se editor array is ' + seEditors);
// }
var indivEditor = seEditors; // changed when moving to discord
function makeEditor(element, index, array) {
indivEditor=array[index][0];
//remove the editor rank from editor name
var extraChars=/\w+\_[0-6]{1,2}$/g;
editorNames.push(indivEditor);
} // end of make editor function
var urlString="https://www.waze.com/user/editor/";
function noEditorName (element, index, array)
{
var filter = array[index];
return filter.length > 0;
}
var result = seEditors.filter(noEditorName);
seEditors = result;
function makeURL(element,index, array) {
editorProfileURL[index]= urlString + element;
}
seEditors.forEach(makeURL);
// as the script continues, values of editorProfileURL are removed... will eventually contain no elements
// so make a back up copy
var backUpEditorProfileURL=editorProfileURL;
} //end of editor pages function
function compileInfo()
{
if(blnDataCollected===true) {
testURLs.forEach(closeEm);
makeCSV();
} else {
controlOpenWindows();
}
}
// editorStats();
function editorStats()
{
if(count===0) {
editorPages();
}
compileInfo();
}
function isNumeric(n)
{
return Number(parseFloat(n))==n;
}
// some real code
var blnisANumber=[];
function findNumber(string)
{
var regex=/^(<strong>Today<\/strong>)/g;
if (string.match(regex)) {
return "0";
}
var number="";
var x=string.slice(8);
var y=x.split("");
y.forEach(function(element, index, array) {
if(isNumeric(element)) {
blnisANumber[index]=true;
} else {
blnisANumber[index]=false;
}
});
var filtered=blnisANumber.filter(itsTrue);
function itsTrue(element, index, array) {
return element === true;
}
filtered.forEach(function(element, index, array) {
number= number + y[index];
});
console.log("the resultant number is ", number);
return number;
} // end of findnumber function
function dateInPast(dateInPast)
{
var d= new Date();
d.setDate(d.getDate()-dateInPast);
//var past=d.toDateString();
var past = d.toLocaleString();
var arrPast=past.split(",");
var dateOnly=arrPast[0];
return dateOnly;
}
function makeCSV()
{
if (howLong>0) {
clearInterval(howLong);
}
var csvContent="";
indivEditorInfo.forEach(function(element, index, array) {
csvContent += element;
csvContent += "\n";
});
var linkElement= document.createElement('a');
linkElement.setAttribute("id", "csv-download");
var encodedUri=encodeURI(csvContent);
linkElement.setAttribute('href', "data:text/csv;charset=utf-8," + encodedUri);
linkElement.innerHTML= "Download SER Editor CSV file";
document.getElementById("csv-info").appendChild(linkElement);
document.getElementById("import-csv").disabled = true;
}
}
})();