Search code examples
javascripthtmlif-statementcontent-script

Loop not activating on keypress [Chrome Extension]


So in my content script, the statement where is says "currenturlval == 'gmail'" does not seem to actually run at all.

var currenturlval;
var geturl = window.location.toString();
if (geturl.includes('twitter.')) {
    currenturlval = 'twitter';
} else if (geturl.includes('mail.google.')) {
    currenturlval = 'gmail';
    console.log(currenturlval);
} else if (geturl.includes('facebook.')) {
    currenturlval = 'facebook';
} else {
    currenturlval = 'default';
}
document.body.onkeydown = function(){
    if(connected == true) {
        keycurrent = keycurrent + 1;
        //initial react DOM render
        if (boolinitial == false) {
            renderOverlay();
            renderDropdown();
            boolinitial = true;
        }
        if (currenturlval == 'twitter') {
            var twitterinput = document.querySelector('[contenteditable]:focus > div');
            extractinputdata(twitterinput);
        } else if (currenturlval == 'gmail') {
            console.log("test");
            var gmailinput = document.querySelector('[aria-label="Message Body"][role="textbox"]');
            extractinputdata(gmailinput);
        }
        keycheck();
    }
};

No error messages pop-up in the console, and this loop works for other sites (ie. twitter and facebook). Not sure why gmail does not work.


Solution

  • Update
    ok, downvote, fair enough, the answer is not correct, pls ignore the previous answer. The code part for the first if loop, works fine in the console when google mail is open. It logs 'gmail'.

    I can't help with the second part, variables missing and can't reproduce. The error is in the second part, I guess.

    If I log:

    var gmailinput = document.querySelector('[aria-label="Message Body"][role="textbox"]');
    

    it returns NULL.

    Initial answer:
    I dont know what you want to do and I am missing context to fully understand, but I think the issue is because of this line (geturl.includes('mail.google.')) { currenturlval = 'gmail'; Change it to (geturl.includes('mail')) { currenturlval = 'gmail';