forked from neoascetic/rawgithack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawgithack.js
135 lines (114 loc) · 5.17 KB
/
rawgithack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(function (doc) {
"use strict";
const GITHUB_API_URL = 'https://api.github.com';
const TEMPLATES = [
[/^(https?):\/\/gitlab\.com\/([^\/]+.*\/[^\/]+)\/(?:raw|blob)\/(.+?)(?:\?.*)?$/i,
'$1://gl.githack.com/$2/raw/$3'],
[/^(https?):\/\/bitbucket\.org\/([^\/]+\/[^\/]+)\/(?:raw|src)\/(.+?)(?:\?.*)?$/i,
'$1://bb.githack.com/$2/raw/$3'],
// snippet file URL from web interface, with revision
[/^(https?):\/\/bitbucket\.org\/snippets\/([^\/]+\/[^\/]+)\/revisions\/([^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i,
'$1://bb.githack.com/!api/2.0/snippets/$2/$3/files/$4'],
// snippet file URL from web interface, no revision
[/^(https?):\/\/bitbucket\.org\/snippets\/([^\/]+\/[^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i,
'$1://bb.githack.com/!api/2.0/snippets/$2/HEAD/files/$3'],
// snippet file URLs from REST API
[/^(https?):\/\/bitbucket\.org\/\!api\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i,
'$1://bb.githack.com/!api/2.0/snippets/$2/files/$3'],
[/^(https?):\/\/api\.bitbucket\.org\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i,
'$1://bb.githack.com/!api/2.0/snippets/$2/files/$3'],
// welcome rawgit refugees
[/^(https?):\/\/(?:cdn\.)?rawgit\.com\/(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,
'$1://gist.githack.com/$2'],
[/^(https?):\/\/(?:cdn\.)?rawgit\.com\/([^\/]+\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i,
'$1://raw.githack.com/$2/$3'],
// ...and maybe gitcdn.xyz? ;)
[/^(https?):\/\/gitcdn\.(?:xyz|link)\/[^\/]+\/(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,
'$1://gist.githack.com/$2'],
[/^(https?):\/\/gitcdn\.(?:xyz|link)\/[^\/]+\/([^\/]+\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i,
'$1://raw.githack.com/$2/$3'],
[/^(https?):\/\/raw\.github(?:usercontent)?\.com\/([^\/]+\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i,
'$1://raw.githack.com/$2/$3'],
[/^(https?):\/\/github\.com\/(.[^\/]+?)\/(.[^\/]+?)\/(?!releases\/)(?:(?:blob|raw)\/)?(.+?\/.+)/i,
'$1://raw.githack.com/$2/$3/$4'],
[/^(https?):\/\/gist\.github(?:usercontent)?\.com\/(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,
'$1://gist.githack.com/$2']
];
var prodEl = doc.getElementById('url-prod');
var devEl = doc.getElementById('url-dev');
var urlEl = doc.getElementById('url');
new Clipboard('.url-copy-button');
var devCopyButton = doc.getElementById('url-dev-copy');
var prodCopyButton = doc.getElementById('url-prod-copy');
if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
devCopyButton.style.display = 'inline-block';
prodCopyButton.style.display = 'inline-block';
}
urlEl.addEventListener('input', formatURL, false);
if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) {
// On iOS, it's quite difficult to copy the value of readonly input elements (see https://git.io/vpI8Z).
// By making the inputs non-readonly and preventing keydown we can mimic the behaviour of readonly inputs while
// improving the copy-input-value interaction.
inputDev.removeAttribute('readonly')
inputProd.removeAttribute('readonly')
inputDev.addEventListener('keydown', function (e) {
e.preventDefault();
});
inputProd.addEventListener('keydown', function (e) {
e.preventDefault();
});
}
formatURL();
function formatURL() {
var url = urlEl.value = decodeURIComponent(urlEl.value.trim());
urlEl.classList.remove('valid');
urlEl.classList.toggle('invalid', url.length);
devEl.value = '';
prodEl.value = '';
devEl.classList.remove('valid');
prodEl.classList.remove('valid');
devCopyButton.disabled = true;
prodCopyButton.disabled = true;
for (var i in TEMPLATES) {
var pattern = TEMPLATES[i][0],
template = TEMPLATES[i][1];
if (pattern.test(url)) {
var ghUrl = url.replace(pattern, template);
var matches = ghUrl.match(/^(\w+:\/\/(raw).githack.com\/([^\/]+)\/([^\/]+))\/([^\/]+)\/(.*)/i);
if (!matches) {
devEl.value = ghUrl;
prodEl.value = cdnize(ghUrl);
setValid();
} else if (matches[2] === 'raw') {
devEl.value = ghUrl;
let apiUrl = `${GITHUB_API_URL}/repos/${matches[3]}/${matches[4]}/git/refs/heads/${matches[5]}`;
fetch(apiUrl)
.then(res => { if (res.ok) return res.json(); })
.then(data => {
let ref = data && data.object && data.object.sha ? data.object.sha : matches[5];
prodEl.value = cdnize(`${matches[1]}/${ref}/${matches[6]}`);
setValid();
});
}
break;
}
}
}
function cdnize(url) {
return url.replace(/^(\w+):\/\/(\w+)/, "$1://$2cdn");
}
function setValid() {
urlEl.classList.remove('invalid');
urlEl.classList.add('valid');
prodEl.classList.add('valid');
devEl.classList.add('valid');
devCopyButton.disabled = false;
prodCopyButton.disabled = false;
}
prodEl.addEventListener('focus', onFocus);
devEl.addEventListener('focus', onFocus);
urlEl.addEventListener('focus', onFocus);
function onFocus(e) {
setTimeout(function () { e.target.select(); }, 1);
}
}(document));