Robinhood QOL Trading

A Robinhood quality of life script

当前为 2025-01-08 提交的版本,查看 最新版本

// ==UserScript==
// @name        Robinhood QOL Trading
// @namespace   Trading
// @match       https://robinhood.com/legend/layout/*
// @grant       none
// @version     1.0
// @license     MIT
// @author      AdSam
// @description A Robinhood quality of life script
// ==/UserScript==

const TIMEOUT = 5;

const BUY_KEY = "b";
const SELL_KEY = "s";
const SET_SHARES_KEY = "z";

function WaitForElement(selector, index, timeout = 0) {
    let iter = 0;

    return new Promise((resolve, reject) => {
        const interval = setInterval(() => {
            if (timeout != 0 && iter >= timeout * 1000) {
                reject(new Error("Timeout"));
            }

            const elements = document.querySelectorAll(selector);

            if (elements[index]) {
                clearInterval(interval);
                resolve(elements[index]);
            }

            iter++;
        }, 1);
    });
}

async function buy(shares) {
    try {
        menuBtn = await WaitForElement(
            '[data-testid="legend_buy_button"]',
            0,
            TIMEOUT
        );
        menuBtn.click();

        dropdown = await WaitForElement('[role="combobox"]', 0, TIMEOUT);
        dropdown.click();

        market = await WaitForElement('[role="option"]', 1, TIMEOUT);
        market.click();

        quantity = await WaitForElement('[name="quantity"]', 0, TIMEOUT);
        quantity.focus();

        document.execCommand("selectAll");
        document.execCommand("insertText", false, shares);

        while (quantity.value != shares) {}

        buyBtn = await WaitForElement('[type="submit"]', 0, TIMEOUT);
        buyBtn.click();
    } catch {
        return 0;
    }

    return shares;
}

async function sell(shares) {
    try {
        menuBtn = await WaitForElement(
            '[data-testid="legend_sell_button"]',
            0,
            TIMEOUT
        );
        menuBtn.click();

        dropdown = await WaitForElement('[role="combobox"]', 0, TIMEOUT);
        dropdown.click();

        market = await WaitForElement('[role="option"]', 1, TIMEOUT);
        market.click();

        quantity = await WaitForElement('[name="quantity"]', 0, TIMEOUT);
        quantity.focus();

        document.execCommand("selectAll");
        document.execCommand("insertText", false, shares.toString());

        while (quantity.value != shares.toString()) {}

        sellBtn = await WaitForElement('[type="submit"]', 0, TIMEOUT);
        sellBtn.click();
    } catch {
        return 0;
    }

    return shares;
}

let shares = 0;
let totalShares = 0;

let debounce = false;

document.addEventListener("keydown", async (event) => {
    if (!debounce) {
        debounce = true;

        if (event.key == BUY_KEY) {
            totalShares += await buy(shares);
        } else if (event.key == SELL_KEY) {
            totalShares -= await sell(shares);
        } else if (event.key == SET_SHARES_KEY) {
            const res = parseFloat(prompt("Enter number of shares:"));

            if (res != null) {
                shares = res;
            }
        }

        debounce = false;
    }
});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址