How to include JavaScript file or library in Chrome console?

后端 未结 10 1857
广开言路
广开言路 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:44

    If anyone, fails to load because hes script violates the script-src "Content Security Policy" or "because unsafe-eval' is not an allowed", I will advice using my pretty-small module-injector as a dev-tools snippet, then you'll be able to load like this:

    imports('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js')
      .then(()=>alert(`today is ${moment().format('ffffdd')}`));

    this solution works because:

    1. It loades the library in xhr - which allows CORS from console, and avoids the script-src policy.
    2. It uses the synchronous option of xhr which allows you to stay at the console/snippet's context, so you'll have the permission to eval the script, and not to get-treated as an unsafe-eval.

提交回复
热议问题