【问题标题】:Why does alert() fire but not console.log() for this jQuery in Greasemonkey example?为什么在 Greasemonkey 示例中这个 jQuery 会触发 alert() 而不是 console.log()?
【发布时间】:2012-04-30 01:18:31
【问题描述】:

对于此测试 Greasemonkey UserScript,警报会弹出,但 Firebug 控制台中没有记录任何内容。
这是 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');

【问题讨论】:

  • 刚刚在 Google Chrome 上测试过,它似乎可以在 jQuery 版本 1.7.2 上运行:jsfiddle.net/2k4wh 也可以在 Firefox 10.0 上运行
  • 附加信息:适用于 jQuery v1.2.6 到 v1.7.2 。萤火虫版本?火狐版本?
  • 您使用的是什么版本的 jQuery、Firefox 和 Firebug?
  • 您确定您没有对控制台应用过滤器吗?只显示错误或警告?
  • 直接在firebug中输入console.log('test'),控制台是否还能正常登录?

标签: javascript jquery firefox console firebug


【解决方案1】:

正如GreaseMonkey manual 中解释的那样,在 GreaseMonkey 脚本中,脚本的全局上下文不是浏览器的真实窗口对象(与在页面上执行的实际脚本不同),而是窗口对象的虚拟版本相同的 API。

console 对象是真实窗口上的全局变量,因此无法从 GreaseMonkey(或至少从 GreaseMonkey 上的 jQuery)访问。

请参阅此页面以了解有关 GreaseMonkey 下 console 的最新情况以及如何记录来自 GreaseMonkey 的消息的说明。

【讨论】:

【解决方案2】:

如果页面本身没有任何<script> 标签,可能是因为stupid way in which Firebug injects the console object

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多