This is an old revision of the document!
Userscript for Tampermonkey:
Sorts Events by City then Event:
==UserScript==
@name TheGrid MyEvents : sort Events by City
@namespace https://the-grid.org/r/?page=myevents
@version 1.0
@description sorts Events by City then Event
@author XandrexGress in Ingress @xandrex on Telegram
@match https://the-grid.org/r/?page=myevents*
@grant none
/UserScript
(function() {
'use strict';
console.log ('—-start—-');
xdx();
console.log ('—-end—-');
/* main
*/
function xdx() {
parseSortAndReplace(document);\\
}
END function xdx()
/* get the select that has an id which ends with _id
* for each optgroup in it, for each option it in :
* - create a new option with “city + event” as key and HTML text
* - delete the original option inside the optgroup
* - store it in an array
* - sort the array by the keys
* - add the new option to the select
*
* for later : find a way to delete the optgroup within the select element
*/
function parseSortAndReplace(myDocument) {
get select with id like _id
//console.log ('start parseSortAndReplace');\\
let nodesSnapshot = myDocument.evaluate(
'//select[contains(@id,"_id")]' // warning : ends-with() is not available in XPath 1.0\\
, myDocument, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
);
let nbrSelects = nodesSnapshot.snapshotLength;
//console.log ('nbrSelects:'+nbrSelects);
if (1==nbrSelects) {\\ const mySelect = nodesSnapshot.snapshotItem(0);\\ const myChildrenCardinal = mySelect.childNodes.length;\\ //console.log ('mySelect children cardinal: '+myChildrenCardinal);\\ if (0!=myChildrenCardinal) {
const myNewEventsList = new Array;\\ //console.log (myNewEventsList);
// for each optgroup\\ //console.log ('Enter foreach optgroup loop');\\ //for (let i=0; i<myChildrenCardinal ; i++) {\\ for (let i=0; i<myChildrenCardinal ; i++) { // DEBUG\\ let myOptGroup = mySelect.childNodes[i];\\ //console.log('lala '+i);\\ //console.log (myOptGroup);\\ if ( (undefined!=myOptGroup) && ('OPTGROUP'==myOptGroup.tagName)) {\\ let myCategory = myOptGroup.getAttribute('label');\\ //console.log ('---category:'+myCategory);\\ let myEventsCardinal = myOptGroup.childNodes.length;\\ //console.log ('size:'+myEventsCardinal);\\ // for each option in optgroup\\ for (let j=0 ; j<myEventsCardinal ; j++) {\\ let myEvent = myOptGroup.childNodes[j];\\ if ((undefined!=myEvent) && ('OPTION'==myEvent.tagName)) {\\ let myValue = myEvent.getAttribute('value');\\ let myCity = myEvent.innerHTML;\\ //console.log (myCity + '/' + myCategory + '/'+ myValue );\\ // create new OPTION\\ let myOption = myDocument.createElement('option');\\ myOption.setAttribute ('value',myValue);\\ myOption.appendChild (myDocument.createTextNode(myCity + ': '+ myCategory));\\ // store as city / optgroup / value into an array\\ let myObject = {\\ key : myCity+'¤'+myCategory\\ , htmlOpt: myOption\\ };\\ //console.log (myObject);\\ myNewEventsList.push (myObject);\\ // delete the option in the optgroup\\ myEvent.parentNode.removeChild (myEvent); // until I find a way to delete the original categories\\ }\\ // END IF\\ }\\ // END FOR myEventsCardinal
// delete the optgroup\\ //myOptGroup.parentNode.removeChild(myOptGroup);\\ //mySelect.childNodes[i].parentNode.removeChild (mySelect.childNodes[i]);\\ }\\ // END IF\\ }\\ // END FOR myChildrenCardinal\\ //console.log ('before sort');\\ //console.log (myNewEventsList);
// sort the list\\ myNewEventsList.sort(function(a,b) {\\ const keyA = a.key.toUpperCase();\\ const keyB = b.key.toUpperCase();\\ if (keyA < keyB) { return 1;\\ } else if (keyA > keyB) { return -1;\\ } else { return 0;\\ }\\ // END IF\\ });\\ // END SORT
//console.log ('after sort');\\ //console.log (myNewEventsList);\\ //console.log (myNewEventsList.length);
// for each item in list, add to select\\ myNewEventsList.forEach( function(myObject2) {\\ //console.log (myObject2.htmlOpt);\\ //mySelect.appendChild(myObject2.htmlOpt);\\ mySelect.insertBefore(myObject2.htmlOpt,mySelect.firstChild); // until I find a way to delete the original categories in the select\\ });\\ // END foreach\\ }\\ // END if (0!=myChildrenCardinal)\\ }\\ // END if (1==nbrSelects)\\
}
END function parseSortAndReplace(myDocument)
})();