Robinhood QOL Trading

A Robinhood quality of life script

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

// ==UserScript==
// @name        Robinhood QOL Trading
// @namespace   Trading
// @match       https://robinhood.com/legend/layout/*
// @grant       none
// @version     1.2
// @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) {
    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.toString());

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

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

async function sell(shares) {
    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();
}

let shares = 0;

let debounce = false;

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

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

            if (res || res == "0") {
                shares = parseFloat(res);
            }
        }

        debounce = false;
    }
});

QingJ © 2025

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