-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentscript.js
229 lines (192 loc) · 8.62 KB
/
contentscript.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
var rtClassName = 'Rotten-Tomatoes';
var rtClassNameSP = 'Rotten-Tomatoes-SP';
// On page load listener, when page fully loaded, execute mouse listener
window.addEventListener('load', (event) => {
// Mouse listener for any move event on the current document.
document.addEventListener('mousemove', function (e) {
let srcElement = e.srcElement;
// ================================ Function Definitions ===============================
// ------------------------------SLIDER CHECK----------------------------------------
function slider(cursorElement) {
var sliderNoVideoOrJunk = '_3RLSRf';
var sliderWithVideo = 'tst-video-overlay-player-html5';
var sliderFound = 'lAtJLC';
//console.log(cursorElement.className);
if (typeof(cursorElement) !== 'undefined'){
if(cursorElement.className == sliderWithVideo){
//console.log(cursorElement.parentElement.nextSibling.className);
if((cursorElement.parentElement.nextSibling.className == sliderFound)
&& cursorElement.parentElement.nextSibling.lastChild.className != rtClassName){
return cursorElement.parentElement.nextSibling;
}
}
else if((cursorElement.className == sliderNoVideoOrJunk) &&
(cursorElement.parentElement.parentElement.parentElement.parentElement.nextSibling)){
//console.log(cursorElement.parentElement.parentElement.parentElement.parentElement.nextSibling);
if((cursorElement.parentElement.parentElement.parentElement.parentElement.nextSibling.className == sliderFound)
&& cursorElement.parentElement.parentElement.parentElement.parentElement.nextSibling.lastChild.className != rtClassName){
return cursorElement.parentElement.parentElement.parentElement.parentElement.nextSibling;
}
}
else{
return false;
}
}
else{
return false;
}
}
// ------------------------------SLIDER ON SINGLE PAGE CHECK----------------------------------------
function sliderOnSinglePage(cursorElement){
//console.log(cursorElement);
var sliderOnPage = 'Zk8aEm _1EcvsB _1Asy_o';
var sliderFound = 'lAtJLC';
if (typeof(cursorElement) !== 'undefined'){
if((cursorElement.className == sliderOnPage) &&
(cursorElement.parentElement.parentElement.parentElement.parentElement.nextSibling)){
if((cursorElement.parentElement.parentElement.parentElement.parentElement.parentElement.nextSibling.className == sliderFound)
&& cursorElement.parentElement.parentElement.parentElement.parentElement.parentElement.nextSibling.lastChild.className != rtClassName){
return cursorElement.parentElement.parentElement.parentElement.parentElement.parentElement.nextSibling;
}
}
else{
return false;
}
}
else{
return false;
}
}
// ------------------------------SINGLE PAGE CHECK----------------------------------------
function singlePage(){
if(document.getElementsByClassName("Rotten-Tomatoes-SP")[0] == null){
if(document.getElementsByClassName('av-detail-section')[0]){
return document.getElementsByClassName('_3QwtCH _16AW_S _2LF_6p dv-node-dp-badges _3vxMzE HaWow5')[0];
}
else
return false;
}
}
// --------------------APPEND ROTTEN TOMATOES SPAN/DIV - (LOADING) --------------------------
function appendRottenTomatoes(AZparentClass,nameOfSpan){
//console.log(AZparentClass)
//console.log(nameOfSpan)
// -- Creates the RT span
var RTspan = document.createElement('span');
RTspan.className = nameOfSpan;
AZLastChild = AZparentClass.lastChild;
// -- Inserts the new rotten tomatoes class before the last child element in the amazon parent class
AZparentClass.insertBefore(RTspan,AZLastChild.nextSibling);
// -- Creates a loading image
var loading = document.createElement("img");
loading.src = chrome.extension.getURL("images/loading.gif");
loading.className = "loading";
// -- Appends loading.gif to the RTspan
if(nameOfSpan == 'Rotten-Tomatoes'){
AZparentClass.getElementsByClassName("Rotten-Tomatoes")[0].appendChild(loading);
}
else if(nameOfSpan == 'Rotten-Tomatoes-SP'){
AZparentClass.getElementsByClassName("Rotten-Tomatoes-SP")[0].appendChild(loading);
}
return RTspan;
}
// --------------- SEARCHES FOR CORRECT SEARCH TERMS FOR SLIDER ELEMENT ---------------------
function sliderGoogleUrl(sliderParentElement){
// Grabs the year the media was made
if(sliderParentElement.getElementsByClassName('nPUwP8 _2Z_R2D')[0]){
var titleYear = sliderParentElement.getElementsByClassName('nPUwP8 _2Z_R2D')[0].innerText;
if(titleYear.includes("min")){
titleYear = sliderParentElement.getElementsByClassName('nPUwP8 _2Z_R2D')[1].innerText;
}
}
//console.log(titleYear);
// Grabs the title of the movie / tvshow from the amazon video DOM
var titleOfMedia = sliderParentElement.getElementsByClassName('_2MiS8F tst-hover-title')[0].innerText;
//console.log(titleOfMedia)
// Formats and appends data to create google search
var formattedTitle = titleOfMedia.split(' ').join('%20');
url = 'https://google.com/search?q=Rotten%20Tomatoes%20' + formattedTitle + '%20' + '(' + titleYear + ')' + '%20';
//console.log(url)
return url;
}
// --------------- SEARCHES FOR CORRECT SEARCH TERMS FOR SINGLE PAGE ---------------------
function singlePageGoogleUrl(){
// Grabs the year the media was made
var titleYear = document.querySelector('[data-automation-id="release-year-badge"]').innerText;
// Grabs the title of the movie / tvshow from the amazon video DOM
var titleOfMedia = document.getElementsByClassName('_2IIDsE _3I-nQy')[0].innerText;
// Formats and appends data to create google search
var formattedTitle = titleOfMedia.split(' ').join('%20');
url = 'https://google.com/search?q=Rotten%20Tomatoes%20' + formattedTitle + '%20' + '(' + titleYear + ')' + '%20';
//console.log(url)
return url;
}
// ----------- SENDS CORRECT GOOGLE SEARCH URL TO BACKGROUND.JS FOR FETCH ----------------
function search(url, RTspan, RTParentElem){
chrome.runtime.sendMessage(
url,
function (response) {
//console.log('correct info + hyperlinked returned');
append(response, RTspan, RTParentElem);
}
);
}
// ------- APPENDS CORRECT ROTTEN TOMATO INFO TO RTSPAN AND REMOVES LOADING.GIF ----------
function append(RTinfo, RTspan, RTParentElem){
// Creates a new span, ...
var ratingSpan = document.createElement("span");
var textnode = document.createElement("p");
textnode.innerHTML = RTinfo + ' ';
ratingSpan.appendChild(textnode);
// Creates a new image element, grabs url, and appends it to the RTspan
var popcornImage = document.createElement("img");
popcornImage.src = chrome.extension.getURL("images/popimage.png");
// Appends Popcorn image depending on if element is on single page / slider
if(RTParentElem.getElementsByClassName("Rotten-Tomatoes")[0]){
RTParentElem.getElementsByClassName("Rotten-Tomatoes")[0].appendChild(popcornImage);
}
else{
RTParentElem.getElementsByClassName("Rotten-Tomatoes-SP")[0].appendChild(popcornImage);
}
// Styling for the entire RTspan class
RTspan.style.display = "flex";
// Styling for the ratingSpan class
ratingSpan.setAttribute("style","padding-top:8px; padding-left:4px;");
// Finds loading image in RTspan
loading = RTspan.getElementsByClassName("loading")[0];
// .. and removes the loading image from the new RTspan
RTspan.removeChild(loading);
// Appends data to the RTspan
if(RTParentElem.getElementsByClassName("Rotten-Tomatoes")[0]){
RTParentElem.getElementsByClassName("Rotten-Tomatoes")[0].appendChild(ratingSpan);
}
else{
RTParentElem.getElementsByClassName("Rotten-Tomatoes-SP")[0].appendChild(ratingSpan);
}
}
// ====================== End of Function Definitions / Program Start =========================
//console.log (srcElement)
var x = slider(srcElement); // Searches for slider element
var y = sliderOnSinglePage(srcElement); // Searches for slider on single page
var z = singlePage(); // Searches for single page
if(x){
//console.log("Moe - 1");
var RTspan = appendRottenTomatoes(x,rtClassName); // Appends to slider element
url = sliderGoogleUrl(x);
//console.log(url);
search(url, RTspan, x);
}
else if(y){
//console.log("Curly - 2");
var RTspan = appendRottenTomatoes(y,rtClassName); // Appends to slider on single page
url = sliderGoogleUrl(y);
search(url, RTspan, y);
}
else if(z){
//console.log("Larry - 3");
var RTspan = appendRottenTomatoes(z,rtClassNameSP); // Appends to single page
url = singlePageGoogleUrl();
search(url, RTspan, z);
}
}, false);
});