DailyIndieGame Tool

Add handy features to DailyIndieGame

目前為 2017-05-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         DailyIndieGame Tool
// @namespace    http://tampermonkey.net/
// @version      2.0.1
// @description  Add handy features to DailyIndieGame
// @icon         http://www.dailyindiegame.com/dailyindiegame.png
// @author       Bisumaruko
// @include      http*://*dailyindiegame.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const pathname = location.pathname;

    var $ = selector => document.querySelector(selector),
        $$ = selector => Array.from(document.querySelectorAll(selector));

    if (pathname.includes('/account_page')) {
        let style = document.createElement('style');

        style.type = 'text/css';
        style.innerHTML = `
            .DIGRetrive {
                padding: 5px;
                box-sizing: border-box;
                border: 1px solid #424242;
            }
            .DIGRetrive > textarea {
                width: 100%;
                height: 150px;
                box-sizing: border-box;
                resize: none;
            }
            .DIGRetrive > div {
                padding-top: 5px;
            }
            .DIGRetrive button {
                width: 130px;
                height: 25px;
                margin-right: 10px;
                border: none;
                background-color: #FD5E0F;
                color: rgb(49, 49, 49);
                font-family: Ropa Sans;
                font-size: 15px;
                font-weight: 600;
                outline: none;
                cursor: pointer;
            }
            .DIGRetrive label {
                margin-right: 10px;
            }
        `;

        document.head.appendChild(style);

        let DIGRetrive = document.createElement('div');

        DIGRetrive.className = 'DIGRetrive';
        DIGRetrive.innerHTML = `
            <textarea></textarea>
            <div>
                <button class="DIGButtonRetrieve">Retrieve</button>
                <button class="DIGButtonCopy">Copy</button>
                <button class="DIGButtonReset">Reset</button>
                <label><input type="checkbox" class="DIGCheckboxTitle">Include Game Title</label>
                <label><input type="checkbox" class="DIGCheckboxJoin">Join Keys</label>
            </div>
        `;

        $('#TableKeys').parentNode.insertBefore(DIGRetrive, $('#TableKeys'));

        $('.DIGButtonRetrieve').addEventListener('click', () => {
            let keys = [],
                trs = $$('#TableKeys tr'),
                separator = $('.DIGCheckboxJoin').checked ? ',' : "\n";

            for (let tr of trs) {
                let tds = tr.querySelectorAll('td'),
                    title = tds[2].textContent.trim(),
                    key = tds[4].textContent.trim();

                if (!key.includes('-')) continue;
                if ($('.DIGCheckboxTitle').checked) key = title + ', ' + key;
                keys.push(key);
            }

            $('.DIGRetrive > textarea').textContent = keys.join(separator);
        });

        $('.DIGButtonCopy').addEventListener('click', () => {
            $('.DIGRetrive > textarea').select();
    		document.execCommand('copy');
        });

        $('.DIGButtonReset').addEventListener('click', () => {
            $('.DIGRetrive > textarea').textContent = '';
        });
    }

    if (pathname === '/account_digstore.html' || pathname === '/account_trades.html') {
        let style = document.createElement('style');

        style.type = 'text/css';
        style.innerHTML = `
            .DIGEasyBuy button {
                padding: 4px 8px;
                outline: none;
            }
            .DIGEasyBuy_checkbox {
                display: none;
            }
            .DIGEasyBuy_checked {
                background-color: #222; !important
            }
        `;

        document.head.appendChild(style);

        let target = $('#form3').closest('tr').firstElementChild,
            DIGEasyBuy = document.createElement('div');

        DIGEasyBuy.className = 'DIGEasyBuy';
        DIGEasyBuy.innerHTML = `
            <button class="DIGButtonPurchase DIG3_Orange_15_Form">Purchase</button>
            <button class="DIGButtonSelectAll DIG3_Orange_15_Form">Select All</button>
        `;

        while (target.lastChild) {
            target.removeChild(target.lastChild);
        }

        target.appendChild(DIGEasyBuy);

        $('.DIGButtonPurchase').addEventListener('click', function () {
            let purchase = async function () {
                let list = $$('.DIGEasyBuy_checkbox:checked'),
                    headers = new Headers(),
                    bought = 0,
                    balance = parseInt($('a[href^="account_transac"]').closest('div').textContent.slice(12)) || 0;

                headers.append('Content-Type', 'application/x-www-form-urlencoded');

                for (let item of list) {
                    let id = parseInt(item.dataset.id),
                        price = parseInt(item.dataset.price),
                        res;

                    if (!id || isNaN(price)) continue;
                    if (balance - price < 0) {
                        msg.alert('Not enough DIG points');
                        break;
                    }

                    let url = 'account_buy.html',
                        init = {
                            method: 'POST',
                            headers: headers,
                            body: 'quantity=1&xgameid=' + id + '&xgameprice1=' + price + '&send=Purchase',
                            mode: 'same-origin',
                            credentials: 'same-origin',
                            cache: 'no-store',
                            referrer: location.origin + '/account_buy_' + id + '.html'
                        };

                    if (pathname === '/account_trades.html') {
                        url = 'account_buytrade_' + id + '.html';
                        init.body = 'gameid=' + id + '&send=Purchase';
                        init.referrer = location.origin + '/account_buytrade_' + id + '.html';
                    }

                    try {
                        res = await fetch(url, init);

                        if (res.ok) {
                            item.click();
                            bought++;
                            balance -= price;
                        } else msg.alert('Purchase failed, server responded an error');
                    } catch (e) {
                        console.log(e);
                        msg.alert('An error occured while making purchase request');
                    }
                }

                if (bought) window.location = location.origin + '/account_page.html';
            };

            try {
                purchase();
            } catch (e) {
                console.log(e);
                msg.alert('An error occured while preparing pruchase');
            }
        });

        $('.DIGButtonSelectAll').addEventListener('click', function () {
            let checkboxes = $$('.DIGEasyBuy_checkbox');

            for (let checkbox of checkboxes) {
                if (this.textContent === 'Select All' && !checkbox.checked) checkbox.click();
                if (this.textContent === 'Cancel' && checkbox.checked) checkbox.click();
            }
            this.textContent = this.textContent === 'Select All' ? 'Cancel' : 'Select All';
        });

        let games = $$('a[href^="account_buy"]');

        for (let game of games) {
            let row = game.closest('tr'),
                checkbox = document.createElement('input');

            checkbox.type = 'checkbox';
            checkbox.className = 'DIGEasyBuy_checkbox';
            checkbox.dataset.id = game.href.replace(/\D/g,'');
            checkbox.dataset.price = parseInt(game.closest('td').previousElementSibling.textContent) || 0;
            checkbox.addEventListener('change', function () {
                let row = this.closest('tr');

                if (this.checked) row.classList.add('DIGEasyBuy_checked');
                else row.classList.remove('DIGEasyBuy_checked');
            });

            row.firstElementChild.appendChild(checkbox);
            row.addEventListener('click', function () {
                this.querySelector('input[type="checkbox"]').click();
            });
        }
    }

    var msg = {
        box: null,
        init() {
            let style = document.createElement('style');

            style.type = 'text/css';
            style.innerHTML = `
                .DIGTool_msg {
                    display: none;
                    position: fixed;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    padding: 10px 20px;
                    border: 1px solid #424242;
                    background-color: rgb(32, 32, 32);
                    color: #FFF;
                    font-size: larger;
                }
                .DIGTool_msg-show {
                    display: block;
                }
            `;
            document.head.appendChild(style);

            let DIGTool_msg = document.createElement('div');

            DIGTool_msg.classList.add('DIGTool_msg');
            document.body.appendChild(DIGTool_msg);
            this.box = DIGTool_msg;
        },
        alert(text) {
            this.box.textContent = text;
            this.box.classList.add('DIGTool_msg-show');
            setTimeout(this.hide.bind(this), 3000);
        },
        hide() {
            this.box.classList.remove('DIGTool_msg-show');
        }
    };

    msg.init();

})();