82

我们是否允许直接从 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 代码允许这样做。这样我就不必担心更新本地文件了。

4

10 回答 10

78

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

好处:

  • 让您链接到特定的提交,以及自动获取最新的(又名大师)
  • 不会因高流量而受到损害;RawGit 要求它的 dev.rawgit.com 链接只能在开发过程中使用,因为 GitCDN 让您可以访问最新版本,而没有服务器爆炸的危险
  • 为您提供自动缩小 HTML、CSS 和 JavaScript 的选项,或以书面形式提供 ( https://min.gitcdn.link )。
  • 添加压缩 (GZip)
  • 添加所有正确的标题(内容类型、缓存控制、电子标签等)

完全披露,我是GitCDN.link的项目维护者

于 2015-06-26T06:40:13.167 回答
39

您可以使用外部服务器rawgithub.com。只需删除单词 'raw' 和 'github' https://raw.github.com/ .. => https://rawgithub.com/之间的一个点并使用它。您在这个问题中找到的更多信息。

但是,根据 rawgithub 网站,它将在 2019 年 10 月结束时关闭。

于 2013-09-23T18:25:25.120 回答
15

您可以直接链接到原始文件,但最好不要这样做,因为原始文件总是以纯/文本标题发送并可能导致加载问题。

于 2013-01-15T00:16:01.730 回答
13

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

于 2013-03-18T18:05:45.597 回答
12

您需要执行以下步骤

  1. 从 github 获取文件的原始 url。类似于https://raw.githubusercontent.com/username/folder/example.css

  2. 访问http://rawgit.com/。将上面的 git url 粘贴到输入框中。它将生成两个 url,一个用于开发,另一个用于生产目的。

  3. 复制其中任何一个,您就完成了。

该文件将充当 CDN。您也可以使用 gist 网址。

于 2014-06-26T08:34:02.353 回答
11

GitHub 页面:https://yourusername.github.io/script.js
GitHub 存储库原始文件:https://github.com/yourusername/yourusername.github.io/blob/master/script.js

使用 GitHub Pages,不要使用原始文件。

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

于 2018-05-30T07:26:43.617 回答
8

对于那些在这篇文章中结束并且只想从 GitHub 中的图像中获取原始链接的人:

如果是图像的情况,您可以在文件链接的末尾添加“?raw=true”。例如原始链接: https ://github.com/githubusername/repo_name/blob/master/20160309_212617-1.png

原始链接: https ://github.com/githubusername/repo_name/blob/master/20160309_212617-1.png?raw=true

于 2020-08-30T12:02:38.790 回答
6

使用 jsdelivr.com

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

加载任何 GitHub 版本、提交或分支
注释:我们建议对支持它的项目使用 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/

于 2020-06-02T10:46:00.387 回答
1

在搜索了相同的功能之后,我最终编写了自己的PHP脚本来充当代理。我一直遇到的麻烦是,即使您从Github自己的页面中获取 RAW 版本/链接并链接到它,发送过来的标题是“文本/纯文本”并且Chrome没有JavaScriptGithub. 由于可能存在明显的安全/篡改问​​题,我也不喜欢为使用第三方服务而发布的其他链接。

所以使用这个脚本,我可以通过 RAW 链接Github,让脚本设置正确的标题,然后输出文件,就好像它来自我自己的服务器一样。此脚本还可以与安全应用程序一起使用,以引入非安全脚本,而不会引发SSL“使用非安全链接”的错误警告。

链接:

<script src="proxy.php?link= https://raw.githubusercontent.com/UserName/repo/master/my_script.js "></script>

代理.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;
?>
于 2014-07-10T13:26:59.507 回答
-1

如果您的网络服务器具有活动的 allow_url_include,则 GitHub 将文件作为原始纯文本/文本提供服务不是问题,因为您可以首先将文件包含在 PHP 脚本中并将其标头修改为正确的 MIME 类型。

于 2018-08-17T08:04:45.020 回答