Steam Wishlist Scraper

Scrapes steam wishlist into a CSV file for Excel / LibreOffice

目前為 2018-03-16 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Steam Wishlist Scraper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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';

    // Scrape titles and release dates
    var titles = document.getElementsByClassName('title');
    var dates = document.getElementsByClassName('value release_date');
    var output = [];
    var clean_output = [];
    for(var k in titles){
        var temp_title = titles[k].innerText;
        var temp_date = dates[k].innerText;
        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,"");
            output.push(temp_title.trim());
            output.push(temp_date.trim());
        }
    }
    output[0] = output[0].slice(1, output[0].length);
    clean_output = cleanArray(output);
    console.log(output);

    // Add CSV download button when we finish compiling the list
    window.addEventListener('load', () => {
    addButton('Download CSV', downloadCSV);
    });

    // 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]) {
                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};
        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;
        //var csv = output.toString();
        var csv = clean_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();
    }

})();