2012-03-12 76 views
1

我想要做一个Greasemonkey脚本来自动选择一个单选按钮。该调查有两个图像,当您单击一个图像时,该选项被选中,并且CAPTCHA框启用投票。如何自动选择照片单选按钮?

我想知道是否有任何方法来自动选择我想要的图像。我试过一些命令,如.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> 

回答

0

请问你的脚本决定要点击哪一个形象?

而且它点击的图像?假设是真的,那么这样的事情应该工作:

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 - 如果它不公开。



更新,现在the target page已给出。

该页面有点棘手;只需点击一下就可以了。此外,检查页面,它不是被点击的图像,它是包含<li>重要的点击处理程序。

所以,用点击和一些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的。如果是这样,你的脚本可能违反服务条款(不会阅读太多葡萄牙语,我没有去寻找他们),他们可能会采取反措施来破坏这个脚本。

+0

我试过的最后一段代码: 'code' var name = document.getElementsByClassName('foto-chamada'); name [i] .click(); '代码' 我会改变[我]取决于我想投票 我试了你的代码,并没有奏效。这里是链接http://tvg.globo.com/bbb/bbb12/votacao/291.html – Comentarist 2012-03-13 17:22:37

+0

今天的调查将会停止,如果在你看到之前关闭,这里是快照。一旦页面加载http://i43.tinypic.com/29kq82o.png一旦选择该选项http://i40.tinypic.com/5n4okm.png代码从页面pastebin.com/raw.php?i= 98V7eMA6 thx – Comentarist 2012-03-13 23:08:26

+0

在检查了该页面及其代码后,我已经更新了答案。页面设计建议可能已经建立了基本的反脚本措施。只要确保,不要使用此代码来违反任何可能适用的法律或服务条款。 – 2012-03-14 01:31:22