Skip to content

Instantly share code, notes, and snippets.

@adrianjagielak
Created July 1, 2024 14:10
Show Gist options
  • Save adrianjagielak/da4270cadf713d10367279a4c587dd04 to your computer and use it in GitHub Desktop.
Save adrianjagielak/da4270cadf713d10367279a4c587dd04 to your computer and use it in GitHub Desktop.
Empik 1.00pln book finder
// ==UserScript==
// @name Button Clicker and Price Checker
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Click buttons and check prices
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
async function waitForButtons(maxWaitTime = 20000) {
const startTime = Date.now();
while (Date.now() - startTime < maxWaitTime) {
const buttons = Array.from(document.querySelectorAll('button')).filter(button => button.textContent.trim() === 'Zarezerwuj w salonie');
if (buttons.length > 0) {
return buttons;
}
await new Promise(resolve => setTimeout(resolve, 100)); // Wait for 100ms
}
return [];
}
async function clickButtonsAndCheckPrices() {
const buttons = await waitForButtons();
if (buttons.length === 0) {
return; // No buttons found within the maximum wait time
}
for (const button of buttons) {
button.click();
const startTime = Date.now();
let priceSpanFound = false;
while (Date.now() - startTime < 2000) {
// Check if the page contains a span with the class 'ta-cac-price' and text contains ','
const priceSpan = document.querySelector('span.ta-cac-price');
if (priceSpan && priceSpan.textContent.trim().includes(',')) {
priceSpanFound = true;
break;
}
await new Promise(resolve => setTimeout(resolve, 100)); // Check every 100ms
}
if (priceSpanFound) {
const prices = document.querySelectorAll('span.ta-cac-price');
for (const price of prices) {
const priceText = price.textContent.trim().replace(',', '.');
if (parseFloat(priceText) <= 1.00) {
alert('Found a price 1,00 or lower!');
return; // Stop execution
}
}
}
// Close the dialog
const closeButton = document.querySelector('button.DrawerTitle__btn[data-ta="close-btn"]');
if (closeButton) {
closeButton.click();
}
await new Promise(resolve => setTimeout(resolve, 100)); // Wait for 100ms
}
// Click the 'next page' link and repeat
const nextPageButton = document.querySelector('a.arrow.next.ta-next-page');
if (nextPageButton) {
nextPageButton.click();
await clickButtonsAndCheckPrices(); // Repeat on the next page
}
}
clickButtonsAndCheckPrices(); // Start the process
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment