简体   繁体   English

为什么在Greasemonkey示例中为此jQuery触发alert()但不触发console.log()?

[英]Why does alert() fire but not console.log() for this jQuery in Greasemonkey example?

For this test Greasemonkey UserScript, the alerts pop up but nothing is logged to the Firebug console. 对于此测试Greasemonkey UserScript,警报会弹出,但没有任何内容记录到Firebug控制台。
This is with Greasemonkey 0.9.18 and Firebug 1.9.1 in Firefox 12.0. 这与Firefox 12.0中的Greasemonkey 0.9.18和Firebug 1.9.1有关。

// ==UserScript==
// @name           test
// @namespace      tester12354
// @include        *
// @require        http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==

(function($) {
    $.fn.tester1 = function(test) {
        alert(test);
        console.log(test);
    }
}(jQuery));


$.extend({
    tester2: function(test) {
        alert(test);
        console.log(test);
    }
});

alert($().jquery)
console.log($().jquery) 

$().tester1('from tester1');
$.tester2('from tester2');

As it is explained in the GreaseMonkey manual , in GreaseMonkey scripts, the global context for the script is not the Browser's real window object (unlike in actual scripts that execute on the page) but a dummy version of the window object with the same APIs. 正如GreaseMonkey手册中所述,在GreaseMonkey脚本中,脚本的全局上下文不是浏览器的真实窗口对象(与在页面上执行的实际脚本不同),而是具有相同API的窗口对象的虚拟版本。

The console object is a global variable on the real window and is thus not accessible from GreaseMonkey (or at least from jQuery on GreaseMonkey). console对象是真实窗口上的全局变量,因此无法从GreaseMonkey(或至少从GreaseMonkey上的jQuery)访问。

See this page for explanation on what's up with console under GreaseMonkey and how you should log messages from GreaseMonkey. 有关GreaseMonkey下console内容的说明以及如何从GreaseMonkey记录消息,请参阅此页面。

如果页面本身没有任何<script>标签,那可能是因为Firebug注入控制台对象愚蠢方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM