博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链接并执行GitHub上托管的外部JavaScript文件
阅读量:2288 次
发布时间:2019-05-09

本文共 4305 字,大约阅读时间需要 14 分钟。

本文翻译自:

When I try to change the linked reference of a local JavaScript file to a GitHub raw version my test file stops working. 当我尝试将本地JavaScript文件的链接引用更改为GitHub原始版本时,我的测试文件停止工作。 The error is: 错误是:

Refused to execute script from ... because its MIME type ( text/plain ) is not executable, and strict MIME type checking is enabled. 拒绝从...执行脚本,因为它的MIME类型( text/plain )不可执行,并且启用了严格的MIME类型检查。

Is there a way to disable this behavior or is there a service that allows linking to GitHub raw files? 有没有办法禁用此行为或是否有允许链接到GitHub原始文件的服务?

Working code: 工作代码:

Non-working code: 非工作代码:


#1楼

参考:


#2楼

This is no longer possible. 这已经不可能了。 GitHub has explicitly disabled JavaScript hotlinking, and newer versions of browsers respect that setting. GitHub明确禁用了JavaScript热链接,而较新版本的浏览器则遵循该设置。


#3楼

There is a good workaround for this, now, by using . 一个很好的办法解决这个,现在,通过使用 。

Steps : 步骤

  1. Find your link on GitHub, and click to the "Raw" version. 在GitHub上找到您的链接,然后单击“原始”版本。
  2. Copy the URL. 复制URL。
  3. Change raw.githubusercontent.com to cdn.jsdelivr.net 更改raw.githubusercontent.comcdn.jsdelivr.net
  4. Insert /gh/ before your username. 在用户名之前插入/gh/
  5. Remove the branch name. 删除branch名称。
  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) (可选)插入要链接的版本 ,如@version (如果不这样做,您将获得最新版本 - 这可能会导致长期缓存)

Examples : 示例

http://raw.githubusercontent.com/
/
/
/path/to/file.js

Use this URL to get the latest version: 使用此URL获取最新版本:

http://cdn.jsdelivr.net/gh/
/
/path/to/file.js

Use this URL to get a specific version or commit hash: 使用此URL获取特定版本或提交哈希:

http://cdn.jsdelivr.net/gh/
/
@
/path/to/file.js

For production environments , consider targeting a specific tag or commit-hash rather than the branch. 对于生产环境 ,请考虑定位特定标记或commit-hash而不是分支。 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. 通过commit-hash或tag链接到文件使链接对版本唯一。


Why is this needed? 为什么需要这个?

In 2013, GitHub started using X-Content-Type-Options: nosniff , which instructs more modern browsers to enforce strict MIME type checking. 2013年,GitHub开始使用X-Content-Type-Options: nosniff ,它指示更多现代浏览器强制执行严格的MIME类型检查。 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). 然后它返回服务器返回的MIME类型的原始文件,防止浏览器按预期使用该文件(如果浏览器遵循该设置)。

For background on this topic, please refer to . 有关此主题的背景信息,请参阅 。


#4楼

rawgithub.com重定向到rawgit.com所以上面的例子现在是

http://rawgit.com/user/package/master/link.min.js


#5楼

To make things clear and short 使事情变得清晰和简短

//raw.githubusercontent.com --> //rawgit.com //raw.githubusercontent.com - > //rawgit.com

Note that this is handled by rawgit's development hosting and not their cdn for production hosting 请注意,这是由rawgit的开发托管处理的,而不是他们用于生产托管的cdn


#6楼

When a file is uploaded to github you can use it as external source or free hosting. 当文件上传到github时,您可以将其用作外部源或免费托管。 Troy Alford has explained it well above. 特洛伊·奥尔福德已经在上面解释了这一点。 But to make it easier let me tell you some easy steps then you can use a github raw file in your site: 但为了让它更容易让我告诉你一些简单的步骤,那么你可以在你的网站中使用github原始文件:

Here is your file's link: 这是您的文件链接:

Now to execute it you have to remove https:// and the dot( . ) between raw and githubusercontent 现在要执行它,你必须删除原始githubusercontent之间的https://和点(。)

Like this: 像这样:

rawgithubusercontent.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js rawgithubusercontent.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js

Now when you will visit this link you will get a link that can be used to call your javascript: 现在,当您访问此链接时,您将获得一个可用于调用您的javascript的链接:

Here is the final link: 这是最后一个链接:

Similarly if you host a css file you have to do it as mentioned above. 同样,如果你托管一个css文件,你必须如上所述。 It is the easiest way to get simple link to call your external css or javascript file hosted on github. 这是获取简单链接以调用github上托管的外部css或javascript文件的最简单方法。

I hope this is helpful. 我希望这是有帮助的。

Referance URL: Referance URL: ://101helper.blogspot.com/2015/11/store-blogger-codes-on-github-boost-blogger-speed.html

转载地址:http://fscnb.baihongyu.com/

你可能感兴趣的文章
手势(UIGestureXXX)使用详解
查看>>
UIMenuController和UIMenuItem,即iOS剪贴板
查看>>
新一代数据查询语言GraphQL来啦
查看>>
Simple Zend_Layout Example
查看>>
The Zend Framework MVC Architecture
查看>>
Framework框架分析总结
查看>>
Windows7下centOS 硬盘安装双系统
查看>>
GRUB引导程序参数
查看>>
phpMyAdmin简明安装教程
查看>>
独立安装LAMP时需要注意的几点
查看>>
socket
查看>>
判断远程url是否有效的几种方法
查看>>
javascript中编写类似in_array()的原型函数
查看>>
go 数据库操作
查看>>
php读取二进制流
查看>>
Golang热重启
查看>>
热重启golang服务器(graceful restart golang http server)
查看>>
echo框架使用:获取与配置
查看>>
PHP模拟多进程并发将内容写入文件
查看>>
nginx.conf配置说明
查看>>