【问题标题】:Including js from raw.github.com包括来自 raw.github.com 的 js
【发布时间】:2011-11-03 01:15:13
【问题描述】:

我有一个链接到 https://raw.github.com/.../master/.../file.js 的 github.com 演示页面,所以我没有每次更改时都需要将.js 文件复制到gh-pages 分支。这适用于除 IE 之外的所有浏览器:

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

此投诉是由于文件传输的事实:

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

这是我无法改变的。

有人知道如何完成同样的事情吗?不知何故允许我链接到master 分支中的文件,而不必总是将它推送到gh-pages 分支?

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

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

【问题讨论】:

    标签: javascript git internet-explorer github


    【解决方案1】:

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

    更新

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

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

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

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

    【讨论】:

    • 既然在这种情况下你已经在使用一个不属于 github 的网站,你不妨使用cdnjs.com
    • 谢谢,rawgithub 很完美。
    • 也看看raw.githack.com。这个想法是一样的,但它是一个普通的 nginx 解决方案,这意味着它应该工作得更快一点:)
    • raw.githack.com 现在支持 https。只需将raw.github.com 替换为raw.githack.com
    • 现在是rawgit.com
    【解决方案2】:

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

    但是,您可以自动将更改提交到 gh-pages 并推送以使您的生活更轻松。您可以使用post-commit hook 自动更新gh-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/* \(.*\)/\1/');
    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
    

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

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

    【讨论】:

      【解决方案3】:

      看看raw.githack.com。这项服务的想法来自 rawgit.com。我刚刚意识到使用整个框架(node.js + express.js)来处理请求代理这样简单的事情太过分了,并且只使用 nginx 来做同样的事情。

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

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

      【讨论】:

        【解决方案4】:

        Github 的原始 URL 并非设计为通用网络主机。将这些内容推送到适当的主机,例如 pages.github.com。

        【讨论】:

        • 是的,问题是每次我提交到 master 以切换到页面分支并复制文件时,它都需要额外的工作。我只是想避免这种情况,因为它似乎没有必要
        【解决方案5】:

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

        此外,它来自受信任的人/公司。

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

        【讨论】:

          【解决方案6】:

          我也在努力实现这一目标。但是,我似乎无法从@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/* \(.*\)/\1/');
          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
          

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

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

          【讨论】:

            猜你喜欢
            • 2016-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-08-19
            • 2014-04-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多