Last active
May 14, 2024 18:20
-
-
Save Peter-Schorn/fab01b6c95be452fe2d6bd320b6f10b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Tray Stacks Counter | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-05-14 | |
// @description try to take over the world! | |
// @author Peter Schorn (schornpe) | |
// @match https://flow-sortation-na.amazon.com/* | |
// @grant none | |
// ==/UserScript== | |
(async function() { | |
'use strict'; | |
/** | |
* https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists | |
* | |
* @param {string} selector | |
*/ | |
async function waitForElement(selector) { | |
return new Promise(resolve => { | |
if (document.querySelector(selector)) { | |
return resolve(document.querySelector(selector)); | |
} | |
const observer = new MutationObserver(mutations => { | |
const element = document.querySelector(selector); | |
console.log(`updateDOM: testing for with querySelector for element: "${selector}": ${element}`); | |
if (element) { | |
resolve(element); | |
observer.disconnect(); | |
} | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true, | |
attributeFilter: ["class"] | |
}); | |
}); | |
} | |
console.log("\n === Tray Stacks Counter ===\n"); | |
let trayCountContainer = await waitForElement("div.tray-count-data-container"); | |
async function main() { | |
let trayCount; // int | |
let targetTrayCount; // int | |
// 980 — 1120 | |
const targetTrayCountRangeRegex = /^\s*(\d+)\D+(\d+)\s*$/g; | |
var childrenElements = trayCountContainer.children; | |
console.log(`childrenElements.length: ${childrenElements.length}`) | |
for (var i = 0; i < childrenElements.length; i++) { | |
var childElement = childrenElements[i]; | |
const label = childElement.querySelector("strong:first-child")?.innerText; | |
const value = childElement.querySelector("strong:last-child")?.innerText; | |
if (label && value) { | |
if (label.match(/Tray Count for AFE:/)?.[0]) { | |
trayCount = parseInt(value); | |
console.log(`found tray count at ${i}: ${trayCount}`); | |
} | |
// TODO: regex contains instead of full match | |
else if (label.match(/Target Tray Count:/)?.[0]) { | |
const targetTrayCountRange = value; | |
console.log(`found tray count range at ${i}: ${targetTrayCountRange}`); | |
const matches = targetTrayCountRangeRegex.exec(targetTrayCountRange); | |
if (matches && matches?.length >= 3) { | |
const lowerBound = parseInt(matches[1]); | |
const upperBound = parseInt(matches[2]); | |
const middle = lowerBound + ((upperBound - lowerBound) / 2); | |
targetTrayCount = middle; | |
console.log(`target tray count: ${targetTrayCount}`); | |
} | |
} | |
} | |
} | |
if (trayCount && targetTrayCount) { | |
const differential = targetTrayCount - trayCount; | |
const stackDifferential = Math.floor(differential / 12); | |
console.log(`differential: ${differential}; stackDifferential: ${stackDifferential}`); | |
} | |
else { | |
console.log(`could not get both trayCount ("${trayCount}") and targetTrayCount ("${targetTrayCount}")`); | |
} | |
} | |
await main(); | |
const mutationObserver = new MutationObserver((mutations) => { | |
console.log( | |
"MutationObserver callback: DOM changed" | |
); | |
main().then((result) => { | |
console.log("main finished in mutation observer"); | |
}); | |
}); | |
mutationObserver.observe(document.body); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment