gpt4 book ai didi

How to link files directly from Github (raw.github.com)(如何直接从Github(raw.githeb.com)链接文件)

转载 作者:bug小助手 更新时间:2023-10-25 17:11:12 27 4
gpt4 key购买 nike



Are we allowed to link files directly from Github ?

我们可以直接从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>


I know this is allowed on Google Code. This way I don't have to worry about updating a local file.

我知道这在谷歌代码上是允许的。这样,我就不必担心更新本地文件了。


更多回答

You can see question analog with good answers here: stackoverflow.com/questions/17341122/…

你可以在这里看到有好答案的模拟问题:Stackoverflow.com/Questions/17341122/…

Possible duplicate of Link and execute external JavaScript file hosted on GitHub

可能复制GitHub上托管的链接和执行外部JavaScript文件

Another link format (with double-slash) for raw content is "github.com/OWNER/REPO/raw/main//README.md"

原始内容的另一种链接格式(带双斜杠)是“githorb.com/owner/repo/raw/main//Readme.md”。

This redirects to raw.githubusercontent.com/OWNER/REPO/raw/main/README.md

这将重定向到raw.githubusercontent.com/OWNER/REPO/raw/main/README.md

You can directly use GitHub's raw content URL to serve files. Replace blob with raw in the file URL to get the raw content link. For example: https://github.com/username/repository/raw/main/path/to/file.ext

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

优秀答案推荐

The great service RawGit was already mentioned, but I'll throw another into the ring: GitCDN.link

前面已经提到了伟大的服务RawGit,但我将把另一个服务GitCDN.link抛到台上


Benefits:



  • Lets you link to specific commits, as well as auto-get the latest (aka master)

  • Incurs no damage from high traffic volumes; RawGit asks that it's dev.rawgit.com links be only used during development, where as GitCDN give you access to the latest version, without the danger of the servers exploding

  • Give you the option of auto minifying your HTML, CSS and JavaScript, or serving it as written (https://min.gitcdn.link).

  • Adds compression (GZip)

  • Adds all the correct headers (Content-Type, cache-control, e-tag, etc)


Full disclosure, I'm a project maintainer at GitCDN.link

完全公开,我是GitCDN.link的项目维护者


This service has been shuttered due to Heroku's policy change to remove all free tiers.

由于Heroku删除所有自由层的策略更改,此服务已关闭。



You can use external server rawgithub.com. Just remove a dot between words 'raw' and 'github' https://raw.github.com/.. => https://rawgithub.com/ and use it. More info you find in this question.

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



However, according to the rawgithub website it will be shutting down at the end of October 2019.

然而,根据rawgithorb网站的说法,它将于2019年10月底关闭。



Append ?raw=true at the end of the file link.

在文件链接的末尾追加?RAW=TRUE。




Example

示例


Original link:

原始链接:


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 link:

原始链接:


https://github.com/numpy/numpy/blob/main/doc/source/_static/favicon/apple-touch-icon.png?raw=true

Https://github.com/numpy/numpy/blob/main/doc/source/_static/favicon/apple-touch-icon.png?raw=true


That will automatically redirect to:

它将自动重定向至:


https://raw.githubusercontent.com/numpy/numpy/main/doc/source/_static/favicon/apple-touch-icon.png

Https://raw.githubusercontent.com/numpy/numpy/main/doc/source/_static/favicon/apple-touch-icon.png



GitHub Pages: https://yourusername.github.io/script.js

GitHub repo raw files: https://github.com/yourusername/yourusername.github.io/blob/master/script.js

GitHub页面:https://yourusername.github.io/script.js GitHub回购原始文件:https://github.com/yourusername/yourusername.github.io/blob/master/script.js



Use GitHub Pages, DO NOT use raw files.

使用GitHub页面,不要使用RAW文件。



Reason:
GitHub Pages are based on CDN, raw files are not. Accessing raw files will directly hit on GitHub servers and increase server load.

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



You can link directly to raw files, but it's best not to do it since the raw files always get sent with a plain/text header and can cause loading problems.

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



Add a branch your project using the name "gh-pages" and then you'll (shortly after branching) be able to use a direct URL such as https://username.github.io/project/master/style.css (using your URL, and assuming "style.css" is a file in the "master" folder in the root of your "project" repository...and that your Github account is "username").

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



You need carry out the following steps

您需要执行以下步骤




  1. Get the raw url of the file from github. Which is something like https://raw.githubusercontent.com/username/folder/example.css


  2. Visit http://rawgit.com/. Paste the git url above in the input box. It will generate two url's, one for development and other for production purpose.


  3. Copy any one of them and you are done.




The file will act as a CDN. You can also use gist urls.

该文件将充当CDN。您也可以使用GIST URL。



Use jsdelivr.com

使用jsdelivr.com


Copied directly from https://www.jsdelivr.com/?docs=gh:

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


load any GitHub release, commit, or branch

note: we recommend using npm for projects that support it

加载任何GitHub版本、提交或分支注意事项:我们建议对支持它的项目使用NPM


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

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


load jQuery v3.2.1

加载jQuery v3.2.1


https://cdn.jsdelivr.net/gh/jquery/[email protected]/dist/jquery.min.js

Https://cdn.jsdelivr.net/gh/jquery/[email受保护]/dist/jquery.min.js


use a version range instead of a specific version

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


https://cdn.jsdelivr.net/gh/jquery/[email protected]/dist/jquery.min.js

Https://cdn.jsdelivr.net/gh/jquery/[email受保护]/dist/jquery.min.js


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

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


omit the version completely to get the latest one

完全忽略该版本以获取最新版本


you should NOT use this in production

您不应在生产中使用此选项


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

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


add ".min" to any JS/CSS file to get a minified version

将“.min”添加到任何JS/css文件即可获得精简版本


if one doesn't exist, we'll generate it for you

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


https://cdn.jsdelivr.net/gh/jquery/[email protected]/src/core.min.js

Https://cdn.jsdelivr.net/gh/jquery/[email受保护]/src/core.min.js


add / at the end to get a directory listing

在末尾添加/以获得目录清单


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

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



After searching for this same functionality, I ended up writing my own PHP script to act as a proxy. The trouble I kept running into is even when you get the RAW version/link from Github and link to it in your own page, the header sent over was 'text/plain' and Chrome was not executing my JavaScript file from Github. I also didn't like the other links posted for using third party services because of the obvious security/tampering issues possible.

在搜索了相同的功能之后,我最终编写了自己的PHP脚本来充当代理。我一直遇到的麻烦是,即使当你从Github那里获得原始版本/链接并在你自己的页面中链接到它时,发送过来的头文件也是‘Text/Plain’,而Chrome没有执行我从Github发来的JavaScript文件。我也不喜欢发布的其他使用第三方服务的链接,因为可能存在明显的安全/篡改问题。



So using this script, I can pass over the RAW link from Github, have the script set the correct headers, and then output the file as if it were coming from my own server. This script can also be used with a secure application to pull in non-secure scripts without throwing SSL errors warning of "Non-secure links used".

因此,使用这个脚本,我可以传递来自Github的原始链接,让脚本设置正确的头,然后输出文件,就像它来自我自己的服务器一样。此脚本还可以与安全应用程序一起使用,以引入不安全的脚本,而不会抛出警告“使用的不安全链接”的SSL错误。



Linking:

链接:




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




proxy.php

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;
?>


RawGit was a popular service for serving raw files directly from GitHub repositories. However, as multiple comments are mentioning, it has reached the end of its lifetime.

RawGit是一项流行的服务,用于直接从GitHub存储库提供原始文件。然而,正如许多评论所提到的那样,它已经到了生命的尽头。


There are alternative options for serving raw files from GitHub repositories.

提供来自GitHub存储库的原始文件还有其他选择。


GitHub Raw Content URL


You can directly use GitHub's raw content URL to serve files. For example:

您可以直接使用GitHub的原始内容URL来提供文件。例如:


https://github.com/username/repository/raw/main/path/to/file.ext

As shown in the example, you can replace blob with raw in the file URL to get the raw content link.

如本例所示,您可以在文件URL中将BLOB替换为RAW,以获得原始内容链接。


GitHub Pages


GitHub Pages is a way to host static websites and assets, including images, directly from your GitHub repository. You can create a gh-pages branch or use the docs folder in your main branch to host your assets.

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



If your webserver has active allow_url_include, GitHub serving the files as raw plain/text is not a problem since you can include the file first in a PHP script and modify its Headers to the proper MIME type.

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


更多回答

This is just what I was looking for -- thanks a lot for the service. Suggestion: instead of requiring the raw URL, you should make it work with the regular URL as well (similar to rawgit).

这正是我要找的--非常感谢你的服务。建议:不需要原始URL,而应该让它与常规URL一起工作(类似于rawgit)。

@10basetom: Great suggestion! I'll add it to the roadmap!

@10basetom:很棒的建议!我会把它添加到路线图中!

@ShaneGadsby Thank you so much. Because of setting the correct headers it forces to download a file which was my desired behavior. I had no other chance to force the download because GitHub even strip the download attribute when using HTML anchor elements.

@ShaneGadsby非常感谢。由于设置了正确的头文件,它会强制下载文件,这是我想要的行为。我没有其他机会强制下载,因为GitHub在使用HTML锚元素时甚至剥离了Download属性。

I'm getting an Internal Server Error when I try this.

我尝试此操作时收到内部服务器错误。

@ShaneGadsby does it still work? nothing happens when I paste a raw link into the input on the page.

@ShaneGadsby它还能用吗?当我将原始链接粘贴到页面上的输入中时,没有任何反应。

There is also rawgit.com for serving raw files. RawGit serves raw files directly from GitHub with proper Content-Type headers.

还有提供原始文件服务的rawgit.com。RawGit使用正确的Content-Type标头直接从GitHub提供原始文件。

I believe that rawgithub.coma nd rawgit.com are the same service, that has been renamed :)

我认为rawgithorb.com.rawgit.com是相同的服务,但已重新命名:)

It appears that rawgit has issues with Chrome? snag.gy/VknHNQ.jpg

看来Rawgit对Chrome有问题?Snag.gy/VKnHNQ.jpg

As of October 8, 2018, RawGit (aka rawgithub) is in a sunset phase. Look elsewhere.

截至2018年10月8日,RawGit(又名rawgithorb)正处于日落阶段。去别处看看吧。

There are no Access-Control-Allow-Origin headers though.... so it's not very useful.

但是,没有EST-Control-Allow-Origin标头。所以没什么用

If you navigate to that url in a browser it re-directs to the raw.githubusercontent link now

如果您在浏览器中导航到该URL,它现在会重定向到raw.githebusercontent链接

@DouglasGaskell That might've been true then, but now, the header is present as: Access-Control-Allow-Origin: *.

@DouglasGaskell这在当时可能是真的,但现在,标题是:Access-Control-Allow-Origin:*。

@theZ3r0CooL That's an advantage to me. Appending ?raw=true is easier and less error prone than modifying the leading path (assuming this is all done manually).

@theZ3r0CooL这对我来说是一个优势。附加?RAW=TRUE比修改前导路径更容易,也不容易出错(假设这都是手动完成的)。

raw files are not” Any source for that?

原始文件不是“有什么来源吗?”

It's not perfect, but it works like a charm! Thanks!

它不是完美的,但它的作用就像一种护身符!谢谢!

@Sista, Is it "are not" or "cannot"? Because the former can be fixed just with time.

@Sista,是“不是”还是“不能”?因为前者可以随着时间的推移而改变。

prevents loading in jsfiddle

防止在jsfiddle中加载

the URL pattern may have changed since 2013: you no longer have to add "/master" to the URL, https://username.github.io/project/file-path is sufficient now

自2013年以来,URL模式可能发生了变化:您不再需要在URL中添加“/MASTER”,现在https://username.github.io/project/file-path就足够了

"RawGit has reached the end of its useful life" - October 8, 2018

《RawGit已经到了使用寿命的尽头》--2018年10月8日

LOL kinda hacky but its the solution I went with LOL... was wanting to use cdn.jsdelivr.net BUT its not great for development when you're making a bunch of changes because of the CDN caching ... anyway thanks for the quick script!

哈哈,有点老生常谈,但这是我的解决方案,哈哈…我想使用cdn.jsdelivr.net,但当您因为CDN缓存而进行一系列更改时,这对开发并不是很好……无论如何,谢谢你的快速脚本!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com