0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コード内のconsoleを全て無効化する方法【JavaScript】

Last updated at Posted at 2021-08-08

コード内のconsoleを全て無効化する方法【JavaScript】

解説は後で付けます

Chrome、Edge、Firefoxで対応します。

remove_console.js

let remove_console_flag=true;//フラグ
let save_console={};//console機能回復用


function remove_console(){

  const all_num=Object.keys(window.console).length;//window.consoleのObjectの総数
  
  if(Object.keys(save_console).length){
    window.console=Object.assign({},save_console);
    save_console={};
  }


  if(remove_console_flag){
    save_console=Object.assign({},window.console);//consoleのオブジェクトをコピー
    for(let i=0;i<all_num;i++){//window.consoleの総数分繰り返す
            let change=window.console[Object.keys(window.console)[i]];//log,trace,count...を取得
            window.console[change.name] = function(s){return;};//空の値を返す変数を上書き

    }
    console.log('表示されない');
    console.count('表示されない');
    console.trace('表示されない');
  }
}

function reset_console(){//consoleをもとに戻す
  remove_console_flag=false;
  remove_console();

}

remove_console();

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?