我想在选项卡获得焦点后短暂延迟后单击按钮

发布于 2025-01-04 18:38:21 字数 1019 浏览 2 评论 0原文

我希望在选择[聚焦]选项卡时执行 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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

婴鹅 2025-01-11 18:38:21

将其分解为几个步骤:

  1. 您需要选项卡焦点来启动计时器。
  2. 您希望计时器在时间到时单击按钮。

请注意,您需要定义哪些节点构成“选项卡”。您当前的代码会在整个页面上聚焦。

所以,代码会是这样的:

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

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);
}
最美的太阳 2025-01-11 18:38:21
// ==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);
}
// ==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);
}
梦屿孤独相伴 2025-01-11 18:38:21

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

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;    
        }
    }
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文