How to include JavaScript file or library in Chrome console?

后端 未结 10 1859
广开言路
广开言路 2020-11-27 08:58

Is there a simpler (native perhaps?) way to include an external script file in the Google Chrome browser?

Currently I’m doing it like this:

document.         


        
10条回答
  •  清酒与你
    2020-11-27 09:34

    As a follow-up to the answer of @maciej-bukowski above ^^^, in modern browsers as of now (spring 2017) that support async/await you can load as follows. In this example we load the load html2canvas library:

    async function loadScript(url) {
      let response = await fetch(url);
      let script = await response.text();
      eval(script);
    }
    
    let scriptUrl = 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js'
    loadScript(scriptUrl);

    If you run the snippet and then open your browser's console you should see the function html2canvas() is now defined.

提交回复
热议问题