JavaScript中的Bookmarklet用于切换Gmail对话视图

时间:2018-03-09 13:36:09

标签: javascript jquery gmail

我想要一个书签来快速切换Gmail会话视图。

@seahorsepip's solution开始,我有:

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

问题是我需要两个书签。

选择器.AO tr:nth-child(8) table:nth-child(1) input选择"对话开"按钮;并.AO tr:nth-child(8) table:nth-child(2) input选择"对话关闭"。

有没有办法让一个书签会检查未检查的书签? (如果我运行"错误"我在控制台中看到Cannot read property 'click' of null。)

1 个答案:

答案 0 :(得分:1)

您只需在书签中添加条件,然后检查是否

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

不是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);

,并提供:

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)