Skip to content

Instantly share code, notes, and snippets.

@rascoop
Last active January 5, 2024 13:53
Show Gist options
  • Save rascoop/fd63cae102944de0c51c84618dd78072 to your computer and use it in GitHub Desktop.
Save rascoop/fd63cae102944de0c51c84618dd78072 to your computer and use it in GitHub Desktop.
Reformat ALBA Printing for Local
// ==UserScript==
// @name Reformat Printing for Local
// @namespace http://tampermonkey.net/
// @version 0.45
// @description Native language might get a lot of foreign addresses, this reformats the print page to use less pages
// @author Richard Scoop <richard.scoop@gmail.com>
// @match https://www.mcmxiv.com/alba/territory-mk?*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
function dynamicSortMultiple() {
/*
* save the arguments object as it will be overwritten
* note that arguments object is an array-like object
* consisting of the names of the properties to sort by
*/
var props = arguments;
return function (obj1, obj2) {
var i = 0, result = 0, numberOfProperties = props.length;
/* try getting a different result from 0 (equal)
* as long as we have extra properties to compare
*/
while(result === 0 && i < numberOfProperties) {
result = dynamicSort(props[i])(obj1, obj2);
i++;
}
return result;
}
}
function titleCase(str) {
str = str.toLowerCase().split(' ');
for (var i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(' ');
}
function hasNumber(str){
return /\d/.test(str)
}
function addBuildingNr(anAddress){
if (anAddress.whatType.toLowerCase() == 'do not call') {
return ' <strong>' + anAddress.buildingnr + '</strong>';
} else {
return ' ' + anAddress.buildingnr;
}
}
var theMapsParent = document.getElementById("map-canvas-top").parentNode;
document.getElementById("map-canvas-top").style.display = "none";
document.getElementById("map-canvas-bottom").style.display = "none";
var Cards = document.getElementsByClassName('card');
var card = null;
for (card of Cards) {
card.style.pageBreakAfter = "avoid";
}
var someDivs = Cards[1].getElementsByTagName('DIV');
if (someDivs.length > 0) {
someDivs[0].style.float = "none";
someDivs[0].style.width = "95%";
someDivs[0].style.paddingLeft = "0";
}
if (someDivs.length > 1) {
var someSpans = someDivs[1].getElementsByTagName('SPAN');
document.title = someSpans[0].innerText.trim() + ' - Alba';
}
theMapsParent.style.minHeight = "1.1in";
var qrCodes = document.getElementsByClassName('qrcode');
var qrcode = null;
var addresses = [];
qrCodes[0].parentNode.parentNode.style.left="4in"
// for (qrcode of qrCodes) {
// qrcode.style.display = "none"
// }
var tableOfAddresses = document.querySelectorAll('table.local.addresses');
tableOfAddresses.forEach(table => {
table.classList.remove('addresses');
});
var row = null
for (row of tableOfAddresses[0].rows) {
row.firstChild.innerHTML = row.firstChild.innerHTML.replace(/<\/?span[^>]*>/g,"");
if (row.lastChild.getElementsByTagName('span').length == 1){
//console.log(row.lastChild.getElementsByTagName('span')[0]);
row.lastChild.removeChild(row.lastChild.getElementsByTagName('span')[0])
};
if (row.lastChild.getElementsByTagName('small').length == 1){
//console.log(row.lastChild.getElementsByTagName('span')[0]);
row.lastChild.removeChild(row.lastChild.getElementsByTagName('small')[0])
};
row.lastChild.innerHTML = row.lastChild.innerHTML.replace(/\, willemstad/gi,"");
row.lastChild.innerHTML = row.lastChild.innerHTML.replace(/\, curacao/gi,"");
if (row.lastChild.innerHTML.includes(", ")) {
var theAddress = row.lastChild.innerHTML.trim();
var whereisthecomma = theAddress.indexOf(", ");
var firstPart = theAddress.slice(0,whereisthecomma);
var secondPart = "";
if (hasNumber(firstPart)) {
secondPart = theAddress.slice(whereisthecomma+2);
theAddress = secondPart.trim() + " " + firstPart.trim();
} else {
theAddress = theAddress.replace(", "," ");
}
row.lastChild.innerHTML = theAddress.trim();
}
row.lastChild.innerHTML = titleCase(row.lastChild.innerHTML.replace(" "," "));
row.lastChild.innerHTML = row.lastChild.innerHTML.trim();
if (row.lastChild.innerHTML !== "") {
var parts = [];
parts = row.lastChild.innerHTML.split(" ")
var addressObject = [];
addressObject.whatType = row.firstChild.innerText;
if (parts.length == 1) {
// special case
//
if (parts[0] !== "") {
// no separation, check if there is a number somewhere in the string and splice it out
if (hasNumber(parts[0])) {
// there is a number somewhere, get its position
// slice the number out, fix the innerHTML and split it into parts again
var thePositionOftheNumber = parts[0].search(/\d+/g);
row.lastChild.innerHTML = row.lastChild.innerHTML.substring(0, thePositionOftheNumber) + " " + row.lastChild.innerHTML.substring(thePositionOftheNumber, row.lastChild.innerHTML.length);
parts = row.lastChild.innerHTML.split(" ")
} else {
addressObject.buildingnrint = 0;
addressObject.buildingnr = "";
addressObject.address = row.lastChild.innerHTML;
addressObject.notes = "";
addressObject.address = addressObject.address.toLowerCase();
}
}
}
if (parts.length > 1) {
var aPart = null;
var whichHasNumber = -1;
var i;
var theBuildingNumberObj = null
for (i = 0; i < parts.length; i++) {
if (hasNumber(parts[i])) {
whichHasNumber = i;
break;
}
}
if (whichHasNumber === -1) {
//special case
// there are no numbers
addressObject.buildingnrint = 0;
addressObject.buildingnr = "";
addressObject.address = row.lastChild.innerHTML;
addressObject.notes = "";
//addressObject.address = addressObject.address.toLowerCase();
} else {
theBuildingNumberObj = /\d+/g.exec(parts[whichHasNumber])
addressObject.buildingnrint = parseInt(theBuildingNumberObj[0]);
addressObject.buildingnr = parts[whichHasNumber];
addressObject.address = "";
for (i = 0; i < whichHasNumber; i++) {
addressObject.address = addressObject.address + parts[i] + " ";
}
addressObject.notes = "";
for (i = (whichHasNumber+1); i < parts.length; i++) {
addressObject.address = addressObject.address + parts[i] + " ";
addressObject.notes = addressObject.notes + parts[i] + " ";
}
addressObject.address = addressObject.address.trim()
addressObject.notes = addressObject.notes.trim();
}
}
//addressObject.address = addressObject.address.toLowerCase();
addressObject.fulladdress = row.lastChild.innerHTML;
addressObject.language = row.firstChild.innerHTML;
addresses.push(addressObject);
}
//console.log(addressObject);
//Check if there is a comma, split the section before the comma,
// if it has numbers, append it to the remaining string separated by a space
// if it has no numbers remove the comma from the original string
// split string into words (space is the delimiter)
// if one of the words has number use it as the number component, everything before it is the address everything after is a note
// Title case the address string and number string
//at this point, system should add an object with only the values to a collection (array of objects) using inner text
}
//
//
addresses.sort(dynamicSortMultiple("address","buildingnrint","buildingnr"));
console.log(addresses)
var groupname = "";
var address = "";
var notCalls = [];
var currentAddresses = "";
var aCounter = 0;
let addressMap = new Map();
for (let address of addresses) {
if (!addressMap.has(address.address)) {
addressMap.set(address.address, new Set());
}
let buildingNumbers = addressMap.get(address.address);
if (!buildingNumbers.has(address.buildingnr)) {
buildingNumbers.add(address.buildingnr);
if (address.address !== groupname) {
if (currentAddresses.trim() !== "") {
notCalls.push(currentAddresses);
aCounter = 0;
}
currentAddresses = titleCase(address.address) + " " + addBuildingNr(address);
groupname = address.address;
} else {
if (aCounter > 6) {
notCalls.push(currentAddresses);
aCounter = 0;
currentAddresses = titleCase(address.address) + " " + addBuildingNr(address);
} else {
currentAddresses = currentAddresses + "," + addBuildingNr(address);
}
}
aCounter++;
}
}
if (currentAddresses.trim() !== "") {
notCalls.push(currentAddresses);
}
while(tableOfAddresses[0].hasChildNodes()) {
tableOfAddresses[0].removeChild(tableOfAddresses[0].firstChild);
}
var notCall = null;
var aRow = null;
var aCell = null;
for (notCall of notCalls){
aRow = tableOfAddresses[0].insertRow(tableOfAddresses[0].rows.length);
aCell = aRow.insertCell(0)
aCell.innerHTML = notCall;
aCell.style.fontSize = "12pt";
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment