我希望在标签获得焦点后短暂延迟后单击一个按钮

时间:2012-02-11 12:11:56

标签: javascript greasemonkey

我希望在选择[聚焦]标签时执行setTimeout功能。我正在使用Mozilla(Greasemonkey)。

这是我尝试过的:

// ==UserScript==
// @name           [udit]click stumble button on pages
// @namespace      uditeewd
// @include        http://www.stumbleupon.com/interest/*
// @include        http://www.stumbleupon.com/channel/*
// @include        http://www.stumbleupon.com/stumbler/*
// @exclude        http://www.stumbleupon.com/stumbler/*/likes/interest*
// @exclude        http://www.stumbleupon.com/interest/*/followers*
// @exclude        http://www.stumbleupon.com/channel/*/followers*
// @exclude        file:///*
// ==/UserScript==

setTimeout(function(ButtonClickAction) {
var stumbButt   = document.querySelector ("div.stumbler-card a.stumble-button");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
stumbButt.dispatchEvent (clickEvent);
}, 0);

document.addEventListener ("onfocus", ButtonClickAction, true);

3 个答案:

答案 0 :(得分:1)

将其分解为步骤:

  1. 您希望选项卡焦点启动计时器。
  2. 您希望计时器在时机成熟时单击按钮。
  3. 请注意,您需要定义哪个节点构成“标签”。您当前的代码会针对整个页面触发焦点。

    因此,代码将类似于:

    var theTab  = document.querySelector (YOU NEED TO FIGURE THIS OUT, IT'S HIGHLY PAGE SPECIFIC);
    
    theTab.addEventListener ("focus", FireClickDelay, true);
    
    function FireClickDelay () {    
        setTimeout (ClickTheButton, 100);
    }
    
    function ClickTheButton () {    
        var stumbButt   = document.querySelector ("div.stumbler-card a.stumble-button");
        var clickEvent  = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        stumbButt.dispatchEvent (clickEvent);
    }
    

答案 1 :(得分:0)

// ==UserScript==
// @name           [udit]click stumble button on pages
// @namespace      uditeewd
// @include        http://www.stumbleupon.com/interest/*
// @include        http://www.stumbleupon.com/channel/*
// @include        http://www.stumbleupon.com/stumbler/*
// @exclude        http://www.stumbleupon.com/stumbler/*/likes/interest*
// @exclude        http://www.stumbleupon.com/interest/*/followers*
// @exclude        http://www.stumbleupon.com/channel/*/followers*
// @exclude        file:///*
// ==/UserScript==

window.onfocus = function() {
    setTimeout (ClickTheButton, 0);
};

function ClickTheButton () {    
    var stumbButt   = document.querySelector ("div.stumbler-card a.stumble-button");
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    stumbButt.dispatchEvent (clickEvent);
}

答案 2 :(得分:0)

window.onblurwindow.onfocus

(function(){
    var timer = null;
    var has_switched = false;

    window.onblur = function(){
      timer = settimeout(changeitup, 2000);
    }  

    window.onfocus = function(){
      if(timer) cleartimeout(timer);
    }

    function changeitup(){
        if( has_switched == false ){
            alert('hey! who switched tabs?')
            has_switched = true;    
        }
    }
})();