Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Background Script - SteamGifts Giveaways
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description Background script for retrieving and counting table column headings from SteamGifts Giveaways page
- // @author Your Name
- // @match https://www.steamgifts.com/giveaways/entered
- // @grant GM_xmlhttpRequest
- // ==/UserScript==
- console.log('Background script loaded.');
- // Create the button
- const button = document.createElement('button');
- button.textContent = 'Retrieve Column Headings';
- button.style.position = 'fixed';
- button.style.left = '10px';
- button.style.bottom = '10px';
- button.style.zIndex = '9999';
- button.style.backgroundColor = '#4CAF50'; // Green background color
- button.style.color = 'white'; // White text color
- button.style.border = 'none'; // No border
- button.style.padding = '10px 20px'; // Padding around the text
- button.style.fontWeight = 'bold'; // Bold text
- button.style.cursor = 'pointer'; // Pointer cursor on hover
- document.body.appendChild(button);
- // Object to store the count of each table column heading across all pages
- const headingsCount = {};
- // Function to retrieve column headings from a page
- function retrieveColumnHeadings(page, endPage, resultDisplay) {
- 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, send the final result to the content script
- const sortedHeadings = Object.entries(headingsCount)
- .sort(([, countA], [, countB]) => countB - countA) // Sort entries by count in descending order
- .map(([heading, count]) => `${heading} (Repeated ${count} times)\n`) // Add line break after each entry
- .join(''); // Join entries with line breaks
- window.postMessage({ type: 'SG_COLUMN_HEADINGS_RESULT', headings: sortedHeadings }, '*');
- return;
- }
- GM_xmlhttpRequest({
- method: 'GET',
- url: `https://www.steamgifts.com/giveaways/entered/search?page=${page}&sort=all`,
- onload: function(response) {
- console.log(`Received response for page ${page}.`);
- const parser = new DOMParser();
- const doc = parser.parseFromString(response.responseText, 'text/html');
- const columnHeadings = doc.querySelectorAll('.table__column__heading');
- columnHeadings.forEach(heading => {
- const text = heading.textContent.trim();
- headingsCount[text] = (headingsCount[text] || 0) + 1;
- });
- // Continue retrieving column headings from the next page after a delay
- setTimeout(() => {
- retrieveColumnHeadings(page + 1, endPage, resultDisplay);
- }, 1000); // 1 second delay
- }
- });
- }
- // Click event handler for the button
- button.addEventListener('click', () => {
- 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;
- }
- button.disabled = true; // Disable the button to prevent multiple clicks
- const resultDisplay = document.createElement('div');
- resultDisplay.style.display = 'none'; // Hide the result display
- document.body.appendChild(resultDisplay);
- resultDisplay.textContent = 'Retrieving data...'; // Display a message indicating data retrieval is in progress
- // Start retrieving column headings from the first page
- retrieveColumnHeadings(1, endPage, resultDisplay);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement