Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Background Script - SteamGifts Giveaways
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description Background script for retrieving and counting table column headings from SteamGifts Giveaways page
- // @author Your Name
- // @match https://www.steamgifts.com/giveaways/entered*
- // ==/UserScript==
- console.log('Background script loaded.');
- // Create the button
- let button = document.createElement('button');
- button.textContent = 'Retrieve Column Headings';
- button.style.position = 'fixed';
- button.style.left = '10px';
- button.style.bottom = '10px';
- button.style.zIndex = '9999';
- button.style.backgroundColor = '#4CAF50'; // Green background color
- button.style.color = 'white'; // White text color
- button.style.border = 'none'; // No border
- button.style.padding = '10px 20px'; // Padding around the text
- button.style.fontWeight = 'bold'; // Bold text
- button.style.cursor = 'pointer'; // Pointer cursor on hover
- document.body.appendChild(button);
- let resultDisplay = document.createElement('div');
- resultDisplay.style.position = 'fixed';
- resultDisplay.style.left = '10px';
- resultDisplay.style.bottom = '50px';
- resultDisplay.style.zIndex = '9999';
- resultDisplay.style.backgroundColor = 'white';
- resultDisplay.style.padding = '10px';
- resultDisplay.style.border = '1px solid #ccc';
- resultDisplay.style.overflow = 'auto';
- resultDisplay.style.maxWidth = '400px';
- resultDisplay.style.maxHeight = '550px';
- document.body.appendChild(resultDisplay);
- // Object to store the count of each table column heading across all pages
- let headingsCount = {};
- // Function to retrieve column headings from a page
- async function retrieveColumnHeadings(page, endPage) {
- console.log(`Retrieving column headings for page ${page}.`);
- if (page > endPage) {
- console.log('All pages processed. Sending result to content script.');
- // If all pages are processed, send the final result to the content script
- const sortedHeadings = Object.entries(headingsCount)
- .sort(([, countA], [, countB]) => countB - countA) // Sort entries by count in descending order
- .map(([heading, count]) => `${heading.replaceAll('<','<').replaceAll('>','>')} (Repeated ${count} times)<br>`) // Add line break after each entry
- .join(''); // Join entries with line breaks
- resultDisplay.innerHTML = sortedHeadings;
- button.disabled = false;
- return;
- }
- let resp = await fetch(`https://www.steamgifts.com/giveaways/entered/search?page=${page}&sort=all`, {method: 'GET'})
- console.log(`Received response for page ${page}.`);
- resultDisplay.textContent = `Processing page ${page} of ${endPage}`
- const parser = new DOMParser();
- const doc = parser.parseFromString(await resp.text(), 'text/html');
- const columnHeadings = doc.querySelectorAll('.table__column__heading');
- columnHeadings.forEach(heading => {
- let text = '';
- const children = heading.childNodes;
- children.forEach(child => {
- if (child.nodeType === Node.TEXT_NODE) {
- text += child.textContent.trim();
- }
- });
- headingsCount[text] = (headingsCount[text] || 0) + 1;
- });
- // Continue retrieving column headings from the next page after a delay
- setTimeout(() => {
- retrieveColumnHeadings(page + 1, endPage, resultDisplay);
- }, 1000); // 1 second delay
- }
- // Click event handler for the button
- button.addEventListener('click', async () => {
- headingsCount = {};
- console.log('Button clicked. Initiating data retrieval.');
- const endPage = parseInt(prompt('Enter the end page number:'), 10);
- if (isNaN(endPage) || endPage < 1) {
- alert('Invalid end page number. Please enter a valid number.');
- return;
- }
- resultDisplay.textContent = "Processing..."
- button.disabled = true; // Disable the button to prevent multiple clicks
- retrieveColumnHeadings(1, endPage);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement