-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
93 lines (83 loc) · 3.19 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
const url = chrome.runtime.getURL('config.json');
let api_key = "";
let past_title = "";
fetch(url)
.then((response) => response.json())
.then((data) => api_key = data["api_key"]);
$(document).bind('DOMSubtreeModified', function() {
const title_element_array = $('.title');
const title_element = title_element_array[title_element_array.length - 1];
let title = "";
try {
title = title_element.getElementsByTagName('img')[0] && title_element.getElementsByTagName('img')[0].alt;
}
catch {
return;
}
if (title === past_title){
return
}
past_title = title;
console.log(title);
injectData(getRatings(title));
});
function getRatings(title) {
let xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", `https://www.omdbapi.com/?apikey=${api_key}&t=${title}`, false );
xmlHttp.send( null );
data = JSON.parse(xmlHttp.response);
const imdb_rating = data.imdbRating;
const metescore = data.Metascore;
const rotten_tomatoes_rating = getRottenTomatoScore(data);
ratings = {
"IMDB": imdb_rating,
"Metascore": metescore,
"Rotten Tomatoes": rotten_tomatoes_rating
};
return ratings
}
function getRottenTomatoScore(data) {
const ratings_array = data.Ratings;
if (ratings_array.length > 1) {
if (ratings_array[1].Source == "Rotten Tomatoes") {
return parseFloat(ratings_array[1].Value);
}
}
return "N/A";
}
function injectData(ratings){
node_array = $('.video-meta ');
node = node_array[node_array.length - 1];
const imdb_span = document.createElement("SPAN");
const metascore_span = document.createElement("SPAN");
const rotten_tomatoes_span = document.createElement("SPAN");
const imdb_score = document.createTextNode(ratings["IMDB"].toString());
const metascore = document.createTextNode(ratings["Metascore"].toString());
const rotten_tomatoes = document.createTextNode(ratings["Rotten Tomatoes"].toString() + "%");
const imdb_logo = createLogo("https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/IMDb_logo.svg/1280px-IMDb_logo.svg.png");
const metascore_logo = createLogo("https://pbs.twimg.com/profile_images/527528131171590144/EQXs3lpX_400x400.png");
const rotten_tomatoes_logo = createLogo("https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Rotten_Tomatoes.svg/1009px-Rotten_Tomatoes.svg.png");
imdb_span.appendChild(imdb_score);
metascore_span.appendChild(metascore);
rotten_tomatoes_span.appendChild(rotten_tomatoes);
if (ratings["IMDB"].toString() !== "N/A"){
node.appendChild(imdb_logo);
node.appendChild(imdb_span);
}
if (ratings["Metascore"].toString() !== "N/A"){
node.appendChild(metascore_logo);
node.appendChild(metascore_span);
}
if (ratings["Rotten Tomatoes"].toString() !== "N/A") {
node.appendChild(rotten_tomatoes_logo);
node.appendChild(rotten_tomatoes_span);
}
}
function createLogo(logo_url){
const logo = document.createElement("IMG");
logo.setAttribute("width", "20");
logo.setAttribute("height", "20");
logo.setAttribute("src", logo_url);
logo.setAttribute("hspace", "2");
return logo;
}