Tampermonkey 复选框在重新加载页面后停止工作

Tampermonkey Checkbox stops working after reloading the page

我正在使用此 Tampermonkey 代码创建复选框,选中这些复选框后,会定期单击某些按钮并刷新页面。
该代码做对了,问题是在更新页面后它会取消选中复选框并停止。

我设法使复选框在代码的最后一部分保持选中状态,问题是在更新页面后即使选中了复选框它也不会继续执行这些功能,并且根据我在测试中观察到的情况取消选中该复选框并再次选中即可。

我认为问题出在代码的开头,但在到处搜索后我无法解决这个问题。据我所见,它仅在单击复选框时才会响应,而在自动选中时不会响应。

function addDomElements () {
    var newNodes = $( `
      <div class="mySpamCntrols foundation-radius fightContainer foundation-base-font">
        <input type="checkbox" class="smallerBox" id="EsquerdaHitBox" data-trgtSelctr="#fightButtonBerserk1">
        <label class="checkboxlabel" for="EsquerdaHitBox">Hit Esquerda</label>
        <input type="checkbox" class="smallerBox" id="MeioHitBox" data-trgtSelctr="#fightButtonBerserk1">
        <label class="checkboxlabel" for="MeioHitBox">Hit Meio</label>
        <input type="checkbox" class="smallerBox" id="DireitaHitkBox" data-trgtSelctr="#fightButtonBerserk2">
        <label class="checkboxlabel" for="DireitaHitkBox">Hit Direita</label>
      </div>
    ` );
    //-- Best to add a <style> node, but spam inline styles for now...
    newNodes.find ("input").attr ("style", "margin:1px !important;line-height:10px !important;");
    newNodes.find ("label").attr ("style", "margin-right: 20px;");

    newNodes.insertAfter($('.foundation-radius.fightContainer.foundation-base-font')[1] );

    //-- Activate checkboxes
    $(".mySpamCntrols").on ("change", "input[data-trgtSelctr]", processMyCheckBoxes);
}
addDomElements ();

function processMyCheckBoxes (zEvent) {
    var chkBox = zEvent.target;
    //-- If box is now checked, fire off an immediate click and start the timers
    if (chkBox.checked) {
        clickButtonByjQuerySelector (chkBox.dataset.trgtselctr);
        //chkBox.spamClckTmr   = setInterval (clickButtonByjQuerySelector, 30000, chkBox.dataset.trgtselctr);
        chkBox.spamClckTmr   = setInterval (clickButtonByjQuerySelector, 2222, chkBox.dataset.trgtselctr);
        chkBox.pageReloadTmr = setTimeout  (location.reload.bind (window.location), 300000);
    }
    //-- Else, if box is now not checked, cancel the timers
    else {
        clearInterval (chkBox.spamClckTmr);
        clearTimeout  (chkBox.pageReloadTmr);
    }
}

function clickButtonByjQuerySelector (jQuerySelector) {
    var targetNode = $(jQuerySelector)[0];
    if (targetNode) {
        console.log ("Clicking node: ", jQuerySelector);
        //-- Warning this my not always suffice.  See 
        targetNode.click ();
    }
    else {
        console.warn (`Node [${jQuerySelector}] not found!`);
    }
}

//Save Checkbox
$(function(){
    $('input:checkbox').each(function() {
        var $el = $(this);
        $el.prop('checked', sessionStorage[$el.prop('id')] === 'true');
    });

    $('input:checkbox').on('change', function() {
        var $el = $(this);
        sessionStorage[$el.prop('id')] = $el.is(':checked');
    });
});

如何让选中的复选框计时器在页面重新加载后重新启动?

您很接近,但设置 checked 属性 还不够。您还必须同步事件处理程序。

一个简单的方法就是让代码点击复选框。

此外,$(function(){ 包装器没有 need/use。

此代码有效:

//-- Restore and Save Checkboxes from sessionStorage:
$('input:checkbox').each (function () {
    var $el     = $(this);
    var elId    = $el.prop ('id');
    if (sessionStorage[elId] === 'true') {
        document.getElementById (elId).click ();
    }
} );

$('input:checkbox').on ('change', function () {
    var $el = $(this);
    sessionStorage[$el.prop ('id')] = $el.is (':checked');
} );