Embed JS Console within website

前端 未结 3 1236
孤城傲影
孤城傲影 2020-12-18 00:46

I want to embed a JS-Console within a website for extended debugging purposes. Are there any libraries or hooks available? How can I catch console.log messages?

3条回答
  •  别那么骄傲
    2020-12-18 01:27

    You could use the eval() function to evaluate javascript in a string and then print that output to some div. This would give you some capabilities of a REPL.

    const consoleElm = document.querySelector('#console');
    const clearButton = document.querySelector('#clear');
    
    clearButton.addEventListener('click', (event) => {
    	consoleElm.innerHTML = '';
    });
    
    const consoleForm = document.querySelector('#console-form');
    consoleForm.addEventListener('submit', (event) => {
    	event.preventDefault();
      const command = event.target.querySelector('#command').value;
      const value = eval(command);
      consoleElm.innerHTML += (value === undefined ? 'undefined' : value) + '\n';
    });

    
    

提交回复
热议问题