Advertisement
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.6.1
- // @description Script for retrieving and counting table column headings from SteamGifts Giveaways page, only from 2024 dates
- // @author meneldur with several codes from missingtexture, SquishedPotatoe, kivan, lav29, greatmastermario, ngoclong19
- // @match https://www.steamgifts.com/giveaways/entered*
- // ==/UserScript==
- console.log('Background script loaded.');
- // Function to ensure button styles are applied
- function styleButton(button) {
- button.classList.add('featured__action-button');
- button.style.position = 'fixed';
- button.style.bottom = '10px';
- button.style.left = '10px';
- button.style.zIndex = '9999';
- button.style.padding = '10px';
- button.style.backgroundColor = '#6BA9DB';
- button.style.color = 'white';
- button.style.border = 'none';
- button.style.borderRadius = '4px';
- button.style.cursor = 'pointer';
- }
- // Create the main button
- const button = document.createElement('button');
- button.textContent = 'Retrieve Data';
- styleButton(button);
- document.body.appendChild(button);
- // Create a button to copy the table data
- const copyButton = document.createElement('button');
- copyButton.textContent = 'Copy Data';
- copyButton.style.position = 'fixed';
- copyButton.style.bottom = '10px';
- copyButton.style.left = '150px';
- styleButton(copyButton);
- copyButton.classList.add('is-hidden');
- document.body.appendChild(copyButton);
- // Create a div element to display the result
- const resultDisplay = document.createElement('div');
- resultDisplay.style.cssText = `
- position: fixed;
- left: 10px;
- bottom: 50px;
- border: 1px solid #ccc;
- padding: 10px;
- background-color: white;
- max-width: 600px;
- max-height: 500px;
- overflow: auto;
- z-index: 9999;
- `;
- resultDisplay.classList.add('is-hidden');
- 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 = 1) {
- console.log(`Retrieving column headings for page ${page}.`);
- resultDisplay.textContent = `Processing page ${page}...`;
- let resp = await fetch(`https://www.steamgifts.com/giveaways/entered/search?page=${page}&sort=all`, {
- method: 'GET'
- });
- console.log(`Received response for page ${page}.`);
- const parser = new DOMParser();
- const doc = parser.parseFromString(await resp.text(), 'text/html');
- const innerWrap = doc.querySelectorAll('.table__row-inner-wrap');
- let continueRetrieving = true;
- innerWrap.forEach(wrap => {
- const dateSpan = wrap.querySelector('div > span[data-timestamp]');
- if (dateSpan) {
- const dateTimestamp = parseInt(dateSpan.getAttribute('data-timestamp'), 10) * 1000;
- const date = new Date(dateTimestamp);
- const year = date.getFullYear();
- // Only process entries from 2024; stop if we encounter an entry outside 2024
- if (year === 2024) {
- 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;
- } else {
- continueRetrieving = false;
- }
- }
- });
- if (continueRetrieving && innerWrap.length > 0) {
- // Continue retrieving column headings from the next page after a delay
- setTimeout(() => {
- retrieveColumnHeadings(page + 1);
- }, 1000); // 1 second delay
- } else {
- // Display results when done
- console.log('All pages processed. Sending result to content script.');
- displayResultsAsTable();
- button.disabled = false;
- }
- }
- // Function to display results as a table
- function displayResultsAsTable() {
- let entriesCountMap = {};
- const table = document.createElement('table');
- table.style.cssText = `border-collapse: collapse; width: 100%;`;
- table.innerHTML = `
- <style>
- tr:nth-child(odd) { background-color: #f2f2f2; }
- tr:nth-child(even) { background-color: white; }
- </style>
- `;
- const sortedHeadings = Object.entries(headingsCount)
- .sort(([keyTextA, countA], [keyTextB, countB]) => {
- if (countB !== countA) {
- return countB - countA;
- } else {
- if (keyTextA < keyTextB) return -1;
- if (keyTextA > keyTextB) return 1;
- return 0;
- }
- });
- let rowIndex = 1;
- sortedHeadings.forEach(([keyText, count]) => {
- const row = document.createElement('tr');
- const numCell = document.createElement('td');
- numCell.textContent = `${rowIndex++}.`;
- numCell.style.padding = '3px 8px';
- row.appendChild(numCell);
- 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: #6BA9DB;'>${key.text.replaceAll('<', '<').replaceAll('>', '>')}</a>`;
- const headingCell = document.createElement('td');
- headingCell.innerHTML = headingText;
- row.appendChild(headingCell);
- const countCell = document.createElement('td');
- countCell.textContent = ` (${count}x) `;
- countCell.style.paddingLeft = '10px';
- countCell.style.paddingRight = '8px';
- countCell.style.paddingTop = '3px';
- countCell.style.paddingBottom = '3px';
- row.appendChild(countCell);
- table.appendChild(row);
- entriesCountMap[count] = (entriesCountMap[count] || 0) + 1;
- });
- button.textContent = 'Retrieve Column Headings';
- resultDisplay.innerHTML = '';
- resultDisplay.appendChild(table);
- copyButton.classList.remove('is-hidden');
- const entriesCountTable = document.createElement('table');
- entriesCountTable.style.marginTop = '20px';
- entriesCountTable.style.textAlign = 'right';
- entriesCountTable.style.color = '#6BA9DB';
- entriesCountTable.innerHTML = `<style>td, th { padding: 3px 8px; }</style>`;
- const row = document.createElement('tr');
- const header1Cell = document.createElement('th');
- header1Cell.textContent = 'Entries';
- row.appendChild(header1Cell);
- const header2Cell = document.createElement('th');
- header2Cell.textContent = 'Games';
- row.appendChild(header2Cell);
- entriesCountTable.appendChild(row);
- Object.entries(entriesCountMap)
- .sort(([entrieA, ], [entrieB, ]) => entrieB - entrieA)
- .forEach(([entries, count]) => {
- const row = document.createElement('tr');
- const entriesCell = document.createElement('td');
- entriesCell.textContent = `${entries}x`;
- row.appendChild(entriesCell);
- const countCell = document.createElement('td');
- countCell.textContent = count;
- row.appendChild(countCell);
- entriesCountTable.appendChild(row);
- });
- resultDisplay.appendChild(entriesCountTable);
- }
- // Function to copy the table data
- async function copyTableData() {
- const input = prompt('Enter the row numbers or ranges you want to copy (e.g., "1,3-5,7"):');
- if (input === null) return;
- function parseRowIndices(input) {
- const indices = [];
- input.split(',').forEach(range => {
- if (range.includes('-')) {
- const [start, end] = range.split('-').map(Number);
- for (let i = start; i <= end; i++) {
- indices.push(i);
- }
- } else {
- indices.push(Number(range));
- }
- });
- return indices;
- }
- const rowIndices = parseRowIndices(input);
- const table = resultDisplay.querySelector('table');
- let data = [];
- table.querySelectorAll('tr').forEach((row, index) => {
- if (rowIndices.includes(index + 1)) {
- let rowData = [];
- row.querySelectorAll('td').forEach((cell) => {
- rowData.push(cell.textContent.trim());
- });
- data.push(rowData.join('\t'));
- }
- });
- await navigator.clipboard.writeText(data.join('\n'));
- alert('Data copied to clipboard!');
- }
- copyButton.addEventListener('click', copyTableData);
- button.addEventListener('click', async () => {
- headingsCount = {};
- resultDisplay.innerHTML = '';
- resultDisplay.classList.remove('is-hidden');
- button.disabled = true;
- await retrieveColumnHeadings(1);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement