Advertisement
Guest User

Script1

a guest
Apr 14th, 2024
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | Source Code | 0 0
  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. // @grant GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. console.log('Background script loaded.');
  12.  
  13. // Create the button
  14. const button = document.createElement('button');
  15. button.textContent = 'Retrieve Column Headings';
  16. button.style.position = 'fixed';
  17. button.style.left = '10px';
  18. button.style.bottom = '10px';
  19. button.style.zIndex = '9999';
  20. button.style.backgroundColor = '#4CAF50'; // Green background color
  21. button.style.color = 'white'; // White text color
  22. button.style.border = 'none'; // No border
  23. button.style.padding = '10px 20px'; // Padding around the text
  24. button.style.fontWeight = 'bold'; // Bold text
  25. button.style.cursor = 'pointer'; // Pointer cursor on hover
  26. document.body.appendChild(button);
  27.  
  28. // Object to store the count of each table column heading across all pages
  29. const headingsCount = {};
  30.  
  31. // Function to retrieve column headings from a page
  32. function retrieveColumnHeadings(page, endPage, resultDisplay) {
  33. console.log(`Retrieving column headings for page ${page}.`);
  34. if (page > endPage) {
  35. console.log('All pages processed. Sending result to content script.');
  36. // If all pages are processed, send the final result to the content script
  37. const sortedHeadings = Object.entries(headingsCount)
  38. .sort(([, countA], [, countB]) => countB - countA) // Sort entries by count in descending order
  39. .map(([heading, count]) => `${heading} (Repeated ${count} times)\n`) // Add line break after each entry
  40. .join(''); // Join entries with line breaks
  41. window.postMessage({ type: 'SG_COLUMN_HEADINGS_RESULT', headings: sortedHeadings }, '*');
  42. return;
  43. }
  44.  
  45. GM_xmlhttpRequest({
  46. method: 'GET',
  47. url: `https://www.steamgifts.com/giveaways/entered/search?page=${page}&sort=all`,
  48. onload: function(response) {
  49. console.log(`Received response for page ${page}.`);
  50. const parser = new DOMParser();
  51. const doc = parser.parseFromString(response.responseText, 'text/html');
  52. const columnHeadings = doc.querySelectorAll('.table__column__heading');
  53. columnHeadings.forEach(heading => {
  54. const text = heading.textContent.trim();
  55. headingsCount[text] = (headingsCount[text] || 0) + 1;
  56. });
  57. // Continue retrieving column headings from the next page after a delay
  58. setTimeout(() => {
  59. retrieveColumnHeadings(page + 1, endPage, resultDisplay);
  60. }, 1000); // 1 second delay
  61. }
  62. });
  63. }
  64.  
  65. // Click event handler for the button
  66. button.addEventListener('click', () => {
  67. console.log('Button clicked. Initiating data retrieval.');
  68. const endPage = parseInt(prompt('Enter the end page number:'), 10);
  69. if (isNaN(endPage) || endPage < 1) {
  70. alert('Invalid end page number. Please enter a valid number.');
  71. return;
  72. }
  73. button.disabled = true; // Disable the button to prevent multiple clicks
  74. const resultDisplay = document.createElement('div');
  75. resultDisplay.style.display = 'none'; // Hide the result display
  76. document.body.appendChild(resultDisplay);
  77. resultDisplay.textContent = 'Retrieving data...'; // Display a message indicating data retrieval is in progress
  78. // Start retrieving column headings from the first page
  79. retrieveColumnHeadings(1, endPage, resultDisplay);
  80. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement