首页 > 解决方案 > 重新加载页面后 Tampermonkey Checkbox 停止工作

问题描述

我正在使用这个 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 https://stackoverflow.com/q/15048223
        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');
    });
});

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

标签: javascriptjqueryuserscriptstampermonkey

解决方案


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

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

此外,不需要/使用该$(function(){包装器。

此代码有效:

//-- 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');
} );

推荐阅读