-1

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dan
  • 1,257
  • 2
  • 15
  • 31

1 Answers1

1

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • 1
    My first version was missing a `)`, can you try with the new version by reloading the page? – Zenoo Mar 09 '18 at 13:49
  • Thanks! That works to switch from ON to OFF; but if run it again I get `Cannot read property 'click' of null` – Dan Mar 09 '18 at 13:53
  • 1
    Can you add `console.log(document.querySelector("[guidedhelpid=save_changes_button]"));` before `document.querySelector("[guidedhelpid=save_changes_button]").click();` to see if the problems comes from here? Tell me what you get in your console. – Zenoo Mar 09 '18 at 13:57
  • `` but only when switching from ON to OFF; otherwise `Uncaught TypeError: Cannot read property 'click' of null at :1:471` – Dan Mar 09 '18 at 14:15
  • I was testing without even using the 'save' line; just to see if the toggling works. And it only worked going from ON to OFF. – Dan Mar 09 '18 at 14:15
  • 1
    @Ze'ev Give me some time to debug this, I'll get back to you. – Zenoo Mar 09 '18 at 14:17
  • Freakin' sweet! You rock! – Dan Mar 09 '18 at 16:51
  • I added a bit to make it work in any of multiple G accounts – Dan Mar 09 '18 at 17:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166572/discussion-between-zeev-and-zenoo). – Dan Mar 10 '18 at 00:51
  • This doesn't work anymore ... has anyone tweaked it for latest Gmail? – Dan Feb 18 '19 at 08:56