Greasemonkey脚本替换jQuery插件函数?

我正在尝试为Firefox编写一个Greasemonkey脚本,以便在检测到一个没有焦点的窗口时更改某些页面的行为。 我希望页面继续运行,就像它仍然处于焦点一样,即使我有另一个选项卡或窗口处于活动状态。

我开始查看CAPS安全策略 ,它似乎适用于内联Javascript( window.onfocuswindow.onblur ),但对外部jQuery脚本插件访问焦点事件没有影响。

这是我的测试页面,它使用jQuery插件来检测焦点。 插件在这里是内联的,但在某些情况下也可能是外部脚本。

      jQuery focus test   (function($) { $.fn.focuscheck = function() { function focusOn () { document.getElementById('console').innerHTML += '
Focus event handler fired.
'; }; function focusOff () { document.getElementById('console').innerHTML += '
Blur event handler fired.
'; }; $(window).bind('focus', focusOn); $(window).bind('blur', focusOff); }; })(jQuery);
$('#func').focuscheck();

还有我的Greasemonkey脚本。 我正在尝试从joanpiedra.com调整jQuery Greasemonkey代码。 在这种情况下,我试图从我的测试页面中完全替换focuscheck jQuery插件function代码。 所有行都与原始行相同,但function focusOff()除外,它应显示不同的结果。 我不知道是否有办法在不更换整个function的情况下执行此操作。 如果这是一个选项,我可能只想用$(window).bind('blur', focusOff);替换单个focusOff函数或行$(window).bind('blur', focusOff);

现在,Greasemonkey脚本在我的测试页面上不起作用。 输出没有变化。 另外,我不确定是否需要将jQuery添加到我的Greasemonkey脚本中,因为页面已经加载了它。

 // ==UserScript== // @name jQuery // @namespace http://www.joanpiedra.com/jquery/greasemonkey // @description Play nicely with jQuery and Greasemonkey // @author Joan Piedra // @homepage http://www.joanpiedra.com/jquery/greasemonkey // @include * // ==/UserScript== // Add jQuery var GM_JQ = document.createElement('script'); GM_JQ.src = 'http://jquery.com/src/jquery-latest.js'; GM_JQ.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(GM_JQ); // Check if jQuery's loaded function GM_wait() { if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); } else { $ = unsafeWindow.jQuery; letsJQuery(); } } GM_wait(); // All your GM code must be inside this function function letsJQuery() { var script = document.createElement('script'); script.type = "text/javascript"; script.innerHTML = "(function($){\ \ $.fn.focuscheck = function(){\ function focusOn () {\ document.getElementById('console').innerHTML += '
Focus event handler fired.
';\ };\ function focusOff () {\ document.getElementById('console').innerHTML += '
CHANGED event handler.
';\ };\ \ $(window).bind('focus', focusOn);\ $(window).bind('blur', focusOff);\ };\ })(jQuery);"; document.getElementsByTagName('head')[0].appendChild(script); }

我不知道是否有办法在不更换整个function的情况下执行此操作

就在这里。 见下面的代码。

另外,我不确定是否需要将jQuery添加到我的Greasemonkey脚本中,因为页面已经加载了它。

你不必。 见下面的代码。

html (与你的相同 – 我刚刚将“内联JS”中的内容添加到innerHTML

  . . function focusOn () { document.getElementById('console').innerHTML += '
Focus event handler fired from inline JS.
'; }; function focusOff () { document.getElementById('console').innerHTML += '
Blur event handler fired from inline JS.
'; }; . .

GM脚本

 // ==UserScript== // @name TESTE 2 // @namespace TESTE 2 // @description TESTE 2 // @include file:///* // ==/UserScript== $ = unsafeWindow.$ $(window).off('blur') function focusOff () { document.getElementById('console').innerHTML += '
Blur event handler fired from Greasemonkey.
'; }; $(window).on('blur', focusOff);

结果

结果图像

在这里,为什么你不应该使用unsafeWindow : http : unsafeWindow

编辑

关于您在评论中的问题

谢谢。 这正是我想要的。 我将研究使用安全包装而不是unsafeWindow。 如果你不介意后续问题: 幻灯片演示 – 我尝试调整类似的代码来覆盖点击function,使用$('.btn-slide').unbind('click')然后添加一个新函数,但它不是很有效。 记录事件,但按钮仍然激活。 $('.btn-slide').bind('click', function(event) { event.preventDefault(); console.log('button clicked'); }); 如何阻止幻灯片动画?

新代码:

 // ==UserScript== // @name TESTE 3 // @namespace TESTE 3 // @description TESTE 3 // @include http://www.webdesignerwall.com/demo/jquery/* // ==/UserScript== $ = unsafeWindow.$ $(window).load(function(){ $('.btn-slide') .unbind('click') .bind('click', function(event) { alert('my function here') console.log('button clicked') }) });