我希望在选择[聚焦]标签时执行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);
答案 0 :(得分:1)
将其分解为步骤:
请注意,您需要定义哪个节点构成“标签”。您当前的代码会针对整个页面触发焦点。
因此,代码将类似于:
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)
(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;
}
}
})();