Greasy Fork

Steam Hotkeys

Add hotkeys to navigate the Steam Discovery Queue

当前为 2017-07-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         Steam Hotkeys
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  Add hotkeys to navigate the Steam Discovery Queue
// @author       Lex
// @match        *://store.steampowered.com/*
// @match        *://steamcommunity.com/*
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==

(function($, undefined) {
    'use strict';

    // ASCII number or "a" letter
    var Hotkeys = new Map([
        ["ADD_TO_WISHLIST", "w"],
        ["NOT_INTERESTED", "s"],
        ["NEXT_QUEUE", ["d", "n"]],
        ["CLICK_REVIEWS", "e"],
        ["INVENTORY", "i"],
        ["PROFILE", "c"],
        ["ACTIVITY", "u"],
        ["CHAT", "l"],
        ["HOME", "h"],
        ["EXPLORE", "x"],
    ]);

    var ONE_CLICK_ADVANCE = true;
    var HOTKEYS_ENABLED = true;

    function loadSettings() {
        ONE_CLICK_ADVANCE = GM_getValue("ONE_CLICK_ADVANCE", ONE_CLICK_ADVANCE);
        HOTKEYS_ENABLED = GM_getValue("HOTKEYS_ENABLED", HOTKEYS_ENABLED);
    }

    function saveSettings() {
        GM_setValue("ONE_CLICK_ADVANCE", ONE_CLICK_ADVANCE);
        GM_setValue("HOTKEYS_ENABLED", HOTKEYS_ENABLED);
    }

    // Returns true if currently browsing a queue
    function inQueue() {
        return Boolean(document.querySelector("div.next_in_queue_content"));
    }

    function clickNextQueue() {
        document.querySelectorAll("div.btn_next_in_queue, #refresh_queue_btn, .queue_actions_ctn a[href='http://store.steampowered.com/explore/']")[0].click();
    }

    function doc_keyUp(e) {
        if (!HOTKEYS_ENABLED)
            return;
        if (document.activeElement.tagName === 'TEXTAREA' || document.activeElement.tagName === 'TEXT')
            return;
        if (Hotkeys.get("ADD_TO_WISHLIST").indexOf(e.keyCode) !== -1) {
            let rm = document.querySelector("#add_to_wishlist_area_success:not([style*='none'])");
            let add = document.querySelector("#add_to_wishlist_area:not([style*='none']) a");
            (rm || add).click();
            if (ONE_CLICK_ADVANCE && inQueue()) {
                let keyElement;
                if (rm !== null)
                    keyElement = "#add_to_wishlist_area:not([style*='none']) a";
                else if (add !== null)
                    keyElement = "#add_to_wishlist_area_success:not([style*='none'])";
                waitForKeyElements(keyElement, clickNextQueue, true);
            }
        } else if (Hotkeys.get("NOT_INTERESTED").indexOf(e.keyCode) !== -1) {
            document.querySelector("div.queue_btn_ignore div:not([style*='none'])").click();
            if (ONE_CLICK_ADVANCE && inQueue()) {
                waitForKeyElements("div.queue_btn_active:not([style*='none'])", clickNextQueue, true);
            }
        } else if (Hotkeys.get("NEXT_QUEUE").indexOf(e.keyCode) !== -1) {
            if (inQueue() || document.querySelector("#refresh_queue_btn") !== null)
                clickNextQueue();
        } else if (Hotkeys.get("CLICK_REVIEWS").indexOf(e.keyCode) !== -1) {
            if (checkVisible(document.getElementById("app_reviews_hash"), 20, "below"))
                window.location = "#app_reviews_hash";
            else
                window.scrollTo(0, 0);
        } else if (Hotkeys.get("INVENTORY").indexOf(e.keyCode) !== -1) {
            window.location = "https://steamcommunity.com/my/inventory/";
        } else if (Hotkeys.get("PROFILE").indexOf(e.keyCode) !== -1) {
            window.location = "https://steamcommunity.com/my/profile/";
        } else if (Hotkeys.get("ACTIVITY").indexOf(e.keyCode) !== -1) {
            window.location = "https://steamcommunity.com/my/home/";
        } else if (Hotkeys.get("CHAT").indexOf(e.keyCode) !== -1) {
            window.location = "https://steamcommunity.com/chat/";
        } else if (Hotkeys.get("HOME").indexOf(e.keyCode) !== -1) {
            window.location = "https://store.steampowered.com/";
        } else if (Hotkeys.get("EXPLORE").indexOf(e.keyCode) !== -1) {
            window.location = "https://store.steampowered.com/explore/";
        }
    }

    // Adds settings to the Customize your queue dialog box
    function addHotkeySettings() {
        var enableChecked = HOTKEYS_ENABLED ? `checked="checked"` : ``;
        var autoAdvance = ONE_CLICK_ADVANCE ? `checked="checked"` : ``;
        var HotKeysSettings = `<div style="float:right; width:50%"><div class="dq_settings_section_title">Hotkeys:</div>
                  <div class="dq_settings_checkrow"><input type="checkbox" id="sdqhotkeys_enabled" ${enableChecked}><label for="sdqhotkeys_enabled">Hotkeys enabled</label></div>
                  <div class="dq_settings_checkrow"><input type="checkbox" id="sdqhotkeys_autoadvance" ${autoAdvance}><label for="sdqhotkeys_autoadvance" title="Automatically go to the next game after adding to wishlist or marking not interested">Auto advance</label></div>
             </div>`;
        $(HotKeysSettings).prependTo($("div.dq_settings_content div.dq_settings_section:first"));
        $("#sdqhotkeys_enabled").change(function(){ HOTKEYS_ENABLED = Boolean(this.checked); saveSettings(); });
        $("#sdqhotkeys_autoadvance").change(function(){ ONE_CLICK_ADVANCE = Boolean(this.checked); saveSettings(); });
    }

    function checkVisible(elm, threshold, mode) {
        threshold = threshold || 0;
        mode = mode || 'visible';

        var rect = elm.getBoundingClientRect();
        var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
        var above = rect.bottom - threshold < 0;
        var below = rect.top - viewHeight + threshold >= 0;

        return mode === 'above' ? above : (mode === 'below' ? below : !above && !below);
    }

    function main() {
        // Convert string hotkeys to char codes
        for (let [key, value] of Hotkeys) {
            if (!Array.isArray(value))
                value = [value];
            for (let i = 0; i < value.length; i++) {
                if (typeof value[i] === 'string')
                    value[i] = value[i].toUpperCase().charCodeAt(0);
            }
            Hotkeys.set(key, value);
        }

        loadSettings();
        waitForKeyElements("div.dq_settings_content", addHotkeySettings, false);

        window.addEventListener('keydown', doc_keyUp, false);
    }

    main();
})(jQuery);