检测用户是否启用了Firebug的确定方法是什么?
答案 0 :(得分:87)
检查console
对象(仅使用Firebug创建),如下所示:
if (window.console && window.console.firebug) {
//Firebug is enabled
}
Firebug开发人员decided to remove window.console.firebug
。您仍然可以通过duck typing
if (window.console && (window.console.firebug || window.console.exception)) {
//Firebug is enabled
}
if (document.getUserData('firebug-Token')) ...
if (console.log.toString().indexOf('apply') != -1) ...
if (typeof console.assert(1) == 'string') ...
但一般情况下,不需要实际这样做。
答案 1 :(得分:20)
如果启用了firebug,则不会定义window.console。 console.firebug将返回版本号。
答案 2 :(得分:9)
自Firebug 1.9.0版起,由于隐私问题,console.firebug
已不再定义;见release notes,bug report。 这打破了上述方法。确实,它将艾伦问题的答案改为“没有办法”;如果 是另一种方式,则认为它是一个错误。
解决方案是检查console.log
的可用性或您要使用或替换的任何内容。
以下建议替换David Brockman上面提到的代码类型,但不删除任何现有功能。
(function () {
var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml',
'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
if (window.console) {
for (var i = 0; i < names.length; i++) {
if (!window.console[names[i]]) {
window.console[names[i]] = function() {};
}
}
} else {
window.console = {};
for (var i = 0; i < names.length; i++) {
window.console[names[i]] = function() {};
}
}
})();
答案 3 :(得分:4)
可能无法检测到。
Firebug有多个标签,可以单独禁用,现在默认情况下不启用。
GMail因为它只能判断我是否启用了“控制台”选项卡。进一步探测可能需要安全规避,而你不想去那里。
答案 4 :(得分:3)
您可以使用类似的方法来防止代码中的firebug调用在未安装时导致错误。
if (!window.console || !console.firebug) {
(function (m, i) {
window.console = {};
while (i--) {
window.console[m[i]] = function () {};
}
})('log debug info warn error assert dir dirxml trace group groupEnd time timeEnd profile profileEnd count'.split(' '), 16);
}
答案 5 :(得分:2)
请注意,Chrome window.console也会返回true或[Object console]
。
此外,我会检查是否安装了Firebug
if (window.console.firebug !== undefined) // firebug is installed
以下是我在Safari和Chrome中获得的内容,没有安装firebug。
if (window.console.firebug) // true
if (window.console.firebug == null) // true
if (window.console.firebug === null) // false
Is-True和Is-Not Operators显然会输入强制类型,这在JavaScript中应该避免。
答案 6 :(得分:0)
目前,window.console.firebug已被最新的firebug版本删除。因为firebug是一个基于扩展的JavaScript调试器,它在window.console中定义了一些新的函数或对象。所以大多数情况下,你只能使用这个新定义的函数来检测firebug的运行状态。
,例如
if(console.assert(1) === '_firebugIgnore') alert("firebug is running!");
if((console.log+'''').indexOf('return Function.apply.call(x.log, x, arguments);') !== -1) alert("firebug is running!");
您可以在每个萤火虫中测试这些方法。
祝福!