Bundle Stars Keys Retrieve

Retrieve keys from Bundle Stars

目前為 2017-04-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bundle Stars Keys Retrieve
// @namespace    http://tampermonkey.net/
// @version      1.1.2
// @description  Retrieve keys from Bundle Stars
// @icon         https://cdn.bundlestars.com/production/brand/apple-touch-icon-180x180.png
// @author       Bisumaruko
// @include      http*://*bundlestars.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

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

    function getAnchor() {
        var anchor = $('h2');

        return anchor && anchor.textContent.trim() === 'Order Keys' ? anchor : null;
    }

    function setup() {
        var anchor = getAnchor();

        if (!anchor) return;
        
        var BSRetrive = document.createElement('div'),
            BSTextarea = document.createElement('textarea'),
            BSBtnContainer = document.createElement('div'),
            BSBtnReveal = document.createElement('button'),
            BSBtnRetrieve = document.createElement('button'),
            BSBtnCopy = document.createElement('button'),
            BSBtnReset = document.createElement('button'),
            BSCheckTitle = document.createElement('label'),
            BSCheckJoin = document.createElement('label'),
    
            style = document.createElement('style');
    
        BSRetrive.classList.add('BSRetrive');
        BSTextarea.classList.add('BSTextarea');
        BSBtnContainer.classList.add('BSBtnContainer');
        BSBtnReveal.classList.add('BSBtnReveal');
        BSBtnReveal.textContent = 'Reveal';
        BSBtnRetrieve.classList.add('BSBtnRetrieve');
        BSBtnRetrieve.textContent = 'Retrieve';
        BSBtnCopy.classList.add('BSBtnCopy');
        BSBtnCopy.textContent = 'Copy';
        BSBtnReset.classList.add('BSBtnReset');
        BSBtnReset.textContent = 'Reset';
    
        BSCheckTitle.innerHTML = '<input type="checkbox" class="BSCheckTitle">Include Game Title';
        BSCheckJoin.innerHTML = '<input type="checkbox" class="BSCheckJoin">Join Keys';
    
        style.type = 'text/css';
        style.innerHTML = `
            .BSRetrive {
                width: 100%;
                height: 200px;
                display: flex;
                flex-direction: column;
                box-sizing: border-box;
                border: 1px solid #424242;
                color: #999999;
            }
            .BSTextarea {
                width: 100%;
                height: 150px;
                border: none;
                background-color: #303030;
                color: #DDD;
                box-sizing: border-box;
                resize: none;
            }
            .BSBtnContainer {
                width: 100%;
                display: flex;
                padding-top: 5px;
                flex-grow: 1;
                box-sizing: border-box;
            }
            .BSBtnContainer > button {
                height: 34px;
                margin-right: 10px;
                padding: 6px 12px;
                border: 1px solid transparent;
                background-color: #262626;
                color: #dedede;
                box-sizing: border-box;
                outline: none;
                cursor: pointer;
            }
            .BSBtnContainer > button:hover {
                color: #A8A8A8;
            }
            .BSBtnContainer > label {
                margin-right: 10px;
                color: #dedede;
            }
        `;
    
        BSBtnReveal.addEventListener('click', () => {
            var keys = $$('a[ng-click^="redeemSerial"]');
    
            for (let key of keys) {
                if (key.parentNode.classList.contains('ng-hide')) continue;
                if (!key.closest('td').classList.contains('key-container')) continue;
                key.click();
            }
        });
    
        BSBtnRetrieve.addEventListener('click', () => {
            var keys = [],
                containers = $$('td.key-container'),
                separator = $('.BSCheckJoin').checked ? ',' : "\n";
    
            for (let container of containers) {
                let key = container.querySelector('input'),
                    title = container.previousElementSibling;

                if (!key) continue;

                key = key.value.trim();
                if (title && $('.BSCheckTitle').checked) key = title.textContent.trim() + ', ' + key;
                keys.push(key);
            }
    
            BSTextarea.textContent = keys.join(separator);
        });
    
        BSBtnCopy.addEventListener('click', () => {
            BSTextarea.select();
    		document.execCommand('copy');
        });
    
        BSBtnReset.addEventListener('click', () => {
            BSTextarea.textContent = '';
        });
    
        document.head.appendChild(style);
        BSBtnContainer.appendChild(BSBtnReveal);
        BSBtnContainer.appendChild(BSBtnRetrieve);
        BSBtnContainer.appendChild(BSBtnCopy);
        BSBtnContainer.appendChild(BSBtnReset);
        BSBtnContainer.appendChild(BSCheckTitle);
        BSBtnContainer.appendChild(BSCheckJoin);
        BSRetrive.appendChild(BSTextarea);
        BSRetrive.appendChild(BSBtnContainer);
        anchor.parentNode.insertBefore(BSRetrive, anchor);
    }

    function observeOrderDetails(target) {
        new MutationObserver(mutations => {
            for (let mutation of mutations) {
                if (!mutation.addedNodes.length) continue;
                if (mutation.addedNodes[0].classList.contains('ng-scope')) setup();
            }
        }).observe(target, {childList: true});
    }

    var anchor = getAnchor();

    if (anchor) {
        setup();
        observeOrderDetails(anchor.closest('body > div.ng-scope'));
    } else {
        var bodyObserver = new MutationObserver(mutations => {
            for (let mutation of mutations) {
                if (!mutation.addedNodes.length) continue;
                if (mutation.addedNodes[0].classList.contains('ng-scope')) {
                    observeOrderDetails(mutation.addedNodes[0]);
                    bodyObserver.disconnect();
                }
            }
        });

        bodyObserver.observe(document.body, {childList: true});
    }
})();