Search code examples
javascripttwitch

How to get method that changes name in javascript


I am making a script that changes settings on twitch.tv

When I need to check if a toggle button is checked, this is simple enough. But the method name to catch the true, false value on the .checked, changes every time I load the page.

var latencyButton = document.getElementsByClassName("tw-toggle__input")[0];

if(latencyButton.__reactEventHandlers$kupq2smnamk.checked) 
   latencyButton.click();
   console.log("Latency Changed");
}

It's the __reactEventHandlers$(This part) that changes so I have something to start off on.

Edit: added the HTML that surrounds the toggle button

<div data-test-selector="low-latency-toggle" class="tw-align-items-center tw-flex"><label class="tw-drop-down-menu-input-item__label tw-flex-grow-1 tw-mg-r-2" for="f8960cb3c79352dd6dd718358e42a812">Low Latency</label><div class="tw-toggle" data-test-selector="low-latency-toggle"><input type="checkbox" class="tw-toggle__input" id="f8960cb3c79352dd6dd718358e42a812" data-a-target="tw-toggle"><label for="f8960cb3c79352dd6dd718358e42a812" class="tw-toggle__button"><p class="tw-hide-accessible">Low Latency</p></label></div></div>

Is there a way to get the checked value without the name of the method? or like a way to get the method name to use in a variable?

Thanks for you time -Crysal


Solution

  • You could maybe try using filter and indexOf to find the function name. Something like:

    let el = document.querySelector(".tw-toggle__input");
    let reactHandlerName = Object.keys(el).filter((item) => item.indexOf('__reactEventHandlers')>=0);
    let reactHandler = el[reactHandlerName[0]]; // this should be the function name