链接并执行托管在 GitHub 上的外部 JavaScript 文件 [英] Link and execute external JavaScript file hosted on GitHub

查看:40
本文介绍了链接并执行托管在 GitHub 上的外部 JavaScript 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将本地 JavaScript 文件的链接引用更改为 GitHub 原始版本时,我的测试文件停止工作.错误是:

When I try to change the linked reference of a local JavaScript file to a GitHub raw version my test file stops working. The error is:

拒绝从 ... 执行脚本,因为其 MIME 类型 (text/plain) 不可执行,并且启用了严格的 MIME 类型检查.

Refused to execute script from ... because its MIME type (text/plain) is not executable, and strict MIME type checking is enabled.

有没有办法禁用这种行为,或者有没有允许链接到 GitHub 原始文件的服务?

Is there a way to disable this behavior or is there a service that allows linking to GitHub raw files?

工作代码:

<script src="bootstrap-wysiwyg.js"></script>

非工作代码:

<script src="https://raw.github.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js"></script>

推荐答案

有一个很好的解决方法,现在使用 jsdelivr.net.

There is a good workaround for this, now, by using jsdelivr.net.

步骤:

  1. 在 GitHub 上找到您的链接,然后点击原始"版本.
  2. 复制网址.
  3. raw.githubusercontent.com改为cdn.jsdelivr.net
  4. 在您的用户名前插入 /gh/.
  5. 删除branch 名称.
  6. (可选)插入您要链接的版本,作为@version(如果不这样做,您将获得最新em> - 这可能会导致长期缓存)
  1. Find your link on GitHub, and click to the "Raw" version.
  2. Copy the URL.
  3. Change raw.githubusercontent.com to cdn.jsdelivr.net
  4. Insert /gh/ before your username.
  5. Remove the branch name.
  6. (Optional) Insert the version you want to link to, as @version (if you do not do this, you will get the latest - which may cause long-term caching)

<小时>

示例:

http://raw.githubusercontent.com/<username>/<repo>/<branch>/path/to/file.js

使用此 URL 获取最新版本:

Use this URL to get the latest version:

http://cdn.jsdelivr.net/gh/<username>/<repo>/path/to/file.js

使用此 URL 获取特定版本或提交哈希:

Use this URL to get a specific version or commit hash:

http://cdn.jsdelivr.net/gh/<username>/<repo>@<version or hash>/path/to/file.js

对于生产环境,请考虑针对特定标签或提交哈希而不是分支.使用最新链接可能会导致文件长期缓存,导致您在推送新版本时无法更新您的链接.通过 commit-hash 或标签链接到文件使链接对于版本是唯一的.

For production environments, consider targeting a specific tag or commit-hash rather than the branch. Using the latest link may result in long-term caching of the file, causing your link to not be updated as you push new versions. Linking to a file by commit-hash or tag makes the link unique to version.

为什么需要这个?

2013 年,GitHub 开始使用 X-Content-Type-Options: nosniff,它指示更现代的浏览器强制执行严格的 MIME 类型检查.然后它以服务器返回的 MIME 类型返回原始文件,防止浏览器按预期使用该文件(如果浏览器遵守该设置).

In 2013, GitHub started using X-Content-Type-Options: nosniff, which instructs more modern browsers to enforce strict MIME type checking. It then returns the raw files in a MIME type returned by the server, preventing the browser from using the file as-intended (if the browser honors the setting).

有关此主题的背景信息,请参阅此讨论主题.

For background on this topic, please refer to this discussion thread.

这篇关于链接并执行托管在 GitHub 上的外部 JavaScript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆