git 包括来自 raw.github.com 的 js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7180099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:53:46  来源:igfitidea点击:

Including js from raw.github.com

javascriptgitinternet-explorergithub

提问by Mark Kahn

I have a github.com demo page that is linking to https://raw.github.com/.../master/.../file.jsso that I don't need to always copy the .jsfile over to the gh-pagesbranch every time it's changed. This works in every browser except IE which complains:

我有一个链接到https://raw.github.com/.../master/.../file.js的 github.com 演示页面,这样我就不需要总是将.js文件复制到gh-pages每次更改时分支。这适用于每个浏览器,除了 IE 抱怨:

SEC7112: Script from https://raw.github.com/cwolves/jQuery-iMask/master/dist/jquery-imask-min.jswas blocked due to mime type mismatch

SEC7112:来自https://raw.github.com/cwolves/jQuery-iMask/master/dist/jquery-imask-min.js 的脚本由于 MIME 类型不匹配而被阻止

This complaint is coming from the fact that the file is transferred with:

此投诉来自以下事实:该文件是通过以下方式传输的:

X-Content-Type-Options: nosniff
Content-Type: text/plain

which I can't change.

我无法改变。

Anyone have any ideas how to accomplish this same thing? Somehow allowing me to link to the file in the masterbranch without having to always push it to the gh-pagesbranch?

任何人都有任何想法如何完成同样的事情?以某种方式允许我链接到master分支中的文件而不必总是将其推送到gh-pages分支?

Actual page: http://cwolves.github.com/jQuery-iMask/

实际页面:http: //cwolves.github.com/jQuery-iMask/

(Minor update -- I changed the gh-pages in this exact instance to include the .js file, so IE is no longer broken, but would still like any feedback :))

(次要更新 - 我在这个确切的实例中更改了 gh-pages 以包含 .js 文件,因此 IE 不再损坏,但仍然需要任何反馈:))

采纳答案by shelhamer

I can't help you with tricking IE, and I think from that angle what you are looking for is impossible (and discouraged, since that is not the purpose of Github's raw URLs).

我无法帮助您欺骗 IE,我认为从这个角度来看,您正在寻找的东西是不可能的(并且不鼓励,因为这不是 Github 原始 URL 的目的)。

However, you can automate committing the changes to gh-pagesand pushing to make your life easier. You can do it with a post-commit hookto update the relevant files in the gh-pagesbranch automatically. I've cooked up such a post-commitscript that watches for changes to certain files and commits them to another branch:

但是,您可以自动提交更改gh-pages并推送,以使您的生活更轻松。您可以使用 apost-commit hookgh-pages自动更新分支中的相关文件。我post-commit编写了这样一个脚本来监视对某些文件的更改并将它们提交到另一个分支:

#!/bin/sh

WATCH_BRANCH="master"
WATCH_FILES="jquery-imask-min.js"
DEST_BRANCH="gh-pages"

# bail out if this commit wasn't made in the watched branch
THIS_BRANCH=$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)//');
if [ "$THIS_BRANCH" != "$WATCH_BRANCH" ]; then
  exit 0
fi

# only update if watched files have changed in the latest commit
CHANGED_FILES=$(git show --pretty="format:" --name-only $WATCH_BRANCH)
if $(echo "$CHANGED_FILES" | grep "^$WATCH_FILES$" -q); then
  # checkout destination branch, then
  # checkout latest version of each watched file and add to index
  git checkout -q $DEST_BRANCH
  git pull -q
  SAVEIFS=$IFS
  IFS=$(echo -n "|")
  for file in $WATCH_FILES; do
    git checkout $WATCH_BRANCH -- $file
    git add $file > /dev/null
  done
  IFS=$SAVEIFS
  # commit with a chance to edit the message, then go back to watched branch
  LATEST_COMMIT=$(git rev-parse $WATCH_BRANCH)
  git commit -m "Also including changes from $WATCH_BRANCH's $LATEST_COMMIT"
  git push origin $DEST_BRANCH
  git checkout -q $WATCH_BRANCH
fi

Note that this is a general script, though I have specified the config vars at the top for your purposes. $WATCH_FILEScan be set to a list of files delimited by braces |such as index.html|js/jquery.js. Paths must be specified relative to the root of the repo.

请注意,这是一个通用脚本,但我已在顶部指定了配置变量以供您使用。$WATCH_FILES可以设置为由大括号分隔的文件列表,|例如index.html|js/jquery.js. 必须相对于存储库的根指定路径。

Let me know if you have any questions, and if the script helps you!

如果您有任何问题,以及脚本是否对您有帮助,请告诉我!

回答by H Sampat

You can try using https://rawgit.com/service. Just replace raw.github.com with rawgit.com

您可以尝试使用https://rawgit.com/服务。只需将 raw.github.com 替换为 rawgit.com

UPDATE

更新

The Rawgit service (former Rawgithub) has been shutdown.

Rawgit 服务(以前的 Rawgithub)已关闭。

RawGit has reached the end of its useful life October 8, 2018

GitHub repositories that served content through RawGit within the last month will continue to be served until at least October of 2019. URLs for other repositories are no longer being served.

If you're currently using RawGit, please stop using it as soon as you can.

RawGit 已达到其使用寿命 2018 年 10 月 8 日

上个月通过 RawGit 提供内容的 GitHub 存储库将继续提供至少到 2019 年 10 月。不再提供其他存储库的 URL。

如果您目前正在使用 RawGit,请尽快停止使用。

回答by Tekkub

Github's raw URLs aren't designed to be a generic web host. Push that stuff off to proper host, like say pages.github.com.

Github 的原始 URL 并非设计为通用 Web 主机。把这些东西推到合适的主机上,比如 pages.github.com。

回答by neoascetic

Take a look at raw.githack.com. The idea of this service is inspired from rawgit.com. I just realized that using a whole framework (node.js + express.js) for such simple thing as requests proxying is overkilling, and made same stuff using nginx only.

看看raw.githack.com。这项服务的想法来自于 rawgit.com。我刚刚意识到将整个框架(node.js + express.js)用于请求代理这样简单的事情是过度杀伤力的,并且仅使用 nginx 制作了相同的东西。

Replace "githubusercontent" domain name chunk in your github/gist URL with "githack" and you're done!

将 github/gist URL 中的“githubusercontent”域名块替换为“githack”就大功告成了!

Furthermore, it supports bitbucket.com- simply replace whole bitbucket domain with bb.githack.com.

此外,它支持bitbucket.com- 只需将整个 bitbucket 域替换为bb.githack.com.

回答by Rodrigo Gra?a

Nowadays theres jsDelivr, its open source, free and fast. And supports GitHub! https://www.jsdelivr.com/

现在有jsDelivr,它是开源的,免费且快速的。并支持GitHub!https://www.jsdelivr.com/

Furthermore its from trusted people/company.

此外,它来自可信赖的人/公司。

I say this because I'm not sure we can trust https://raw.githack.com/

我这么说是因为我不确定我们是否可以信任https://raw.githack.com/

回答by James M. Greene

I am also trying to achieve this. However, I cannot seem to get the solution from @shelhamer to work in my codebase. Below is the updated post-commit hook script that I used to get it working:

我也在努力实现这一目标。但是,我似乎无法从 @shelhamer 获得解决方案以在我的代码库中工作。下面是我用来让它工作的更新后的提交后钩子脚本:

#!/bin/sh

WATCH_BRANCH="master"
WATCH_FILES="jquery-imask-min.js"
DEST_BRANCH="gh-pages"

# bail out if this commit wasn't made in the watched branch
THIS_BRANCH=$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)//');
if [ "$THIS_BRANCH" != "$WATCH_BRANCH" ]; then
  exit 0
fi

# only update if watched files have changed in the latest commit
CHANGED_FILES=$(git show --pretty="format:" --name-only $WATCH_BRANCH)
if $(echo "$CHANGED_FILES" | grep "^$WATCH_FILES$" -q); then
  # checkout destination branch, then
  # checkout latest version of each watched file and add to index
  git checkout -q $DEST_BRANCH
  git pull -q
  SAVEIFS=$IFS
  IFS=$(echo -n "|")
  for file in $WATCH_FILES; do
    git checkout $WATCH_BRANCH -- $file
    git add $file > /dev/null
  done
  IFS=$SAVEIFS
  # commit with a chance to edit the message, then go back to watched branch
  LATEST_COMMIT=$(git rev-parse $WATCH_BRANCH)
  git commit -m "Also including changes from $WATCH_BRANCH's $LATEST_COMMIT"
  git push origin $DEST_BRANCH
  git checkout -q $WATCH_BRANCH
fi

I had to update the use of grep to make the regex successfully match (-P was not an option on the grep implementation included in Git Bash shell for Windows), add a git pulland a git push origin $DEST_BRANCH. Oh, and I had to add an empty shell of the directories and files in advance (perhaps just the directories would have sufficed?).

我必须更新 grep 的使用才能使正则表达式成功匹配(-P 不是 Windows 的 Git Bash shell 中包含的 grep 实现的一个选项),添加 agit pull和 a git push origin $DEST_BRANCH。哦,我不得不提前添加一个目录和文件的空壳(也许只有目录就足够了?)。

Since this is actually doing a push, I think it may be better to switch this script to being a post-receive script instead of post-commit. Otherwise, you could be pushing code to gh-pages that never made it into master [if you choose not to push it].

由于这实际上是在进行推送,因此我认为将此脚本切换为接收后脚本而不是提交后脚本可能会更好。否则,您可能会将代码推送到从未成为 master 的 gh-pages [如果您选择不推送它]。