简体   繁体   English

在浏览器控制台中导入或需要任何库

[英]import or require any library in browser console

While debugging application most of the time I feel like it would hve easier if I could include any library into browser console and try some of the function from that libraty. 大多数时候,在调试应用程序时,我觉得如果可以将任何库包含在浏览器控制台中并尝试该libraty中的某些功能,它将更加容易。

Now in modern javascript/es6/es6/typescript world is there any thing short and quick available to to import a script instantly to the brower so that it can be used to directly 现在,在现代javascript/es6/es6/typescript世界中,有什么短而快的工具可以立即将脚本导入浏览器,以便可以直接使用

Example

While debugging, if I want Observable I should be able to do something like this 调试时,如果我希望可以观察到,则应该可以执行以下操作

import {Observable} from 'rxjs/Observable';  //typescript way
const Observable= require('rxjs/Observable'); // require way

But these are not working. 但是这些都不起作用。

already explored dynamic <script> tag 已经探索过动态<script>标签

I already explore old way to dynamically using the <script> tag like below but its dificult for large number of libs and also not elegant 我已经探究了动态使用<script>标记的旧方法,如下所示,但是它对于大量的libs来说是困难的,而且也不美观

var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type (or see below for non wait option)
jQuery.noConflict();

Some browsers ( including Chrome ) can use snippets in console, either as built-in features or extensions. 某些浏览器( 包括Chrome )可以在控制台中使用摘要,作为内置功能或扩展程序。

One-liner script to do this is 单行脚本执行此操作是

document.head.appendChild(Object.assign(
    document.createElement('script'),
    { src: '...' }
));

Considering that most websites already have jQuery loaded (even if they don't, this can be handled by browser extensions, such as Chrome jQuery Injector ) it can be shortened to: 考虑到大多数网站已经加载了jQuery(即使没有加载,也可以通过浏览器扩展程序(例如Chrome jQuery Injector )来处理),可以将其缩短为:

$.getScript('...');

Any piece of code that should be always available in console can also be exposed as global function with user scripts (Tampermonkey in Chrome, etc), with some limitations imposed by user script security. 控制台中应始终可用的任何代码段,也可以通过用户脚本(Chrome中的Tampermonkey等)作为全局功能公开,但受用户脚本安全性的限制。

This will probably will be possible with dynamic import() , which is currently stage 3 proposal and isn't implemented in browsers. 动态import() 可能会实现这一点, 动态import()当前是第3阶段建议,并且未在浏览器中实现。


A suitable solution that works for most major libraries the one may be curious to quickly experiment with is to navigate to official library website and open the console. 对于大多数主要图书馆来说,一个可能很想快速尝试的合适解决方案是导航到官方图书馆网站并打开控制台。 Due to the fact that the websites often provide live examples, relevant variables are exposed to global scope. 由于网站经常提供实时示例,因此相关变量会暴露在全球范围内。 Known examples are $ for jQuery , angular for AngularJS , ng for Angular , Rx for RxJS , moment for Moment.js ... 已知实例为$jQuery的angularAngularJSng角度RxRxJSmomentMoment.js ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM