Search code examples
javascripthtmlnetflix

Logging in to the Netflix website using JavaScript


I'm trying to write a wrapper for the Netflix web page in Qt using QWebEngine so that I can use my remote control to navigate. For those who didn't know, the Netflix website can't even be navigated using the arrow keys...

So, anyway, I have used QWebChannel to inject some JavaScript code into the web page, and can (visually, at least) modify the relevant elements:

document.getElementsByName("email")[0].value = "%1";
document.getElementsByName("password")[0].value = "%2";
document.getElementsByClassName("btn login-button btn-submit btn-small")[0].click();

This actually works (I can see the fields filled with what I provide for %1 and %2, and the button is pressed programmatically), except for one crucial issue: this results in the messages below the input forms telling me "Please enter a valid email." and "Your password must contain between 4 and 60 characters.". These tell me somehow just setting the HTML elements' values doesn't have the same effect as me manually typing in the values. Could someone help me figure out why this doesn't work, and how I can make it work? I would like to restrict myself to plain JavaScript, it seems like a simple enough task to achieve without e.g. jQuery or some other Javascript library.

I understand this is a terrible way to approach the whole Netflix-on-a-HTPC thing, but I don't want to go digging through e.g. Flix2Kodi's Python to figure out what they are doing (which seems to me is a lot more susceptible to bad breakage than the end result I'm aiming for).


Solution

  • The input field for the email uses some sort of HTML5 and ReactJS validation mix.
    However it seems like ReactJS validation cant handle the the dynamic value change, so I tried to find a way to deactivate it, which I did not directly, but I guessed that it has to add some sort of event handler to the form so I came up with this:

    var validatingForm = document.getElementsByClassName("simple-login-form")[0];
    var nonValidatingForm = document.getElementsByClassName("simple-login-form")[0].cloneNode(true);
    validatingForm.parentNode.replaceChild(nonValidatingForm, validatingForm);
    

    which gets rid of all event handlers and therefore ReactJS's validation. Now you can set your value using your code:

    document.getElementsByName("email")[0].value = "%1";
    document.getElementsByClassName("btn login-button btn-submit btn-small")[0].click();
    

    Note that HTML5 is still validating the inputs, so you have to provide an E-Mail Adress, if you want to get rid of that too set the input type to text before changing the value:

    document.getElementsByName("email")[0].setAttribute("type", "text");
    

    However the next page after the Button click asks for the password so you'll have to provide it there as I didn't find a way around this.

    Buuuuuttt could you not have saved the password in your browser, let it do it's autofill work and fire the click event only?