2012-04-30 21 views
1

對於此測試Greasemonkey UserScript,警報彈出,但沒有記錄到Firebug控制檯。
這是在Firefox 12.0中的Greasemonkey 0.9.18和Firebug 1.9.1。爲什麼alert()在Greasemonkey示例中爲此jQuery啓動而不是console.log()?

// ==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'); 
+0

你使用的是什麼版本的jQuery,Firefox和Firebug? – Jack

+2

你確定你沒有在控制檯上應用過濾器嗎?只顯示錯誤或警告? –

+0

如果直接輸入'console.log('test')'到firebug中,控制檯是否仍然正常登錄? –

回答

2

正如在GreaseMonkey manual中所解釋的,在GreaseMonkey腳本中,腳本的全局上下文不是瀏覽器的真實窗口對象(與在頁面上執行的實際腳本不同),而是虛擬版本的窗口對象相同的API。

console對象是真實窗口上的全局變量,因此無法從GreaseMonkey(或至少從GreaseMonkey上的jQuery)訪問。

請參閱此頁面,瞭解GreaseMonkey下的console和GreaseMonkey如何記錄消息。

+0

順便說一句 - 它可能是您的日誌最終在內部的Firefox控制檯上 - 點擊CTRL + SHIFT + K打開內置控制檯。 – Guss

+0

謝謝國王。他們確實出現在Firefox控制檯中。我也嘗試過Scriptish,因爲它們的目標是Firebug。 https://github.com/scriptish/scriptish/wiki/console但它似乎不適用於我正在運行的版本。我創建了一個錯誤報告。 https://scriptish.lighthouseapp.com/projects/83146-firefox-extension/tickets/614-scriptish-not-logging-to-firebug-console – GollyJer

相關問題