Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - 链接并执行GitHub上托管的外部JavaScript文件(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>   ask by AuthorProxy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Steps :(步骤 :) Find your link on GitHub, and click to the "Raw" version.(在GitHub上找到您的链接,然后单击“原始”版本。) Copy the URL.(复制URL。) Change raw.githubusercontent.com to cdn.jsdelivr.net(更改raw.githubusercontent.comcdn.jsdelivr.net) Insert /gh/ before your username.(在用户名之前插入/gh/ 。) Remove the branch name.(删除branch名称。) (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 .(有关此主题的背景信息,请参阅此讨论主题 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...