-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
125 lines (114 loc) · 4.34 KB
/
main.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
const YTapiKey = "QUl6YVN5QnRNVkdMcVgzemdvMWdLV3FTNE9naW1FLXM2TDhxbGtr";
var RickDetected = 0;
var lastYTid;
function readJSONfile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4) {
if (rawFile.status == "200") {
callback(rawFile.responseText);
} else if (parseInt(rawFile.status) >= 400) {
callback(parseInt(rawFile.status));
}
}
}
rawFile.send(null);
}
function youtubeParser(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(shorts\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match(regExp);
if (match) {
if (typeof match[7] !== 'undefined' && match[7].length==11) return match[7];
if (typeof match[8] !== 'undefined' && match[8].length==11) return match[8];
}
return false;
}
function isThisArickroll(rickLink) {
document.getElementById("rickornot").innerHTML = "";
if (!rickLink) return;
RickDetected = 0;
var rickYTid = youtubeParser(rickLink);
if (typeof(rickYTid) != 'string') RickDetected = -1;
readJSONfile("https://raw.githubusercontent.com/PGgamer2/Rickroll-Detector/main/rickrolls.json", function(callback) {
var totalLinks;
if (typeof(callback) != 'string') {
RickDetected = -3;
} else totalLinks = JSON.parse(callback);
// Check if video's ID is between these ones
if (RickDetected == 0) {
for (var i = 0; i < totalLinks.YouTube.Video.length; i++) {
if (rickYTid == youtubeParser(totalLinks.YouTube.Video[i])) {
RickDetected = 1;
break;
}
}
for (var i = 0; i < rickrollHistory.length; i++) {
if (rickYTid == youtubeParser(rickrollHistory[i])) {
RickDetected = 1;
break;
}
}
}
if (RickDetected == 0) {
readJSONfile("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + rickYTid + "&key=" + atob(YTapiKey), function(callback) {
var videoInfos;
if (typeof(callback) != 'string') {
if (callback == 403) {
RickDetected = -2; // YT quota exceeded
} else RickDetected = -1;
} else videoInfos = JSON.parse(callback);
try {
// Check if video's author is between these ones
if (RickDetected == 0) {
for (var i = 0; i < totalLinks.YouTube.Channel.length; i++) {
if (totalLinks.YouTube.Channel[i] == videoInfos.items[0].snippet.channelId) {
RickDetected = 1;
break;
}
}
}
if (RickDetected == 0) {
var vidTitle = videoInfos.items[0].snippet.title.replace(/ /g, '').toLowerCase();
var vidDescription = videoInfos.items[0].snippet.description.replace(/ /g, '').toLowerCase();
if (vidTitle.indexOf("rickroll") !== -1 || vidTitle.indexOf("nevergonnagiveyouup") !== -1 || vidTitle.indexOf("rickastley")!== -1 ) {
// Check if title contains any combination of "nevergonnagiveyouup"
RickDetected = 2;
} else if (vidDescription.indexOf("rickroll") !== -1 || vidDescription.indexOf("nevergonnagiveyouup" || vidTitle.indexOf("rickastley")!== -1) !== -1) {
// Check if description contains any combination of "nevergonnagiveyouup"
RickDetected = 2;
}
}
} catch (error) {
RickDetected = -1;
}
DisplayRickRoll(rickYTid);
});
} else {
DisplayRickRoll(rickYTid);
}
});
}
const resultMessages = [["This URL seems Rickroll-free! You're safe.", "lightgreen"],
["A Rickroll has been detected!", "red"],
["This is probably a Rickroll.", "yellow"]];
const errorsMessages = [["This isn't a valid YouTube URL!", "white"],
["YouTube quota has been exceeded. Try again later.", "purple"],
["An error has occurred! Cannot get the blacklist.", "violet"]];
function DisplayRickRoll(ytID) {
lastYTid = ytID;
if (RickDetected < 0 || RickDetected == 1) {
document.getElementById("addToHistory").style.display = "none";
} else {
document.getElementById("addToHistory").style.display = "";
}
if (RickDetected == 1) addToHistory(ytID);
if (RickDetected < 0) {
document.getElementById("rickornot").innerHTML = errorsMessages[-RickDetected - 1][0];
document.getElementById("rickornot").style.color = errorsMessages[-RickDetected - 1][1];
} else {
document.getElementById("rickornot").innerHTML = resultMessages[RickDetected][0];
document.getElementById("rickornot").style.color = resultMessages[RickDetected][1];
}
}