我们是否允许直接从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上允许这样做。这样我就不用担心更新本地文件了。
当然,为什么不?它甚至更新保存,除非有人在git中更改历史记录并强制推送。
已经提到了优质的服务RawGit,但我会把另一个扔进戒指:GitCDN.link
优点:
完全披露,我是GitCDN.link的项目维护者
您可以使用外部服务器rawgithub.com
。只需删除单词'raw'和'github'https://raw.github.com/ .. => https://rawgithub.com/之间的点,然后使用它。 More info you find in this question.
然而,根据rawgithub网站,它将在2019年10月底关闭。
您需要执行以下步骤
该文件将充当CDN。您还可以使用gist网址。
您可以直接链接到原始文件,但最好不要这样做,因为原始文件始终使用普通/文本标头发送,并且可能导致加载问题。
使用名称“gh-pages”为您的项目添加分支,然后您(在分支后不久)能够使用https://username.github.io/project/master/style.css等直接URL(使用您的URL,并假设“style.css”是一个文件, “master”文件夹位于“项目”存储库的根目录中......并且您的Github帐户是“用户名”)。
GitHub页面:https://yourusername.github.io/script.js
GitHub repo原始文件:https://github.com/yourusername/yourusername.github.io/blob/master/script.js
使用GitHub页面,不要使用原始文件。
原因:GitHub页面基于CDN,原始文件不是。访问原始文件将直接命中GitHub服务器并增加服务器负载。
在搜索相同的功能后,我最终编写了自己的PHP
脚本作为代理。我遇到的麻烦就是当你从Github
获得RAW版本/链接并在你自己的页面中链接到它时,发送的标题是'text / plain'而Chrome
没有从JavaScript
执行我的Github
文件。我也不喜欢使用第三方服务发布的其他链接,因为可能存在明显的安全/篡改问题。
因此,使用此脚本,我可以从Github
传递RAW链接,让脚本设置正确的标头,然后输出文件,就像它来自我自己的服务器一样。此脚本还可以与安全应用程序一起使用,以引入非安全脚本,而不会抛出“使用非安全链接”的SSL
错误警告。
链接:
<script src =“proxy.php?link = https://raw.githubusercontent.com/UserName/repo/master/my_script.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;
?>
如果您的网络服务器有活动的allow_url_include,那么将文件作为原始普通/文本提供服务的GitHub不是问题,因为您可以先将文件包含在PHP脚本中并将其标题修改为正确的MIME类型。