Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何自动选择照片单选按钮?_Javascript_Greasemonkey_Userscripts - Fatal编程技术网

Javascript 如何自动选择照片单选按钮?

Javascript 如何自动选择照片单选按钮?,javascript,greasemonkey,userscripts,Javascript,Greasemonkey,Userscripts,我正在尝试使用Greasemonkey脚本来自动选择单选按钮。投票有两个图像,当您单击一个图像时,将选中该选项,并启用验证码框进行投票 我想知道是否有办法自动选择我想要的图像。我尝试了一些命令,如.click和.checked,但没有成功 以下是投票中的部分源HTML: <fieldset> <legend>Validação de voto</legend> <div class="box-votacao"> <ul class="

我正在尝试使用Greasemonkey脚本来自动选择单选按钮。投票有两个图像,当您单击一个图像时,将选中该选项,并启用验证码框进行投票

我想知道是否有办法自动选择我想要的图像。我尝试了一些命令,如.click和.checked,但没有成功

以下是投票中的部分源HTML:

<fieldset>
<legend>Validação de voto</legend>
<div class="box-votacao">
    <ul class="duplo-lateral clearFix" id="participantes">

        <li class="off">
            <div class="foto">
                <a href="#">
                    <img class="foto-chamada" src="http://s.glbimg.com/et/bb/f/original/participantes/joao_carvalho_360x300.png" alt="João Carvalho">
                    <div class="selecionado"></div>
                    <p class="bloco-01 off">
                        <input name="opt" id="opt" value="1" type="radio">
                        <strong class="nome">João Carvalho</strong>
                    </p>
                </a>
            </div>
            <p class="legenda">Para votar em João Carvalho, disque 0303 108 8401 ou envie um SMS para 88401</p>
        </li>
        <li class="off ultimo">
            <div class="foto">
                <a href="#">
                    <img class="foto-chamada" src="http://s.glbimg.com/et/bb/f/original/participantes/yuri_360x300.png" alt="Yuri">
                    <div class="selecionado"></div>
                    <p class="bloco-01 off">
                        <input name="opt" id="opt" value="2" type="radio">
                        <strong class="nome">Yuri</strong>
                    </p>
                </a>
            </div>
            <p class="legenda">Para votar em Yuri, disque 0303 108 8402 ou envie um SMS para 88402</p>
        </li>

    </ul>
</div>
<div class="validacao clearFix noBorder">
    <div id="desabilitado"></div>
    <ul>
        <li>
            <img id="captcha" title="seguranca" />
        </li>
        <li class="digitar-codigo">
            <input name="answer" type="text" />
            <p id="erro" class="erro"><span>Erro!</span> <span class="erro-txt">Texto incorreto. Tente novamente.</span></p>
        </li>
        <li class="botao">
            <span><input name="votar" type="submit" class="submit" value="votar"/></span>
        </li>
    </ul>
</div>
</fieldset>

脚本如何确定要单击的图像

点击的是图像吗?假设这是真的,那么像这样的事情应该会起作用:

var targImage   = document.querySelector ("div.box-votacao img.foto-chamada[alt='João Carvalho']");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
targImage.dispatchEvent (clickEvent);
如果它不起作用,发布一个链接到目标页面,如果它是公共的。。。或者一个链接到页面的已保存快照(包括javascript),如果它不是公共的

更新,现在已经给出

那一页有点棘手;需要的不仅仅是点击一下。此外,检查页面时,被单击的不是图像,而是包含该内容的页面上的单击处理程序

因此,通过点击和一些JS的组合,以下内容已经过测试:

//--- Click the link and list-item associated with the desired image.
var targImg     = document.querySelector ("div.box-votacao img.foto-chamada[alt='João Carvalho']");
targLink        = targImg.parentNode;
targListItem    = targImg.parentNode.parentNode.parentNode;

clickNode (targListItem);

var ps          = unsafeWindow.ps;
for (var j in ps) {
    if (ps[j].className) {
        ps[j].className = ps[j].className.replace ("on", "off");
    }
}
targListItem.className  = targListItem.className.replace ("off", "on");
targListItem.getElementsByTagName ("input")[0].checked = "true";

var captchaBox  = document.querySelector ("#desabilitado");
if (captchaBox) {
    captchaBox.id = "habilitado";
}


function clickNode (targNode) {
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    targNode.dispatchEvent (clickEvent);
}

最后,这个页面可能被设计成阻止像Greasemonkey这样的脚本。如果是这样的话,您的脚本可能违反了服务条款,葡萄牙语读得不多,我没有去找他们,他们可能会采取对策来破坏此脚本。

我尝试的最后一个代码:code var name=document.getElementsByClassName'foto-chamada';名称[i]。单击;代码我会根据我要投票的代码进行更改。我尝试了你的代码,但没有成功。这是今天投票的链接,如果在你看到之前就关闭了,这是快照。一旦加载了页面,一旦选择了选项,就可以从页面pastebin.com/raw.php?i=98V7eMA6 thx中提前选择代码。在检查了该页面及其代码之后,我已经更新了答案。页面设计表明可能内置了基本的反脚本措施。请确保,不要使用此代码违反任何可能适用的法律或服务条款。非常感谢。投票已过期,我将在新投票出现时进行测试。我知道网站有这样的保护来避免机器人和宏,但我的目的是以身作则,而不是传播代码来违反任何东西。因为我相信还有另外一个网站也有类似的民意调查,就像这个例子中没有验证码,但是选择看起来很相似。单击也不起作用。假设我是对的,我会尝试根据你的代码为这一个做一个代码。只是测试了它并成功了,再次感谢你。我添加了此文档。getElementsByName'answer'[0]。焦点;