Last active
June 18, 2026 11:06
-
-
Save pedrolamas/debcf696ed94890e083595eff5276f96 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 John Pye Auctions Total Calculator | |
| // @namespace http://tampermonkey.net/ | |
| // @version 2026-06-18_12-06 | |
| // @description Calculates the total to pay on John Pye Auctions site | |
| // @author Pedro Lamas | |
| // @match https://www.johnpyeauctions.co.uk/Event/LotDetails/* | |
| // @match https://www.johnpyeauctions.co.uk/Events/Details/* | |
| // @match https://www.johnpyeauctions.co.uk/Browse* | |
| // @match https://www.johnpyeauctions.co.uk/Account/Bidding/Watching | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=johnpyeauctions.co.uk | |
| // @grant GM_getValue | |
| // @grant GM_setValue | |
| // ==/UserScript== | |
| (function () { | |
| 'use strict'; | |
| const CACHE_KEY = 'jpa_shipping_cache'; | |
| const CACHE_TTL_MS = 10 * 24 * 60 * 60 * 1000; // 10 days | |
| const now = Date.now(); | |
| const getValue = (x) => +(x.replace(/,/g, '')) | |
| const getShippingValue = (x) => getValue(/[$€£]((\d+,)*\d+\.\d+)/.exec(x.innerText)[1]); | |
| const formatNumber = (x) => x.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) | |
| const getListingIdAndShippingCost = (listingElement) => { | |
| const listingId = listingElement.getAttribute('data-listingid'); | |
| const shippingTableElement = listingElement.getElementsByClassName("shipping-table")[0]; | |
| const vatOnlyOnBuyersPremium = /VAT +Only +Payable +on +(the +)?Buyer\'?s +Premium/i | |
| .test(document.title); | |
| if (shippingTableElement) { | |
| const shippingCost = getShippingValue(shippingTableElement); | |
| const cacheItem = cache[listingId] = { | |
| value: shippingCost, | |
| timestamp: now | |
| }; | |
| if (vatOnlyOnBuyersPremium) { | |
| cacheItem.vatOnlyOnBuyersPremium = true; | |
| } | |
| return { | |
| listingId, | |
| shippingCost, | |
| vatOnlyOnBuyersPremium, | |
| shippingSource: 'table' | |
| }; | |
| } | |
| const cacheItem = cache[listingId]; | |
| if (cacheItem) { | |
| return { | |
| listingId, | |
| shippingCost: cacheItem.value, | |
| vatOnlyOnBuyersPremium: cacheItem.vatOnlyOnBuyersPremium, | |
| shippingSource: 'cache' | |
| } | |
| } | |
| return { | |
| listingId, | |
| shippingCost: 7.99, | |
| vatOnlyOnBuyersPremium: false, | |
| shippingSource: 'fallback' | |
| }; | |
| } | |
| const updateObservedElementTotal = (element, calculateTotal, shippingSource) => { | |
| const indicator = shippingSource === 'fallback' | |
| ? '🔴' | |
| : shippingSource === 'cache' | |
| ? '🟡' | |
| : '🟢' | |
| element.nextElementSibling.innerText = `${indicator}${element.previousSibling.textContent.trim()}${calculateTotal(getValue(element.innerText))}`; | |
| }; | |
| const rawCache = GM_getValue(CACHE_KEY, {}); | |
| const cache = Object.fromEntries( | |
| Object.entries(rawCache).filter(([, entry]) => now - entry.timestamp <= CACHE_TTL_MS) | |
| ) | |
| const listingElements = document.querySelectorAll('div[data-listingid]'); | |
| for (const listingElement of listingElements) { | |
| const { listingId, shippingCost, shippingSource, vatOnlyOnBuyersPremium } = getListingIdAndShippingCost(listingElement); | |
| const calculateTotal = vatOnlyOnBuyersPremium | |
| ? (value) => formatNumber(value + (value * 0.25 + shippingCost) * 1.2) | |
| : (value) => formatNumber((value * 1.25 + shippingCost) * 1.2); | |
| const bidAmountElement = document.getElementById("BidAmount"); | |
| if (bidAmountElement && bidAmountElement.type != "hidden") { | |
| const updateBidAmountTotal = () => { | |
| bidAmountElement.nextElementSibling.innerText = calculateTotal(getValue(bidAmountElement.value)); | |
| } | |
| bidAmountElement.oninput = updateBidAmountTotal; | |
| updateBidAmountTotal(); | |
| } | |
| const elementsToObserve = listingElement.querySelectorAll('span.NumberPart'); | |
| const observer = new MutationObserver(() => { | |
| for (const element of elementsToObserve) { | |
| updateObservedElementTotal(element, calculateTotal, shippingSource); | |
| } | |
| }); | |
| for (const element of elementsToObserve) { | |
| element.insertAdjacentHTML("afterEnd", "<span></span>"); | |
| updateObservedElementTotal(element, calculateTotal, shippingSource); | |
| observer.observe(element, { childList: true }); | |
| } | |
| } | |
| GM_setValue(CACHE_KEY, cache); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment