-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
267 lines (237 loc) · 6.69 KB
/
content.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var title = document.getElementsByTagName("title")[0].innerHTML;
class Anilist{
constructor(){
this.url=window.location.href;
this.authenticated=false;
}
authenticate(){
let a = /access_token=[^&]+/gi.exec(this.url);
let AUTH_TOKEN=a[0].toString().replace(/access_token=/gi, "");
chrome.storage.sync.set({auth_token:AUTH_TOKEN,logged_in:true});
console.log(`logged in!! token was :\n ${AUTH_TOKEN} \n DO NOT SHARE!!`)
this.authenticated=true;
}
changeUI(){
if(this.authenticated){
//will change UI in future
}
}
//clears the chrome storage data of the extension only
resetData(){
let confirmAction = confirm("Are you sure to execute this action this will make u relogin?");
if (confirmAction) {
chrome.storage.sync.clear(()=>{
alert("You have cleared all your data saved in chrome");
})
} else {
alert("Action canceled");
}
}
}
//AnimePahe Class wrapper
class AnimePahe {
constructor() {
this.name = null;
this.episode = null;
this.isPaused = true;
}
//Gets the name and episode from the html>title of animepahe not too fancy
animepaheNameEpisode(str) {
let anime = str.split("::")[0];
let [name, episode] = anime.split("Ep.");
const intEp = parseInt(episode);
this.name = name;
this.episode = intEp;
return {
name,
intEp,
};
}
showAnimeName() {
return this.name;
}
showAnimeEpisode() {
return this.episode;
}
onLoaded() {
document.querySelector(".click-to-load").addEventListener("click", () => {
console.log('%c Player is Now Loaded!', 'font-weight: bold; font-size: 50px;color: red; text-shadow: 3px 3px 0 rgb(217,31,38) , 6px 6px 0 rgb(226,91,14) , 9px 9px 0 rgb(245,221,8) , 12px 12px 0 rgb(5,148,68) , 15px 15px 0 rgb(2,135,206) , 18px 18px 0 rgb(4,77,145) , 21px 21px 0 rgb(42,21,113)');
return true;
});
}
changeUI(){
//Will add functionality to inject a little UI for faster interaction with the extension
document.createElement()
}
}
class Gogoanime {
constructor() {
//Gets the title name directly from the div which has
this.name = null;
this.episode = null;
}
gogoanimeEpisode(str) {
let [animeName, strEp] = str.split("Episode");
const intEp = parseInt(strEp);
this.name = animeName;
this.episode = intEp;
return {
animeName,
intEp,
};
}
changeUI(){
//Will add functionality to inject a little UI for faster interaction with the extension
document.createElement()
}
}
//Checks whether the URL contains the site name
function containsURL(str) {
if (location.hostname.match(str)) {
return true;
} else if(location.href.match(str)){
return true;
}else{
return false;
}
}
if(containsURL("anilist.co/404#access_token")){
let a=new Anilist();
a.authenticate()
}else{
console.log(location.hostname)
}
//Not a good name but checks which site and returns the instance of the site's class wrapper
function checkSite() {
if (containsURL("animepahe.org") || containsURL("animepahe.com")) {
return new AnimePahe();
} else if (containsURL("gogoanime.ai")) {
try {
title = document.getElementsByClassName("title_name")[0].children[0].textContent;
console.log(title);
} catch (error) {
//the current active page is homepage
title=null;
}
return new Gogoanime();
}else{
//pass
}
}
//Returns the name based on the site
function toAnimeName(w) {
const instanceOfClass=checkSite();
if (instanceOfClass instanceof AnimePahe) {
let a = instanceOfClass.animepaheNameEpisode(w);
console.log(a.name + "| " + a.intEp);
return (a.name!==null?a.name:console.warn(title+" was not found"));
} else if (instanceOfClass instanceof Gogoanime) {
let a = instanceOfClass.gogoanimeEpisode(w);
console.log(a.animeName + "| " + a.intEp);
return (a.animeName!==null?a.animeName:console.warn(title+" was not found"));
} else {
//pass
}
}
function handleResponse(response) {
return response.json().then(function (json) {
return response.ok ? json : Promise.reject(json);
});
}
function handleData(data) {
let aboutAnime={
key: data.data.Media.description,
coverImage: data.data.Media.coverImage.large,
titleEnglish: data.data.Media.title.english,
latestAnime: data.data.Media.id
}
chrome.storage.sync.set(aboutAnime, function() {
console.log(aboutAnime);
});
return data;
}
function halwa(a){
return a
}
async function requestAnilist(query,variables) {
let url = 'https://graphql.anilist.co';
chrome.storage.sync.get(null,(a)=>{
if("auth_token" in a){
console.log("ahh yes");
console.log(a.auth_token);
options = {
method: "POST",
headers: {
'Authorization': 'Bearer ' + a.auth_token,
'Content-Type': 'application/json',
'Accept': 'application/json',
},body: JSON.stringify({
query: query,
variables: variables,
}),
};
}else{
options = {
method: "POST",
headers: {
"Content-Type": "application/json",
'Accept': "application/json",
},body: JSON.stringify({
query: query,
variables: variables,
}),
};
};
Promise.resolve(fetch(url, options)
.then(handleResponse)
.then(handleData)
.catch((error) => {
console.warn(error);
}));
}
)}
/*
Returns the Anime Info call stack is
getAnime()-> Initial call wrapping the other two functions
requestAnilist()-> Requests the Anilist for the data
chrome.storage.sync() Sets the data in the chrome storage
*/
async function getAnime(){
let variables = {
search: toAnimeName(title),
page: 1,
perPage: 1,
};
let query=` query ($id: Int, $search: String) {
Media (id: $id, search: $search, type: ANIME) {
id
duration
description(asHtml: false)
episodes
bannerImage
title {
romaji
english
native
}
coverImage {
large
}
}
}`
;
await requestAnilist(query,variables);
}
Promise.resolve(getAnime());
/*
Call Stack
getAnime()→toAnimeName()→checkSite()→containsURL()→Matches the URL with given input
| ↓
| Variables+Query(Static)
↓ ↓
↪requestAnilist()→returnHeader[inner function]()
↓
header+query(static)
↓
fetch()→ Store in Chrome.storage.sync();
*/