javascript - 使用 Tampermonkey 插入控制按钮自动点击的复选框

标签 javascript jquery userscripts tampermonkey

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

有人可以帮助我吗?

最佳答案

所以,您正尝试激活这些复选框以在选中时触发这些计时器并在未选中时取消计时器?

如果是:

  1. change 事件添加一个监听器(这样键盘和鼠标都可以工作)。
  2. 存储对计时器的引用,以便可以删除它们。
  3. 为“DRY”可维护代码使用一个事件监听器。添加 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>

关于javascript - 使用 Tampermonkey 插入控制按钮自动点击的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54521650/

相关文章:

javascript - 如何将 KendoUI 数据源 OData 参数发送到抽象的 RESTful AngularJS 数据工厂?

javascript - querySelectorAll() 在 Opera Mobile 11.5 中不起作用?

javascript - 仅应用jquery mobile 页面的一部分?

Javascript 滚动条 - 向下滚动但不向上滚动 clientY

javascript - 如何使用 jquery 获取警报的复选框值?

javascript - 将数组从 PHP 迭代传递到 JS

jquery - 当页面滚动到达特定id时添加类

javascript - 在不同的框中按 Enter 时防止按钮上的 onclick 事件触发

javascript - 如何使用 JavaScript 在页面上提交第一个表单?

javascript - 在 React 站点中使用搜索栏时,iOS 键盘不会关闭