在实时网页上执行 JavaScript 文件的最快、最简单的方法是什么?

What’s the quickest, easiest way to execute a JavaScript file on a live web page?

我有这个 JavaScript 文件 URL:

https://rawgit.com/Khan/tota11y/master/build/tota11y.min.js

(托管在 GitHub 上并通过 RawGit 传递),我想通过浏览器的控制台将其注入实时网页。

为了实现这一点,我可以动态创建一个 <script> 元素并将其附加到 DOM:

(function () {
  var s = document.createElement('script');
  s.src = 'https://rawgit.com/Khan/tota11y/master/build/tota11y.min.js';
  document.body.appendChild(s);
}())

但这既不快也不容易。我想使用一个 API 通过简单的调用使这成为可能,例如:

exec('https://rawgit.com/Khan/tota11y/master/build/tota11y.min.js')

Chrome 或 Firefox 是否在其控制台中提供这样的 API(或类似的东西)?


用例: 在实时网页上快速测试 JavaScript 托管在 GitHub 上的库。

更新:The Developer Toolbar has been removed from Firefox Nightly as of 18th May 2018.

在 Firefox 中,您可以使用开发者工具栏注入脚本。用Shift+F2调出并输入inject https://rawgit.com/Khan/tota11y/master/build/tota11y.min.js

More info on MDN

据我所知,你需要一个模块加载能力。您有两条路线:

  • 要么使用像 Require.js that already does that kind of dynamic loading for you. It enforces you to use AMD style of scripts though. This website 这样的模块加载器,在 javascript 中更多地讨论模块。 require 对象将在控制台中可用,因为它是全局的。

  • 如果您已经使用 jQuery,请使用类似 jQuery does here 的 hackish 函数(它是一个相当大的库,为此导入)。 jQuery 是全局函数,您可以在控制台中调用该函数。

编辑:我没有link浏览器特定功能的原因是因为它太具体了。使用仅 JavaScript 的解决方案适用于所有浏览器以测试兼容性。

对于 chrome,有一个名为 Script Injector 的插件可以做到这一点

https://chrome.google.com/webstore/detail/script-injector/fddnddnolonllcgfbenaloajnbhebmob?hl=en