Steam Wishlist Scraper

Scrapes steam wishlist into a CSV file for Excel / LibreOffice

当前为 2018-03-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         Steam Wishlist Scraper
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Scrapes steam wishlist into a CSV file for Excel / LibreOffice
// @author       [email protected]
// @match        http://store.steampowered.com/wishlist/profiles/*
// @grant        none
// ==/UserScript==

( function() {
    'use strict';

    var titles = [];
    var dates = [];

    var final_output = [];

    checkData();

    function scrollScrape(){
        if ((window.innerHeight + window.scrollY) < document.body.offsetHeight) {
            scrapeData();
            setTimeout(function() {
                scrollScrape();
            }, 100);
        } else {
            console.log("End");
            doneScraping();
        }
    }

    function scrapeData(){
        // Scrape titles and release dates
        titles = document.getElementsByClassName('title');
        dates = document.getElementsByClassName('value release_date');
        var output = [];
        var clean_output = [];
        var data = false;
        console.log(titles);
        for(var i = 0; i < titles.length; i++){
            var temp_title = titles[i].innerText;
            var temp_date = dates[i].innerText;
            console.log(temp_title);
            console.log(temp_date);
            if (typeof temp_title != 'undefined' && temp_title.length != 0 && typeof temp_date != 'undefined' && temp_date.length != 0){
                // Strip commas, add "^" for newlines
                temp_title = "^" + temp_title.replace(/,/g,"");
                temp_date = temp_date.replace(/,/g,"");
                temp_title = temp_title.trim();
                temp_date = temp_date.trim();
                if ( !final_output.includes(temp_title)){
                    output.push(temp_title);
                    output.push(temp_date);
                }
            }
        }
        console.log(output);
        //if (output.length != 0){
        //output[0] = output[0].slice(1, output[0].length);
        clean_output = cleanArray(output);
        //console.log(clean_output);
        final_output = final_output.concat(clean_output);
        console.log(final_output);
        //}
        window.scrollBy(0, 500);
    }

    function checkData(){
        titles = document.getElementsByClassName('title');
        dates = document.getElementsByClassName('value release_date');
        if (typeof titles[0] == 'undefined' || titles[0] == null){
            console.log("waiting for wishlist.js...");
            setTimeout(function() {
                checkData();
            }, 1000);
        } else {
            console.log("wishlist.js completed.");
            scrollScrape();
        }
    }

    // Removes "undefined" and null entries in the array, trims whitespace
    function cleanArray(arr) {
        var len = arr.length, i;

        for(i = 0; i < len; i++ ) {
            if (arr[i] && typeof arr[i] != 'undefined') {
                arr.push(arr[i]);  // copy non-empty values to the end of the array
            }
        }

        arr.splice(0 , len);  // cut the array and leave only the non-empty values

        return arr;
    }

    // Add a button
    function addButton(text, onclick, cssObj) {
        //cssObj = cssObj || {position: 'absolute', top: '7%', left:'4%', 'z-index': 3};
        cssObj = {position: 'absolute', top: '25px', left:'25px', 'z-index': 3};
        let button = document.createElement('button'), btnStyle = button.style;
        document.body.appendChild(button);
        button.innerHTML = text;
        button.onclick = onclick;
        Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key]);
        return button;
    }

    // Generate CSV file and download
    function downloadCSV(args) {
        var data, filename, link;
        final_output[0] = final_output[0].slice(1, final_output[0].length);
        var csv = final_output.toString();
        if (csv == null) return;

        console.log(csv);

        // Replaces "|" with newlines
        csv = csv.replace( /\^/g, "\n");
        csv = csv.replace(/,,/g, ',');

        console.log(csv);

        filename = 'wishlist.csv';

        if (!csv.match(/^data:text\/csv/i)) {
            csv = 'data:text/csv;charset=utf-8,' + csv;
        }
        data = encodeURI(csv);

        link = document.createElement('a');
        link.setAttribute('href', data);
        link.setAttribute('download', filename);
        link.click();
    }

        function doneScraping(){
        window.scrollTo(0,0);
        // Add CSV download button when we finish compiling the list
       // window.addEventListener('load', () => {
       //     addButton('Download CSV', downloadCSV);
        //});
        console.log(final_output);
        console.log("Done!");
        downloadCSV();
    }

})();