直接来自GitHub的JavaScript文件等Hotlink资源

Ion*_*zău 20 javascript github

我问了一个关于从GitHub中包含资源问题,答案是使用原始链接:

https://raw.github.com/username/repository/branch/file.js
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下内容包含脚本:

<script
   type="text/javascript"
   src="https://raw.github.com/username/repo/master/src/file.js"
></script>
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

拒绝执行' https://raw.github.com/username/repo/master/src/file.js '中的脚本,因为它的MIME类型('text/plain')不可执行,并且启用了严格的MIME类型检查.

有没有其他方法可以解决此错误?

用法示例

我不是在制作中使用它而是用于演示页面:

project-repository-from-github
  ?? master
  ?   ?? src
  ?   ?   ?? my-jQuery-plugin.js
  ?   ?? README.md
  ?? gh-pages
      ?? css
      ?   ?? style.css
      ?? index.html
Run Code Online (Sandbox Code Playgroud)

在index.html页面中,我想拥有my-jQuery-plugin.js的最新版本.所以,我会将原始URL包含在脚本中.

我该如何修复错误?

Ion*_*zău 33

是的,Github 在2013年4月改变了这个:

我们X-Content-Type-Options: nosniff在2011年将标题添加到我们的原始URL响应中,这是打击热链接的第一步.这具有强制浏览器根据Content-Type标题处理内容的效果.这意味着当我们设置Content-Type: text/plain文件的原始视图时,浏览器将拒绝将该文件视为JavaScript或CSS.

但是由于http://combinatronics.com/,我们可以包含GH脚本.唯一的变化是从raw.github.com变为combinatronics.com:

<script
   type="text/javascript"
   src="https://combinatronics.com/username/repo/master/src/file.js"
></script>
Run Code Online (Sandbox Code Playgroud)

该项目由Github托管,是开源的.

是的,@ Lix是正确的.这些文件不是从Github提供的,而是来自combinatronics.


我找到的另一个解决方法是代替:

<script
   type="text/javascript"
   src="https://combinatronics.com/username/repo/master/src/file.js"
></script>
Run Code Online (Sandbox Code Playgroud)

你可以使用$.getScriptjQuery函数:

<script>
  $.getScript("https://combinatronics.com/username/repo/master/src/file.js", function () {
    /* do something when loaded */
  });
</script>
Run Code Online (Sandbox Code Playgroud)

  • 我确信所有者是一个好人,但也许有一天他忘记按时更新他的域名,一些骗子截取了这个名字,BAM 你现在在你的网站上有一个恶意软件脚本。被警告。 (4认同)
  • 需要警告人们,内容现在不是来自GitHub**.即使所有者声称他们不隶属于GitHub,域名和*real*GitHub网站的`raw`子域的相似性也是相当误导的,并不是很明显. (3认同)

Men*_*nua 7

我发现对我来说最好的解决方案是使用 GitHub 页面:https : //pages.github.com/

限制是你只能使用 repo 中的一个分支来保存你的脚本(即:gh-pages 分支)

示例用法
将 test.js 脚本推送到 REPO 的 gh-pages 分支。

该脚本位于 yourusername.github.io/REPO/test.js(更新可能需要大约 10 分钟)

如果您不更改脚本(CDN 永远缓存特定 url),其他解决方案
http://rawgithub.com对开发和生产(使用 CDN)都有好处。