Last active
May 17, 2023 10:16
-
-
Save alufers/6a50408ab6eec6cd3a65666235762641 to your computer and use it in GitHub Desktop.
Pepper/mydealz/allegro disk price per GB userscript
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 Pepper parser | |
| // @namespace http://tampermonkey.net/ | |
| // @version 0.1 | |
| // @description Parse product listings on pepper.pl | |
| // @author ChatGPT | |
| // @match https://www.pepper.pl/* | |
| // @match https://www.mydealz.de/*\ | |
| // @match https://allegro.pl/* | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=pepper.pl | |
| // @grant none | |
| // ==/UserScript== | |
| (function () { | |
| "use strict"; | |
| function getSelectors() { | |
| switch (document.location.host) { | |
| case "allegro.pl": | |
| case "www.allegro.pl": | |
| return { | |
| threadList: ".opbox-sheet", | |
| threadGrid: ".opbox-sheet article", | |
| threadPrice: "span[aria-label~=cena]", | |
| threadTitle: "h2 a", | |
| visited: "thread-visited", | |
| }; | |
| default: | |
| return { | |
| threadGrid: ".threadGrid:not(.userscript-visited)", | |
| threadPrice: ".thread-price", | |
| threadTitle: ".js-thread-title", | |
| voteTemp: ".vote-temp", | |
| expiredThreads: ".cept-show-expired-threads", | |
| visited: "thread-visited", | |
| threadList: ".js-threadList", | |
| }; | |
| } | |
| } | |
| let SELECTORS = getSelectors(); | |
| function addPricePerGBMarkers() { | |
| const extractedData = []; | |
| document.querySelectorAll(`${SELECTORS.threadGrid}`).forEach((thread) => { | |
| if (thread.querySelector("." + SELECTORS.visited)) return; | |
| const threadPriceEl = thread.querySelector(SELECTORS.threadPrice); | |
| const threadTitleEl = thread.querySelector(SELECTORS.threadTitle); | |
| const voteTempEl = SELECTORS.voteTemp | |
| ? thread.querySelector(SELECTORS.voteTemp) | |
| : null; | |
| const expiredEl = SELECTORS.expiredThreads | |
| ? thread.querySelector(SELECTORS.expiredThreads) | |
| : null; | |
| if (!threadPriceEl || !threadTitleEl) { | |
| return; | |
| } | |
| const priceString = threadPriceEl.textContent.trim(); | |
| const price = parseInt( | |
| priceString.replace(/[^0-9,]/g, "").replace(",", ".") | |
| ); | |
| const currency = priceString.replace(/[0-9,]/g, "").trim(); | |
| let score = null; | |
| if (voteTempEl) { | |
| score = parseInt(voteTempEl.textContent.trim().replace(/[^0-9-]/g, "")); | |
| } | |
| const title = threadTitleEl.textContent.trim(); | |
| const isExpired = expiredEl ? Boolean(expiredEl) : false; | |
| const capacity = title.match( | |
| /\b(?:\d+(?:\.\d+)?\s*(?:GB|TB)|\d+\s*TB\s*\d+\s*GB)\b/i | |
| ); | |
| const size = capacity ? capacity[0].toLowerCase().replace(/\s/g, "") : ""; | |
| let sizeInGB = ""; | |
| if (size.includes("tb")) { | |
| sizeInGB = parseInt(size.replace("tb", "")) * 1000; | |
| } else if (size.includes("gb")) { | |
| sizeInGB = parseInt(size.replace("gb", "")); | |
| } | |
| if (title.match(/laptop|konsol|tablet|Windows (11|10)|Ryzen/i)) { | |
| return; | |
| } | |
| const pricePerGB = sizeInGB ? (price / sizeInGB).toFixed(4) : null; | |
| extractedData.push({ | |
| price, | |
| currency, | |
| title, | |
| score, | |
| isExpired, | |
| sizeInGB, | |
| pricePerGB, | |
| domNode: thread, | |
| }); | |
| // Add element to display pricePerGB after the thread title | |
| const pricePerGBEl = document.createElement("span"); | |
| pricePerGBEl.style.color = isExpired ? "gray" : "red"; | |
| pricePerGBEl.textContent = pricePerGB | |
| ? ` (${pricePerGB} ${currency}/GB)` | |
| : ""; | |
| pricePerGBEl.classList.add(SELECTORS.visited); | |
| threadTitleEl.insertAdjacentElement("afterend", pricePerGBEl); | |
| }); | |
| } | |
| addPricePerGBMarkers(); | |
| const threadList = document.querySelector(SELECTORS.threadList); | |
| if (threadList) { | |
| const observer = new MutationObserver((mutations) => { | |
| mutations.forEach((mutation) => { | |
| mutation.addedNodes.forEach((node) => { | |
| if ( | |
| node.classList && | |
| (node.classList.contains("threadGrid") || | |
| node.classList.contains("thread")) | |
| ) { | |
| addPricePerGBMarkers(); | |
| } | |
| }); | |
| }); | |
| }); | |
| observer.observe(threadList, { childList: true }); | |
| window.addPricePerGBMarkers = addPricePerGBMarkers; | |
| setInterval(addPricePerGBMarkers, 1500); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment