本文翻译自:Link and execute external JavaScript file hosted on GitHub

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: 工作代码:

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

Non-working code: 非工作代码:

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

#1楼

参考:https://stackoom.com/question/1AlDW/链接并执行GitHub上托管的外部JavaScript文件


#2楼

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

Heads up: nosniff header support coming to Chrome and Firefox 抬头:来自Chrome和Firefox的nosniff标头支持


#3楼

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

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/<username>/<repo>/<branch>/path/to/file.js

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

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

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

http://cdn.jsdelivr.net/gh/<username>/<repo>@<version or hash>/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 this discussion thread . 有关此主题的背景信息,请参阅此讨论主题 。


#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: 这是您的文件链接:

https://raw.githubusercontent.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js https://raw.githubusercontent.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js

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: 这是最后一个链接:

https://rawgit.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js https://rawgit.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js

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: http://101helper.blogspot.com/2015/11/store-blogger-codes-on-github-boost-blogger-speed.html Referance URL: http ://101helper.blogspot.com/2015/11/store-blogger-codes-on-github-boost-blogger-speed.html

链接并执行GitHub上托管的外部JavaScript文件相关推荐

  1. 在github上托管Maven存储库

    本文翻译自:Hosting a Maven repository on github I have a fork of a small open sourced library that I'm wo ...

  2. github流程图_10月份Github上最热门的JavaScript开源项目

    开源最前线(ID:OpenSourceTop) 猿妹整编 转载请注明来源作者 10 月份 GitHub 上最热门的JavaScript开源项目排行已经出炉啦,在本月的名单中,有开源书籍.有开源课程等, ...

  3. react 开源项目_2020年2月Github上最热门的JavaScript开源项目

    来自:开源最前线(ID:OpenSourceTop)  2月份GitHub上最热门的JavaScript开源项目排行已经出炉啦,一起来看看上榜详情: 1joplin https://github.co ...

  4. 如何链接外部JavaScript文件

    在本教程中,我们向您展示如何将外部JavaScript文件 (文件扩展名以.js结尾)链接到网页. 注意 在HTML中,您可以将JavaScript嵌入网页或外部JavaScript文件中,也可以同时 ...

  5. 3月份GitHub上最热门的JavaScript开源项目(下)

    开源最前线(ID:OpenSourceTop) 猿妹整编 转载请注明来源作者 3月份GitHub上最热门的JavaScript开源项目新鲜出炉,web界面的JavaScript库Vue强势霸榜,Moz ...

  6. 在github上托管属于自己的网页

    文章目录 前言 一.配置github 1.建立新的仓库(repository) 二.配置git 1.git与github连接 2.测试git与github的连接 3.通过git上传,删除github仓 ...

  7. github和maven_在github上托管Maven存储库(包含源代码和javadoc)

    github和maven 如何通过maven使其他开发人员可以使用小型开源库? 一种方法是将其部署在Maven Central Repository上 . 我想要做的是将其部署到github ,因此我 ...

  8. 在github上托管Maven存储库(包含源代码和javadoc)

    如何通过maven将小型开源库提供给其他开发人员? 一种方法是将其部署在Maven Central Repository上 . 我想要做的是将其部署到github ,因此我可以自由地对其进行修改. 这 ...

  9. GitHub上的编程语言:JavaScript领衔Java次之

    最近,Linguist项目统计了代码托管网站GitHub上各类编程语言的热度.下图展示了自2008年以来各编程语言的排名状况及它们的相对热度. \\ \\ 从上图中可以看出,Perl已经不再流行,C# ...

最新文章

  1. 大数据工程师手册:全面系统的掌握必备知识与工具
  2. koa-router让人迷惑的文档和源码实现
  3. PHP使用Charles抓包手机
  4. P5135-painting【组合数学】
  5. 如何掌握Java内存(并保存程序)
  6. 最新 Unity3D鼠标滑轮控制物体放大缩小 [
  7. 前端学习(1110):this指向问题
  8. 【Matlab】设置绘图窗口大小及颜色
  9. Asp.net AJAX JavaScript 基本类型扩展(7)——String 类型及其扩展
  10. .net core不能使用httpget_.NET Core使用Nlog记录日志
  11. sqlplus工具linux,sqlplus 工具的使用
  12. CDA Level1 考试心得
  13. 怎样安装计算机病毒,台式机电脑中病毒如何重装系统图文教程
  14. 房产中介管理系统软件的开发和使用(附源码)
  15. element实现table表格序号递增的效果
  16. 做了3年单片机工程师,如果工资还是3-5千,那你就看过来
  17. selenium模拟刷百度流量源码
  18. 中顶访客登记管理系统
  19. 统计图表插件Chart.js(前端常用图表)
  20. 天合光能高纪凡的光伏崛起之路

热门文章

  1. 避免打断链式结构:使用.compose( )操作符
  2. 监听者模式理解(Swing)
  3. 算法-----最大子序和(Java 版本)
  4. 通俗易懂地解释遗传算法?有什么例子?
  5. js 判断是否为null
  6. linux关于程序的user,c – “get_user_pages”如何工作(对于linux驱动程序)
  7. Android之关于Intent的Flag属性的讲解
  8. Android10.0 startActivity启动过程
  9. 字符串模糊/精确查询——mysql
  10. 洛谷P1372 又是毕业季IP1414 又是毕业季II[最大公约数]