Please provide a keyboard shortcut for Trade Plan

Please provide a keyboard shortcut for Trade Plan in tv.dhan

Never mind, I spent my own time and resources and figured it out. Not expecting any help from Dhan…

@Conrad_Ray

  1. Install Tampermonkey browser extension.
  2. Install the following script to enable ALT+Q shortcut for “Trade Plan” button.
// ==UserScript==
// @name         Dhan – Trade Plan Shortcut
// @namespace    http://tampermonkey.net
// @version      1.0
// @description  Press Alt+Q to instantly open the Trade Plan panel on tv.dhan.co
// @author       You
// @match        https://tv.dhan.co/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const getIframeDocument = () => {
        try {
            return document.querySelector('iframe')?.contentDocument || null;
        } catch {
            return null;
        }
    };

    const openTradePlan = () => {
        let buttons = document.querySelectorAll('[class*="customButton"]');

        if (!buttons.length) {
            const iframeDoc = getIframeDocument();
            buttons = iframeDoc?.querySelectorAll('[class*="customButton"]') || [];
        }

        buttons[3]?.click();
    };

    const onKeyDown = (e) => {
        if (e.altKey && e.code === 'KeyQ') {
            e.preventDefault();
            e.stopImmediatePropagation();
            openTradePlan();
        }
    };

    const attachToIframe = () => {
        const iframeDoc = getIframeDocument();
        if (iframeDoc) {
            iframeDoc.addEventListener('keydown', onKeyDown, true);
        } else {
            setTimeout(attachToIframe, 1000);
        }
    };

    document.addEventListener('keydown', onKeyDown, true);
    window.addEventListener('keydown', onKeyDown, true);

    const iframe = document.querySelector('iframe');
    if (iframe) {
        iframe.addEventListener('load', attachToIframe);
        setTimeout(attachToIframe, 3000);
    } else {
        const observer = new MutationObserver(() => {
            const newIframe = document.querySelector('iframe');
            if (newIframe) {
                observer.disconnect();
                newIframe.addEventListener('load', attachToIframe);
                setTimeout(attachToIframe, 3000);
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

})();