Guest User

Script1

a guest
Apr 22nd, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Entered Giveaway Counter - SteamGifts Giveaways
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3.1
  5. // @description Script for retrieving and counting table column headings from SteamGifts Giveaways page
  6. // @author
  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.cssText = `position:fixed; left:10px; bottom:10px;`;
  16. button.style.fontWeight = 'bold';
  17. button.classList.add('featured__action-button');
  18. document.body.appendChild(button);
  19.  
  20. let resultDisplay = document.createElement('div');
  21. resultDisplay.style.cssText = `position:fixed; left:10px; bottom:50px; border: 1px solid #ccc; padding:10px`;
  22. resultDisplay.style.backgroundColor = 'white';
  23. resultDisplay.style.overflow = 'auto';
  24. resultDisplay.style.maxWidth = '22dvw';
  25. resultDisplay.style.maxHeight = '76dvh';
  26. document.body.appendChild(resultDisplay);
  27.  
  28. // Object to store the count of each table column heading across all pages
  29. let headingsCount = {};
  30.  
  31. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  32. let mutationObserver = new MutationObserver(() => { resultDisplay.style.overflowY = 'auto' });
  33.  
  34. // Function to retrieve column headings from a page
  35. async function retrieveColumnHeadings(page, endPage) {
  36. console.log(`Retrieving column headings for page ${page}.`);
  37. if (page > endPage) {
  38. console.log('All pages processed. Sending result to content script.');
  39. // If all pages are processed, create the table and display it in the result display
  40. displayResultsAsTable();
  41. button.disabled = false;
  42. mutationObserver.observe(resultDisplay, { childList: true, subtree: true });
  43. return;
  44. }
  45.  
  46. let resp = await fetch(`https://www.steamgifts.com/giveaways/entered/search?page=${page}&sort=all`, { method: 'GET' });
  47. console.log(`Received response for page ${page}.`);
  48. resultDisplay.textContent = `Processing page ${page} of ${endPage}`;
  49. const parser = new DOMParser();
  50. const doc = parser.parseFromString(await resp.text(), 'text/html');
  51.  
  52. const innerWrap = doc.querySelectorAll('.table__row-inner-wrap');
  53. innerWrap.forEach(wrap => {
  54. let key = { text: wrap.querySelector('.table__column__heading').childNodes[0].textContent.trim(), appID: 0 };
  55. let thumbnail = wrap.querySelector('.table_image_thumbnail');
  56. if (thumbnail) {
  57. key.appID = thumbnail.style.backgroundImage.substring(48);
  58. key.appID = key.appID.substring(0, key.appID.search('/'));
  59. }
  60. let keyText = JSON.stringify(key);
  61. headingsCount[keyText] = (headingsCount[keyText] || 0) + 1;
  62. });
  63.  
  64. // Continue retrieving column headings from the next page after a delay
  65. setTimeout(() => {
  66. retrieveColumnHeadings(page + 1, endPage);
  67. }, 1000); // 1 second delay
  68. }
  69.  
  70. // Function to display results as a table
  71. function displayResultsAsTable() {
  72. // Create a table element
  73. const table = document.createElement('table');
  74. table.style.borderCollapse = 'collapse'; // Collapse borders
  75.  
  76. // Create the table header row
  77. const headerRow = document.createElement('tr');
  78. const headers = ['', ''];
  79. headers.forEach(header => {
  80. const th = document.createElement('th');
  81. th.appendChild(document.createTextNode(header));
  82. th.style.border = '1px solid #ccc'; // Add border to table header cell
  83. th.style.padding = '8px'; // Add padding to table header cell
  84. headerRow.appendChild(th);
  85. });
  86. table.appendChild(headerRow);
  87.  
  88. // Sort the headings by count in descending order and populate the table
  89. const sortedHeadings = Object.entries(headingsCount)
  90. .sort(([, countA], [, countB]) => countB - countA)
  91. .forEach(([keyText, count]) => {
  92. // Create a new row
  93. const row = document.createElement('tr');
  94.  
  95. // Get the key object and text
  96. const key = JSON.parse(keyText);
  97. const headingText = key.appID === 0
  98. ? key.text.replaceAll('<', '&lt;').replaceAll('>', '&gt;')
  99. : `<a href='https://store.steampowered.com/app/${key.appID}/' style='color:blue'>${key.text.replaceAll('<', '&lt;').replaceAll('>', '&gt;')}</a>`;
  100.  
  101. // Create a cell for the heading text
  102. const headingCell = document.createElement('td');
  103. headingCell.innerHTML = headingText;
  104. headingCell.style.border = '1px solid #ccc'; // Add border to table cell
  105. headingCell.style.padding = '8px'; // Add padding to table cell
  106. row.appendChild(headingCell);
  107.  
  108. // Create a cell for the repeat count
  109. const countCell = document.createElement('td');
  110. countCell.textContent = count;
  111. countCell.style.border = '1px solid #ccc'; // Add border to table cell
  112. countCell.style.padding = '8px'; // Add padding to table cell
  113. row.appendChild(countCell);
  114.  
  115. // Append the row to the table
  116. table.appendChild(row);
  117. });
  118.  
  119. // Clear the existing content in the result display and append the table
  120. resultDisplay.innerHTML = '';
  121. resultDisplay.appendChild(table);
  122.  
  123. // Add dashed gray lines between each result
  124. const rows = resultDisplay.querySelectorAll('tr');
  125. rows.forEach((row, index) => {
  126. if (index > 0) {
  127. row.style.borderTop = '1px dashed #ccc';
  128. }
  129. });
  130. }
  131.  
  132. // Click event handler for the button
  133. button.addEventListener('click', async () => {
  134. headingsCount = {};
  135. mutationObserver.disconnect();
  136.  
  137. console.log('Button clicked. Initiating data retrieval.');
  138. const endPage = parseInt(prompt('Enter the end page number:'), 10);
  139. if (isNaN(endPage) || endPage < 1) {
  140. alert('Invalid end page number. Please enter a valid number.');
  141. return;
  142. }
  143. resultDisplay.textContent = "Processing...";
  144. button.disabled = true; // Disable the button to prevent multiple clicks
  145. retrieveColumnHeadings(1, endPage);
  146. });
Advertisement
Add Comment
Please, Sign In to add comment