-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.html
61 lines (57 loc) · 1.85 KB
/
index.html
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
<html>
<head>
<title>Tiktok date extractor</title>
<style>
body {
margin: 1em;
font-family: "Helvetica", Arial, sans-serif;
font-size: 18px;
}
button, input {
font-size: 16px;
}
.form {
margin-bottom: 1em;
}
</style>
</head>
<body>
<script>
function getVidId() {
const tiktokUrl= document.querySelector("#url").value;
// This regex should be safe as "Only letters, numbers, underscores, or periods are allowed" in TikTok usernames.
const regex = /(?<=\/video\/)(.*?)(?=$|[^0-9])/;
const vidId = regex.exec(tiktokUrl)[0];
return vidId;
}
function extractUnixTimestamp(vidId) {
// BigInt needed as we need to treat vidId as 64 bit decimal. This reduces browser support.
const asBinary = BigInt(vidId).toString(2);
const first31Chars = asBinary.slice(0, 31);
const timestamp = parseInt(first31Chars, 2);
return timestamp;
}
function unixTimestampToHumanDate(timestamp) {
const milliseconds = timestamp * 1000;
const dateObject = new Date(milliseconds);
const humanDateFormat = dateObject.toUTCString()+" (UTC)";
return humanDateFormat;
}
function getDate() {
const vidId = getVidId();
const unixTimestamp = extractUnixTimestamp(vidId);
const humanDateFormat = unixTimestampToHumanDate(unixTimestamp);
document.querySelector("#date").textContent = humanDateFormat;
}
</script>
<div class="form">
<label for="url">Tiktok video URL:</label>
<input type="text" id="url" name="url" />
<button onclick="getDate()">Get uploaded date</button>
</div>
<div class="output">
<span id="label">Uploaded on: </span>
<span id="date"></span>
</div>
</body>
</html>