javascriptjquerygmail

Bookmarklet in JavaScript to toggle Gmail conversation view


I want a bookmarklet to quickly toggle the Gmail conversation view on and off.

Starting from @seahorsepip's solution, I have:

javascript:window.location.href = "https://mail.google.com/mail/u/0/#settings/general";setTimeout(function(){document.querySelector("div.AO table tbody tr:nth-child(8) table:nth-child(2) td:nth-child(1) input:not(:checked)").click();document.querySelector("[guidedhelpid=save_changes_button]").click();}, 1000);

The problem is that I need two bookmarklets.

Selector .AO tr:nth-child(8) table:nth-child(1) input selects the "Conversation ON" button; and .AO tr:nth-child(8) table:nth-child(2) input selects "Conversation OFF".

Is there a way to have one bookmarklet that will check the one that is not checked? (If I run the "wrong" one I see Cannot read property 'click' of null in console.)


Solution

  • You could simply add a condition to your bookmarklet, checking if

    document.querySelector("div.AO table tbody tr:nth-child(8) table:nth-of-type(2) input").checked
    

    isn't null:

    window.location.href = "https://mail.google.com" + window.location.pathname + "#settings/general";
    sBase = "div.AO table tbody tr:nth-child(8) table:nth-of-type(";
    sOn = sBase + "1) input";
    sOff = sBase + "2) input";
    
    setTimeout(function(){
        if (document.querySelector(sOff).checked)
            document.querySelector(sOn).click();
        else
            document.querySelector(sOff).click();
        document.querySelector("[guidedhelpid=save_changes_button]").click();
    }, 1000);
    

    Giving:

    javascript:window.location.href="https://mail.google.com"+window.location.pathname+"#settings/general";sBase="div.AO table tbody tr:nth-child(8) table:nth-of-type(";sOn=sBase+"1) input";sOff=sBase+"2) input";setTimeout(function(){if(document.querySelector(sOff).checked) document.querySelector(sOn).click();else document.querySelector(sOff).click();document.querySelector("[guidedhelpid=save_changes_button]").click()},1000)