使用 Tampermonkey 插入控制按钮自动点击的复选框
Use Tampermonkey to insert checkboxes that control autoclicking of buttons
我想创建 3 个复选框,选中这些复选框后会定期点击一个按钮(每个)。
左键和中键是同一种情况,只是美观不同。像这样:
<a id="fightButtonBerserk1" name="defender" value="Berserk" class="button foundation-style smallhelp profileButton fightButton" title="" href="#">
<i class="icon-friends"></i>Berserk! (5 golpes)</a>
右边的按钮是这样的:
<a id="fightButtonBerserk2" name="attacker" value="Berserk" class="button foundation-style smallhelp profileButton fightButton" title="" href="#">
<i class="icon-friends"></i>Berserk! (5 golpes)</a>
这是在我想要的位置插入复选框的代码,我目前拥有:
function addDomElements(){var a=$('<div class="foundation-radius fightContainer foundation-base-font"></div>'),
b=$('<input type="checkbox" class="smallerBox" id="EsquerdaHitBox" name="EsquerdaHitBox" style="margin:1px !important;line-height:10px !important;">'),
c=$('<label class="checkboxlabel" for="EsquerdaHitBox" style="margin-right: 20px;">Hit Left</label>'),
d=$('<input type="checkbox" class="smallerBox" id="MeioHitBox" name="MeioHitBox" style="margin:1px !important;line-height:10px !important;">'),
e=$('<label class="checkboxlabel" for="MeioHitBox" style="margin-right: 20px;">Hit Middle</label>'),
f=$('<input type="checkbox" class="smallerBox" id="DireitaHitkBox" name="DireitaHitkBox" style="margin:1px !important;line-height:10px !important;">'),
g=$('<label class="checkboxlabel" for="DireitaHitkBox" style="margin-right: 20px;">Hit Right</label>');
a.append(b),a.append(c),a.append(d),a.append(e),a.append(f),a.append(g),
$(a).insertAfter($('[class="foundation-radius fightContainer foundation-base-font"]')[1])}
我已经有一些功能只有一半的东西,但我需要在我想停止时一直关闭和打开它。
我想用每个复选框激活这种代码:
var button=document.getElementById("fightButtonBerserk1");
setInterval(function(){
button.click();
}, 30000);
var i = setInterval(function() {
window.location.reload();
}, 300000);
有人可以帮助我吗?
那么,您是否正在尝试激活这些复选框以在选中时触发这些计时器并在未选中时取消计时器?
如果是:
- 为
change
事件添加监听器(这样键盘和鼠标都可以工作)。
- 存储对计时器的引用以便删除它们。
- 为 "DRY" 可维护代码使用一个事件侦听器。添加一个
data-
属性,以便代码知道哪个目标与哪个复选框对应。
- 不要那样添加节点!
此代码有效:
(专业提示:点击 运行 片段按钮后,但在选中复选框之前,点击所述按钮右侧的 "Full page" link。)
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 Left</label>
<input type="checkbox" class="smallerBox" id="MeioHitBox" data-trgtSelctr="#fightButtonBerserk2">
<label class="checkboxlabel" for="MeioHitBox">Hit Middle</label>
<input type="checkbox" class="smallerBox" id="DireitaHitkBox" data-trgtSelctr="#fightButtonBerserk3">
<label class="checkboxlabel" for="DireitaHitkBox">Hit Right</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!`);
}
}
/********************************************************************
******* Everything below this block, including the HTML and *******
******* CSS blocks, is simulated target page. *******
******* It's NOT part of the userscript. *******
********************************************************************/
$("button").click (zEvent => {
console.log (`Button ${zEvent.target.textContent} clicked.`);
} );
.mySpamCntrols {
margin: 1em;
border: 1px solid green;
border-radius: 0.5ex;
padding: 1ex 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foundation-radius fightContainer foundation-base-font">fight 1
<button id="fightButtonBerserk1">Berserk 1</button>
<button id="fightButtonBerserk2">Berserk 2</button>
<button id="fightButtonBerserk3">Berserk 3</button>
</div>
<div class="foundation-radius fightContainer foundation-base-font">fight 2</div>
我想创建 3 个复选框,选中这些复选框后会定期点击一个按钮(每个)。
左键和中键是同一种情况,只是美观不同。像这样:
<a id="fightButtonBerserk1" name="defender" value="Berserk" class="button foundation-style smallhelp profileButton fightButton" title="" href="#">
<i class="icon-friends"></i>Berserk! (5 golpes)</a>
右边的按钮是这样的:
<a id="fightButtonBerserk2" name="attacker" value="Berserk" class="button foundation-style smallhelp profileButton fightButton" title="" href="#">
<i class="icon-friends"></i>Berserk! (5 golpes)</a>
这是在我想要的位置插入复选框的代码,我目前拥有:
function addDomElements(){var a=$('<div class="foundation-radius fightContainer foundation-base-font"></div>'),
b=$('<input type="checkbox" class="smallerBox" id="EsquerdaHitBox" name="EsquerdaHitBox" style="margin:1px !important;line-height:10px !important;">'),
c=$('<label class="checkboxlabel" for="EsquerdaHitBox" style="margin-right: 20px;">Hit Left</label>'),
d=$('<input type="checkbox" class="smallerBox" id="MeioHitBox" name="MeioHitBox" style="margin:1px !important;line-height:10px !important;">'),
e=$('<label class="checkboxlabel" for="MeioHitBox" style="margin-right: 20px;">Hit Middle</label>'),
f=$('<input type="checkbox" class="smallerBox" id="DireitaHitkBox" name="DireitaHitkBox" style="margin:1px !important;line-height:10px !important;">'),
g=$('<label class="checkboxlabel" for="DireitaHitkBox" style="margin-right: 20px;">Hit Right</label>');
a.append(b),a.append(c),a.append(d),a.append(e),a.append(f),a.append(g),
$(a).insertAfter($('[class="foundation-radius fightContainer foundation-base-font"]')[1])}
我已经有一些功能只有一半的东西,但我需要在我想停止时一直关闭和打开它。
我想用每个复选框激活这种代码:
var button=document.getElementById("fightButtonBerserk1");
setInterval(function(){
button.click();
}, 30000);
var i = setInterval(function() {
window.location.reload();
}, 300000);
有人可以帮助我吗?
那么,您是否正在尝试激活这些复选框以在选中时触发这些计时器并在未选中时取消计时器?
如果是:
- 为
change
事件添加监听器(这样键盘和鼠标都可以工作)。 - 存储对计时器的引用以便删除它们。
- 为 "DRY" 可维护代码使用一个事件侦听器。添加一个
data-
属性,以便代码知道哪个目标与哪个复选框对应。 - 不要那样添加节点!
此代码有效:
(专业提示:点击 运行 片段按钮后,但在选中复选框之前,点击所述按钮右侧的 "Full page" link。)
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 Left</label>
<input type="checkbox" class="smallerBox" id="MeioHitBox" data-trgtSelctr="#fightButtonBerserk2">
<label class="checkboxlabel" for="MeioHitBox">Hit Middle</label>
<input type="checkbox" class="smallerBox" id="DireitaHitkBox" data-trgtSelctr="#fightButtonBerserk3">
<label class="checkboxlabel" for="DireitaHitkBox">Hit Right</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!`);
}
}
/********************************************************************
******* Everything below this block, including the HTML and *******
******* CSS blocks, is simulated target page. *******
******* It's NOT part of the userscript. *******
********************************************************************/
$("button").click (zEvent => {
console.log (`Button ${zEvent.target.textContent} clicked.`);
} );
.mySpamCntrols {
margin: 1em;
border: 1px solid green;
border-radius: 0.5ex;
padding: 1ex 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foundation-radius fightContainer foundation-base-font">fight 1
<button id="fightButtonBerserk1">Berserk 1</button>
<button id="fightButtonBerserk2">Berserk 2</button>
<button id="fightButtonBerserk3">Berserk 3</button>
</div>
<div class="foundation-radius fightContainer foundation-base-font">fight 2</div>