使用Tampermonkey插入控制按钮autoclicking复选框

问题描述 投票:1回答:1

我要创建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);

有人能帮我吗?

javascript jquery userscripts tampermonkey
1个回答
1
投票

所以,您要激活这些复选框选中火灾时,这些定时器和未选中时,取消计时器?

如果是这样的话:

  1. 添加一个侦听器change事件(使键盘和鼠标的工作)。
  2. 商店,使他们可以被删除的计时器的参考。
  3. 使用一个事件侦听器“干”维护的代码。添加data-属性,这样的代码知道哪个目标与复选框去。
  4. 不要添加节点那样的!

此代码的工作: (临提示:点击运行片断按钮后,但勾选复选框前,打出了“整页”​​链接,说按钮的右侧)

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 https://stackoverflow.com/q/15048223
        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>
© www.soinside.com 2019 - 2024. All rights reserved.