如何直接从Github (raw.github.com)链接文件

108

我们能直接从Github链接文件吗?

<link rel="stylesheet" href="https://raw.github.com/username/project/master/style.css"/>
<script src="https://raw.github.com/username/project/master/script.js"></script>
我知道在Google Code上是允许这样做的。这样我就不用担心更新本地文件了。

4
您可以在这里看到一个带有良好答案的类似问题:https://dev59.com/qWQm5IYBdhLWcg3wyhZE - AuthorProxy
5
可能是 Link and execute external JavaScript file hosted on GitHub 的重复问题。 - Troy Alford
另一种用于原始内容的链接格式(使用双斜杠)是“https://github.com/OWNER/REPO/raw/main//README.md”。 - Joe Bowbeer
这将重定向到 https://raw.githubusercontent.com/OWNER/REPO/raw/main/README.md - anjanesh
你可以直接使用GitHub的原始内容URL来提供文件。在文件URL中将blob替换为raw以获取原始内容链接。例如:https://github.com/用户名/仓库/raw/main/路径/到/文件.扩展名 - undefined
12个回答

81

已经提到了很棒的服务RawGit,但我还想介绍另一个:GitCDN.link

好处:

  • 可以链接到特定的提交,也可以自动获取最新版本(即主分支)
  • 不会因高流量而受损;RawGit要求仅在开发期间使用其dev.rawgit.com链接,而GitCDN则让您访问最新版本,而无需担心服务器爆炸的危险
  • 提供自动缩小HTML、CSS和JavaScript的选项,或按原样提供服务(https://min.gitcdn.link)。
  • 添加压缩(GZip)
  • 添加所有正确的标头(Content-Type、cache-control、e-tag等)

完全透明,我是GitCDN.link的项目维护者

由于Heroku政策变更删除了所有免费层级,此服务已关闭。


2
这正是我一直在寻找的 - 非常感谢您提供的服务。建议:与其要求原始URL,不如使其能够与常规URL一起使用(类似于rawgit)。 - thdoan
2
@10basetom:好建议!我会把它加入路线图! - Shane Gadsby
1
@ShaneGadsby 非常感谢。由于设置了正确的标头,它强制下载文件,这是我想要的行为。我没有其他机会强制下载,因为即使使用HTML锚元素时,GitHub甚至也会剥离download属性。 - Michael Kühnel
9
当我尝试这样做时,出现了一个“内部服务器错误”。 - tresf
1
@ShaneGadsby 还能用吗?当我将原始链接粘贴到页面输入框中时,什么也没有发生。 - mindplay.dk
显示剩余3条评论

38

9
还有一个名为 https://rawgit.com/ 的网站可以用来提供原始文件。RawGit 通过适当的 Content-Type 头直接从 GitHub 服务器上提供原始文件。 - sentenza
6
我认为rawgithub.com和rawgit.com是同一个服务,只是更名了 :) - Shane Gadsby
似乎 RawGit 在 Chrome 上存在问题?https://snag.gy/VknHNQ.jpg - danielh
31
截至2018年10月8日,RawGit(也称为rawgithub)处于逐渐停用阶段。请到其他地方寻找替代品。 - Poulsbo

31

虽然没有“Access-Control-Allow-Origin”头,但这并不是很有用。 - Douglas Gaskell
如果您在浏览器中导航到该URL,它现在会重定向到“raw.githubusercontent”链接。 - theZ3r0CooL
@DouglasGaskell 或许在过去是这样,但现在,头部信息已经变成了:Access-Control-Allow-Origin: * - tony19
3
@theZ3r0CooL 这对我来说是一个优势。添加 ?raw=true 比手动修改前导路径更容易且更少出错(假设所有操作都是手动完成的)。 - tony19

20

GitHub Pages:https://yourusername.github.io/script.js
GitHub仓库Raw文件:https://github.com/yourusername/yourusername.github.io/blob/master/script.js

请使用GitHub Pages,不要使用Raw文件。

原因: GitHub Pages基于CDN,而Raw文件不是。访问Raw文件将直接命中GitHub服务器并增加服务器负载。


4
有来源吗?“原始文件不是” - Franklin Yu
不是完美的,但它像魔法一样工作!谢谢! - Patrick José Pereira
@Sista,是“are not”还是“cannot”?因为前者只需要时间就可以解决。 - Pacerier

16
您可以直接链接到原始文件,但最好不要这样做,因为原始文件总是带有 plain/text 头,并可能导致加载问题。

3
防止在 jsfiddle 中加载 - jack

15

使用名称为“gh-pages”的分支添加到您的项目中,然后(分支后不久)您将能够使用直接URL,例如https://username.github.io/project/master/style.css(使用您的URL,并假设“style.css”是位于“project”存储库根目录中“master”文件夹内的文件...而且您的Github帐户名为“用户名”)。


1
URL模式自2013年以来可能已经发生了变化:您不再需要在URL中添加“/master”,现在只需使用https://username.github.io/project/file-path即可。 - Andreas Rozek

14

21
"RawGit已经到达其有用寿命的尽头" - 2018年10月8日 - showdev

12

使用 jsdelivr.com

https://www.jsdelivr.com/?docs=gh中直接复制:

加载任何 GitHub 发布、提交或分支
注意:对于支持 npm 的项目,我们建议使用 npm

https://cdn.jsdelivr.net/gh/user/repo@version/file

加载 jQuery v3.2.1

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/dist/jquery.min.js

使用版本范围而不是特定版本

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2/dist/jquery.min.js

https://cdn.jsdelivr.net/gh/jquery/jquery@3/dist/jquery.min.js

完全省略版本以获取最新版本

你不应该在生产环境中使用此选项

https://cdn.jsdelivr.net/gh/jquery/jquery/dist/jquery.min.js

将“.min”添加到任何 JS/CSS 文件以获取缩小版本

如果不存在,我们会为您生成它

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/src/core.min.js

在结尾添加 / 以获取目录列表

https://cdn.jsdelivr.net/gh/jquery/jquery/


0
在寻找同样的功能后,我编写了自己的PHP脚本作为代理。我遇到的问题是即使你从Github获取原始版本/链接并在自己的页面上链接它,发送的标头仍然是“text/plain”,而Chrome无法执行来自Github的JavaScript文件。我也不喜欢使用第三方服务的其他链接,因为可能存在明显的安全/篡改问题。
因此,使用这个脚本,我可以传递来自Github的RAW链接,让脚本设置正确的标头,然后将文件输出,就像它来自我的服务器一样。这个脚本还可以与安全应用程序一起使用,以拉取非安全脚本,而不会引发“使用非安全链接”的SSL错误警告。
链接方式:

proxy.php

<?php
###################################################################################################################
# 
# This script can take two URL variables
# 
# "type"
#   OPTIONAL
#   STRING
#   Sets the type of file that is output
# 
# "link"
#   REQUIRED
#   STRING
#   The link to grab and output through this proxy script
# 
###################################################################################################################



# First we need to set the headers for the output file
# So check to see if the type is specified first and if so, then set according to what is being requested
if(isset($_GET['type']) && $_GET['type'] != ''){
    switch($_GET['type']){
        case 'css':
            header('Content-Type: text/css');
            break;

        case 'js':
            header('Content-Type: text/javascript');
            break;

        case 'json':
            header('Content-Type: application/json');
            break;

        case 'rss':
            header('Content-Type: application/rss+xml; charset=ISO-8859-1');
            break;

        case 'xml':
            header('Content-Type: text/xml');
            break;

        default:
            header('Content-Type: text/plain');
            break;
    }

# Otherwise, try and determine what file type should be output by the file extension from the link
}else{
    # See if we can find a file type in the link specified and set the headers accordingly

    # If css file extension is found, then set the headers to css format
    if(strstr($_GET['link'], '.css') != FALSE){
        header('Content-Type: text/css');

    # If javascript file extension is found, then set the headers to javascript format
    }elseif(strstr($_GET['link'], '.js') != FALSE){
        header('Content-Type: text/javascript');

    # If json file extension is found, then set the headers to json format
    }elseif(strstr($_GET['link'], '.json') != FALSE){
        header('Content-Type: application/json');

    # If rss file extension is found, then set the headers to rss format
    }elseif(strstr($_GET['link'], '.rss') != FALSE){
        header('Content-Type: application/rss+xml; charset=ISO-8859-1');

    # If css xml extension is found, then set the headers to xml format
    }elseif(strstr($_GET['link'], '.xml') != FALSE){
        header('Content-Type: text/xml');

    # If we still haven't found a suitable file extension, then just set the headers to plain text format
    }else{
        header('Content-Type: text/plain');
    }
}

# Now get the contents of our page we're wanting
$contents = file_get_contents($_GET['link']);

# And finally, spit everything out
echo $contents;
?>

LOL 有点 hacky,但这是我选的解决方案 LOL... 原本想使用 https://cdn.jsdelivr.net/,但当你因为 CDN 缓存而进行大量更改时,它并不适合开发... 不管怎样,感谢提供快速脚本! - Banning

0
RawGit是一个流行的服务,可以直接从GitHub存储库中提供原始文件。然而,正如多个评论所提到的,它已经到了生命周期的尽头。
有其他选项可以从GitHub存储库中提供原始文件。

GitHub原始内容URL

您可以直接使用GitHub的原始内容URL来提供文件。例如:
https://github.com/username/repository/raw/main/path/to/file.ext

如示例所示,您可以在文件URL中将blob替换为raw以获取原始内容链接。

GitHub Pages

GitHub Pages是一种从您的GitHub存储库直接托管静态网站和资源(包括图像)的方式。您可以创建一个gh-pages分支或使用主分支中的docs文件夹来托管您的资源。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接