Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Entered Giveaway Counter - SteamGifts Giveaways
- // @namespace http://tampermonkey.net/
- // @version 0.3.1
- // @description Script for retrieving and counting table column headings from SteamGifts Giveaways page
- // @author
- // @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.cssText = `position:fixed; left:10px; bottom:10px;`;
- button.style.fontWeight = 'bold';
- button.classList.add('featured__action-button');
- document.body.appendChild(button);
- let resultDisplay = document.createElement('div');
- resultDisplay.style.cssText = `position:fixed; left:10px; bottom:50px; border: 1px solid #ccc; padding:10px`;
- resultDisplay.style.backgroundColor = 'white';
- resultDisplay.style.overflow = 'auto';
- resultDisplay.style.maxWidth = '22dvw';
- resultDisplay.style.maxHeight = '76dvh';
- document.body.appendChild(resultDisplay);
- // Object to store the count of each table column heading across all pages
- let headingsCount = {};
- var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
- let mutationObserver = new MutationObserver(() => { resultDisplay.style.overflowY = 'auto' });
- // 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, create the table and display it in the result display
- displayResultsAsTable();
- button.disabled = false;
- mutationObserver.observe(resultDisplay, { childList: true, subtree: true });
- 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 innerWrap = doc.querySelectorAll('.table__row-inner-wrap');
- innerWrap.forEach(wrap => {
- let key = { text: wrap.querySelector('.table__column__heading').childNodes[0].textContent.trim(), appID: 0 };
- let thumbnail = wrap.querySelector('.table_image_thumbnail');
- if (thumbnail) {
- key.appID = thumbnail.style.backgroundImage.substring(48);
- key.appID = key.appID.substring(0, key.appID.search('/'));
- }
- let keyText = JSON.stringify(key);
- headingsCount[keyText] = (headingsCount[keyText] || 0) + 1;
- });
- // Continue retrieving column headings from the next page after a delay
- setTimeout(() => {
- retrieveColumnHeadings(page + 1, endPage);
- }, 1000); // 1 second delay
- }
- // Function to display results as a table
- function displayResultsAsTable() {
- // Create a table element
- const table = document.createElement('table');
- table.style.borderCollapse = 'collapse'; // Collapse borders
- // Create the table header row
- const headerRow = document.createElement('tr');
- const headers = ['', ''];
- headers.forEach(header => {
- const th = document.createElement('th');
- th.appendChild(document.createTextNode(header));
- th.style.border = '1px solid #ccc'; // Add border to table header cell
- th.style.padding = '8px'; // Add padding to table header cell
- headerRow.appendChild(th);
- });
- table.appendChild(headerRow);
- // Sort the headings by count in descending order and populate the table
- const sortedHeadings = Object.entries(headingsCount)
- .sort(([, countA], [, countB]) => countB - countA)
- .forEach(([keyText, count]) => {
- // Create a new row
- const row = document.createElement('tr');
- // Get the key object and text
- const key = JSON.parse(keyText);
- const headingText = key.appID === 0
- ? key.text.replaceAll('<', '<').replaceAll('>', '>')
- : `<a href='https://store.steampowered.com/app/${key.appID}/' style='color:blue'>${key.text.replaceAll('<', '<').replaceAll('>', '>')}</a>`;
- // Create a cell for the heading text
- const headingCell = document.createElement('td');
- headingCell.innerHTML = headingText;
- headingCell.style.border = '1px solid #ccc'; // Add border to table cell
- headingCell.style.padding = '8px'; // Add padding to table cell
- row.appendChild(headingCell);
- // Create a cell for the repeat count
- const countCell = document.createElement('td');
- countCell.textContent = count;
- countCell.style.border = '1px solid #ccc'; // Add border to table cell
- countCell.style.padding = '8px'; // Add padding to table cell
- row.appendChild(countCell);
- // Append the row to the table
- table.appendChild(row);
- });
- // Clear the existing content in the result display and append the table
- resultDisplay.innerHTML = '';
- resultDisplay.appendChild(table);
- // Add dashed gray lines between each result
- const rows = resultDisplay.querySelectorAll('tr');
- rows.forEach((row, index) => {
- if (index > 0) {
- row.style.borderTop = '1px dashed #ccc';
- }
- });
- }
- // Click event handler for the button
- button.addEventListener('click', async () => {
- headingsCount = {};
- mutationObserver.disconnect();
- 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