Skip to content

Instantly share code, notes, and snippets.

@latel
Created April 2, 2018 02:31
Show Gist options
  • Save latel/66dfff9705bc199d63c73a3e3123c993 to your computer and use it in GitHub Desktop.
Save latel/66dfff9705bc199d63c73a3e3123c993 to your computer and use it in GitHub Desktop.
disable wechat-dev-tools from twittering
var _console = {
_disabledGroupFlags: [],
log: window.console.log.bind(window.console),
info: window.console.info.bind(window.console),
debug: window.console.debug.bind(window.console),
group: window.console.group.bind(window.console),
groupEnd: window.console.groupEnd.bind(window.console)
};
window.console.info = function() {
// 开发者工具中不显示vConsole的debug print
if (_console._disabledGroupFlags.length || (/wechatdevtools/.test(navigator.userAgent) && /\[system]/.test(arguments[0]))) {
return false;
}
_console.info.apply(window.console, arguments);
};
window.console.debug = function() {
// 当console.group被屏蔽时, 也屏蔽debug
if (!_console._disabledGroupFlags[_console._disabledGroupFlags.length - 1]) {
_console.debug.apply(window.console, arguments);
}
};
window.console.group = function(descriptor) {
// 如果匹配如下正则,则忽略此次的console.group
if (!/wx\..+(begin|end)$/.test(descriptor)) {
_console._disabledGroupFlags.push(false);
return _console.group(descriptor);
}
_console._disabledGroupFlags.push(true);
};
window.console.groupEnd = function() {
// 如果此次console.group没有被屏蔽,才执行对应的console.groupEnd
if (!_console._disabledGroupFlags.pop()) {
_console.groupEnd();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment