2014-03-25 8 views
3

greasemonkey를 사용하여 지금 구매 버튼을 클릭하고 싶습니다. 버튼의클릭 스크립트 버튼

// ==UserScript== 
// @name  script 
// @namespace sc 
// @include  * 
// @version  1 
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js 
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js 
// @grant GM_addStyle 
// ==/UserScript== 

waitForKeyElements ("#buy-now", triggerMostButtons); 

function triggerMostButtons (jNode) { 
    triggerMouseEvent (jNode[0], "mouseover"); 
    triggerMouseEvent (jNode[0], "mousedown"); 
    triggerMouseEvent (jNode[0], "click"); 
    triggerMouseEvent (jNode[0], "mouseup"); 
} 

function triggerMouseEvent (node, eventType) { 
    var clickEvent = document.createEvent('MouseEvents'); 
    clickEvent.initEvent (eventType, true, true); 
    node.dispatchEvent (clickEvent); 
} 

HTML : 무엇을에 넣어 내가

var TargetLink   = $("a:contains('Ik neem er een!')") 
if (TargetLink.length) 
    window.location.assign (TargetLink[0].href); 

같은 다른 코드를 시도

<div class="buy-now"><a data-spm-anchor-id="0.0.0.0" data-widget-cid="widget-14" tabindex="-1" id="buy-now" class="buy-now-btn" href="#">Buy Now</a><a id="add-to-cart" class="add-to-cart-btn" href="#">Add to Cart</a></div> 

waitForKeyElements ("a.class:contains('buy-now-btn')", clickOnFollowButton); 

function clickOnFollowButton (jNode) { 
    var clickEvent = document.createEvent ('MouseEvents'); 
    clickEvent.initEvent ('click', true, true); 
    jNode[0].dispatchEvent (clickEvent); 
} 

는 누군가가 나에게 explenation을 줄 수 최초의 파라미터로서의 waitForKeyElements 버튼을 클릭 했습니까?

답변

2

아래 코드가 원하는지 궁금합니다.

document.querySelector('#buy-now').click() 

Demo

일부 스크립트 DOMContentLoaded시 the page you mentioned에로드되지 않은 것 같다. 나를위한 문제는, 지연 해결 추가

// ==UserScript== 
// @name   script 
// @namespace sc 
// @include  http://www.aliexpress.com/item/Free-shipping-100-Genuine-Original-P1000-Mobile-phone-data-cable-for-Samsung-P1000-P6800-P7500-USB/1477114758.html 
// @version  1 
// @description http://stackoverflow.com/questions/22633509/userscript-to-click-button/22684724 
// @grant  none 
// ==/UserScript== 
setTimeout(function() { 
    document.querySelector('#buy-now').click() 
}, 1000) 
+0

http://www.aliexpress.com/item/Free-shipping-100-Genuine-Original-P1000-Mobile-phone-data-cable- for-Samsung-P1000-P6800-P7500-USB/1477114758.html # 작동하지 않는 것 같습니다. 또 다른 제안이 있니? – Remco

+1

@Remco Swienink 답변이 수정되었습니다. 지연을 추가하면 문제가 해결됩니다. – zanetu