Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| userscripts [2019/01/17 21:48] – created dondarkness | userscripts [2020/10/06 17:32] (current) – removed dondarkness | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | Userscript for Tampermonkey: | ||
| - | \\ | ||
| - | Sorts Events by City then Event:\\ | ||
| - | \\ | ||
| - | // ==UserScript==\\ | ||
| - | // @name          | ||
| - | // @namespace  | ||
| - | // @version  | ||
| - | // @description  | ||
| - | // @author  | ||
| - | // @match  | ||
| - | // @grant  | ||
| - | // ==/ | ||
| - | |||
| - | (function() { | ||
| - | |||
| - | 'use strict'; | ||
| - | // | ||
| - | xdx();\\ | ||
| - | // | ||
| - | |||
| - | /* 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\\ | ||
| - |   // | ||
| - | let nodesSnapshot = myDocument.evaluate(\\ | ||
| - |     '// | ||
| - |  , myDocument, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, | ||
| - | );\\ | ||
| - |  let nbrSelects = nodesSnapshot.snapshotLength; | ||
| - |   // | ||
| - | |||
| - | if (1==nbrSelects) {\\ | ||
| - |       const mySelect = nodesSnapshot.snapshotItem(0); | ||
| - |       const myChildrenCardinal = mySelect.childNodes.length; | ||
| - |       // | ||
| - | if (0!=myChildrenCardinal) { | ||
| - | |||
| - | const myNewEventsList = new Array;\\ | ||
| - |           // | ||
| - | |||
| - | // for each optgroup\\ | ||
| - |           // | ||
| - |           //for (let i=0; i< | ||
| - |           for (let i=0; i< | ||
| - |               let myOptGroup = mySelect.childNodes[i]; | ||
| - |               // | ||
| - |                   // | ||
| - |               if ( (undefined!=myOptGroup) && (' | ||
| - |                   let myCategory = myOptGroup.getAttribute(' | ||
| - |                   // | ||
| - |                   let myEventsCardinal = myOptGroup.childNodes.length; | ||
| - |                   // | ||
| - | // for each option in optgroup\\ | ||
| - |                   for (let j=0 ; j< | ||
| - |                       let myEvent = myOptGroup.childNodes[j]; | ||
| - |                       if ((undefined!=myEvent) && (' | ||
| - |                           let myValue = myEvent.getAttribute(' | ||
| - |                           let myCity  | ||
| - |                           // | ||
| - | // create new OPTION\\ | ||
| - |                           let myOption = myDocument.createElement(' | ||
| - |                               myOption.setAttribute (' | ||
| - |                               myOption.appendChild  | ||
| - | // store as city / optgroup / value into an array\\ | ||
| - | let myObject = {\\ | ||
| - |                                 key   : myCity+' | ||
| - | , htmlOpt: myOption\\ | ||
| - | };\\ | ||
| - |                           // | ||
| - |                           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\\ | ||
| - |                   // | ||
| - |                   // | ||
| - | }\\ | ||
| - | // END IF\\ | ||
| - | }\\ | ||
| - | // END FOR myChildrenCardinal\\ | ||
| - |           // | ||
| - |           // | ||
| - | |||
| - | // sort the list\\ | ||
| - |           myNewEventsList.sort(function(a, | ||
| - |               const keyA = a.key.toUpperCase(); | ||
| - |               const keyB = b.key.toUpperCase(); | ||
| - |               if        (keyA < keyB) { return  | ||
| - | } else if (keyA > keyB) { return -1;\\ | ||
| - |               } else                  { return  | ||
| - | }\\ | ||
| - | // END IF\\ | ||
| - | });\\ | ||
| - | // END SORT | ||
| - | |||
| - |           // | ||
| - |           // | ||
| - |           // | ||
| - | |||
| - | // for each item in list, add to select\\ | ||
| - | myNewEventsList.forEach( function(myObject2) {\\ | ||
| - |               // | ||
| - |               // | ||
| - |               mySelect.insertBefore(myObject2.htmlOpt, | ||
| - | });\\ | ||
| - | // END foreach\\ | ||
| - | }\\ | ||
| - | // END if (0!=myChildrenCardinal)\\ | ||
| - | }\\ | ||
| - | // END if (1==nbrSelects)\\ | ||
| - | }\\ | ||
| - | // END function parseSortAndReplace(myDocument) | ||
| - | |||
| - | })(); | ||
| - | |||