-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongurl.js
75 lines (72 loc) · 1.77 KB
/
longurl.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
const base64 = {
charset: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".split(
""
),
encode: integer => {
if (integer === 0) {
return "0";
}
let s = [];
while (integer > 0) {
s = [base64.charset[integer % 64], ...s];
integer = Math.floor(integer / 64);
}
return s.join("");
},
decode: chars =>
chars
.split("")
.reverse()
.reduce(
(prev, curr, i) => prev + base64.charset.indexOf(curr) * 64 ** i,
0
)
};
function longUrl(hash) {
var urlStarts = {
u: "https://scratch.mit.edu/users/",
p: "https://scratch.mit.edu/projects/",
e: "editor",
s: "https://scratch.mit.edu/studios/",
d: "https://scratch.mit.edu/discuss/topic/",
o: "https://scratch.mit.edu/discuss/post/"
};
var type = urlStarts[hash[1]];
if (type == "https://scratch.mit.edu/users/") {
return type + hash.substring(2);
} else {
if (type == "editor") {
return (
"https://scratch.mit.edu/projects/" +
base64.decode(hash.substring(2)) +
"/editor"
);
} else {
return type + base64.decode(hash.substring(2));
}
}
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "false";
}
if (window.location.hash === "") {
window.location.replace("./home/");
} else {
var output = longUrl(window.location.hash);
if (getCookie("preview") === "true") {
window.location.replace("/preview#" + window.location.hash[1] + output);
} else {
window.location.replace(output);
}
}