Bookmarklet in JavaScript to toggle Gmail conversation view

泄露秘密 提交于 2019-12-25 18:56:08

问题


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


回答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)


来源:https://stackoverflow.com/questions/49195033/bookmarklet-in-javascript-to-toggle-gmail-conversation-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!