我正在使用此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');
});
});
在重新加载页面后,如何使选定的复选框计时器重新启动?
答案 0 :(得分:1)
您已经接近,但是仅设置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');
} );