我们能直接从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上是允许这样做的。这样我就不用担心更新本地文件了。我们能直接从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上是允许这样做的。这样我就不用担心更新本地文件了。已经提到了很棒的服务RawGit,但我还想介绍另一个:GitCDN.link
好处:
完全透明,我是GitCDN.link的项目维护者
由于Heroku政策变更删除了所有免费层级,此服务已关闭。
download
属性。 - Michael Kühnelrawgithub.com
。只需在单词“raw”和“github”之间删除一个点https://raw.github.com/.. => https://rawgithub.com/并使用它。更多信息您可以在这个问题中找到。
然而,根据rawgithub网站的消息,它将在2019年10月底关闭。?raw=true
。
示例
原始链接:
https://github.com/numpy/numpy/blob/main/doc/source/_static/favicon/apple-touch-icon.png
原始链接:https://github.com/numpy/numpy/blob/main/doc/source/_static/favicon/apple-touch-icon.png?raw=true
那将自动重定向到:https://raw.githubusercontent.com/numpy/numpy/main/doc/source/_static/favicon/apple-touch-icon.png
Access-Control-Allow-Origin: *
。 - tony19?raw=true
比手动修改前导路径更容易且更少出错(假设所有操作都是手动完成的)。 - tony19GitHub 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服务器并增加服务器负载。
使用名称为“gh-pages”的分支添加到您的项目中,然后(分支后不久)您将能够使用直接URL,例如https://username.github.io/project/master/style.css(使用您的URL,并假设“style.css”是位于“project”存储库根目录中“master”文件夹内的文件...而且您的Github帐户名为“用户名”)。
从Github获取文件的原始URL,类似于https://raw.githubusercontent.com/username/folder/example.css
访问http://rawgit.com/。将上面的git url粘贴到输入框中。它会生成两个URL,一个用于开发,另一个用于生产目的。
复制其中任意一个,并完成操作。
使用 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
在结尾添加 / 以获取目录列表
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;
?>
https://github.com/username/repository/raw/main/path/to/file.ext
blob
替换为raw
以获取原始内容链接。
GitHub Pages是一种从您的GitHub存储库直接托管静态网站和资源(包括图像)的方式。您可以创建一个gh-pages
分支或使用主分支中的docs
文件夹来托管您的资源。
blob
替换为raw
以获取原始内容链接。例如:https://github.com/用户名/仓库/raw/main/路径/到/文件.扩展名
- undefined