Advertisement
kivan24

Untitled

Apr 22nd, 2024 (edited)
114
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Background Script - SteamGifts Giveaways
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Background script for retrieving and counting table column headings from SteamGifts Giveaways page
  6. // @author       Your Name
  7. // @match        https://www.steamgifts.com/giveaways/entered*
  8. // ==/UserScript==
  9.  
  10. console.log('Background script loaded.');
  11.  
  12. // Create the button
  13. let button = document.createElement('button');
  14. button.textContent = 'Retrieve Column Headings';
  15. button.style.position = 'fixed';
  16. button.style.left = '10px';
  17. button.style.bottom = '10px';
  18. button.style.zIndex = '9999';
  19. button.style.backgroundColor = '#4CAF50'; // Green background color
  20. button.style.color = 'white'; // White text color
  21. button.style.border = 'none'; // No border
  22. button.style.padding = '10px 20px'; // Padding around the text
  23. button.style.fontWeight = 'bold'; // Bold text
  24. button.style.cursor = 'pointer'; // Pointer cursor on hover
  25. document.body.appendChild(button);
  26.  
  27. let resultDisplay = document.createElement('div');
  28. resultDisplay.style.position = 'fixed';
  29. resultDisplay.style.left = '10px';
  30. resultDisplay.style.bottom = '50px';
  31. resultDisplay.style.zIndex = '9999';
  32. resultDisplay.style.backgroundColor = 'white';
  33. resultDisplay.style.padding = '10px';
  34. resultDisplay.style.border = '1px solid #ccc';
  35. resultDisplay.style.overflow = 'auto';
  36. resultDisplay.style.maxWidth = '400px';
  37. resultDisplay.style.maxHeight = '550px';
  38. document.body.appendChild(resultDisplay);
  39.  
  40. // Object to store the count of each table column heading across all pages
  41. let headingsCount = {};
  42.  
  43. // Function to retrieve column headings from a page
  44. async function retrieveColumnHeadings(page, endPage) {
  45.     console.log(`Retrieving column headings for page ${page}.`);
  46.     if (page > endPage) {
  47.         console.log('All pages processed. Sending result to content script.');
  48.         // If all pages are processed, send the final result to the content script
  49.         const sortedHeadings = Object.entries(headingsCount)
  50.             .sort(([, countA], [, countB]) => countB - countA) // Sort entries by count in descending order
  51.             .map(([heading, count]) => `${heading.replaceAll('<','&lt;').replaceAll('>','&gt;')} (Repeated ${count} times)<br>`) // Add line break after each entry
  52.             .join(''); // Join entries with line breaks
  53.         resultDisplay.innerHTML = sortedHeadings;
  54.         button.disabled = false;
  55.         return;
  56.     }
  57.  
  58.     let resp = await fetch(`https://www.steamgifts.com/giveaways/entered/search?page=${page}&sort=all`, {method: 'GET'})
  59.     console.log(`Received response for page ${page}.`);
  60.     resultDisplay.textContent = `Processing page ${page} of ${endPage}`
  61.     const parser = new DOMParser();
  62.     const doc = parser.parseFromString(await resp.text(), 'text/html');
  63.     const columnHeadings = doc.querySelectorAll('.table__column__heading');
  64.     columnHeadings.forEach(heading => {
  65.       let text = '';
  66.       const children = heading.childNodes;
  67.       children.forEach(child => {
  68.         if (child.nodeType === Node.TEXT_NODE) {
  69.           text += child.textContent.trim();
  70.         }
  71.       });
  72.       headingsCount[text] = (headingsCount[text] || 0) + 1;
  73.     });
  74.     // Continue retrieving column headings from the next page after a delay
  75.     setTimeout(() => {
  76.       retrieveColumnHeadings(page + 1, endPage, resultDisplay);
  77.     }, 1000); // 1 second delay
  78. }
  79.  
  80. // Click event handler for the button
  81. button.addEventListener('click', async () => {
  82.     headingsCount = {};
  83.     console.log('Button clicked. Initiating data retrieval.');
  84.     const endPage = parseInt(prompt('Enter the end page number:'), 10);
  85.     if (isNaN(endPage) || endPage < 1) {
  86.         alert('Invalid end page number. Please enter a valid number.');
  87.         return;
  88.     }
  89.     resultDisplay.textContent = "Processing..."
  90.     button.disabled = true; // Disable the button to prevent multiple clicks
  91.     retrieveColumnHeadings(1, endPage);
  92. });
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement