adi
adi

Reputation: 207

I want to click a button after a short delay after a tab gets focus

I want the setTimeout function to execute when I've selected[focused] the tab. I am using Mozilla (Greasemonkey).

Here's what I've tried:

// ==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);

Upvotes: 0

Views: 915

Answers (3)

yodog
yodog

Reputation: 6232

window.onblur and window.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;    
        }
    }
})();

Upvotes: 0

adi
adi

Reputation: 207

// ==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);
}

Upvotes: 0

Brock Adams
Brock Adams

Reputation: 93443

Break it down to steps:

  1. You want a tab focus to start a timer.
  2. You want the timer to click a button when the time comes.

Note that you need to define what node(s) constitute the "tab". Your current code fires on focus for the whole page.

So, the code would be something like:

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);
}

Upvotes: 2

Related Questions