Hotlink资源直接来自GitHub的JavaScript文件 [英] Hotlink resources like JavaScript files directly from GitHub
问题描述
我询问了有关从GitHub中包含资源的问题,答案是使用原始链接:
https://raw.github.com/username/repository/branch/file.js
我正在尝试使用以下脚本:
< script
type =text / javascript
src =https://raw.github.com/username/repo/master/src/file.js
> < /脚本>
但我得到以下错误:
拒绝执行' 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 $ b $b└─index.html
在index.html页面中,我希望拥有最新版本的my-jQuery-plugin.js。所以,我会将原始网址包含到脚本中。
如何修复错误?
解决方案是的,Github 在2013年4月更改了:
我们在2011年的原始URL响应中添加了
X-Content-Type-Options:nosniff
这是打击盗链的第一步。这具有强制浏览器根据Content-Type
标头处理内容的效果。这意味着当我们为文件的原始视图设置Content-Type:text / plain
时,浏览器将拒绝将该文件视为JavaScript或CSS。
不过感谢 http://rawgit.com/ ,我们可以包括GH脚本。唯一的改变是从
raw.github.com
中删除rawgithub.com : < script
type =text / javascript
src =https://rawgit.com/username /repo/master/src/file.js
>< / script>
该项目为托管在Github上,开源。
是的,@Lix是正确的。这些文件不是从Github提供的,而是从Rawgit网站提供的。
我发现的另一个解决方法是,而不是:
< script
type =text / javascript
src =https:// raw。 github.com/username/repo/master/src/file.js
>< / script>
您可以使用
$。getScript
jQuery函数:
< script>
$ .getScript(https://raw.github.com/username/repo/master/src/file.js,function(){
/ *加载时执行某些操作* /
});
< / script>
I asked a question about including resources from GitHub and the answer was to use the raw link:
https://raw.github.com/username/repository/branch/file.js
I am trying to include a script using:
<script type="text/javascript" src="https://raw.github.com/username/repo/master/src/file.js" ></script>
but I get the following error:
Refused to execute script from 'https://raw.github.com/username/repo/master/src/file.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
Are there any alternatives to fix this error?
Usage example
I don't use this in the production but for a demo page:
project-repository-from-github ├─ master │ ├─ src │ │ └─ my-jQuery-plugin.js │ └─ README.md └─ gh-pages ├─ css │ └─ style.css └─ index.html
In the index.html page I want to have the latest build of my-jQuery-plugin.js. So, I would include the raw URL to the script.
How do I fix the error?
解决方案Yes, Github changed this in April, 2013:
We added the
X-Content-Type-Options: nosniff
header to our raw URL responses way back in 2011 as a first step in combating hotlinking. This has the effect of forcing the browser to treat content in accordance with theContent-Type
header. That means that when we setContent-Type: text/plain
for raw views of files, the browser will refuse to treat that file as JavaScript or CSS.But thanks to http://rawgit.com/ we can include GH scripts. The only change is to remove the dot from
raw.github.com
that becomesrawgithub.com
:<script type="text/javascript" src="https://rawgit.com/username/repo/master/src/file.js" ></script>
The project is hosted on Github being open-source.
And yes, @Lix is correct. The files are not being served from Github but from the rawgit site.
Another workaround I found is that instead of:
<script type="text/javascript" src="https://raw.github.com/username/repo/master/src/file.js" ></script>
you can use
$.getScript
jQuery function:<script> $.getScript("https://raw.github.com/username/repo/master/src/file.js", function () { /* do something when loaded */ }); </script>
这篇关于Hotlink资源直接来自GitHub的JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!