-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
256 lines (220 loc) · 9.81 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
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
const bearerTokenLabel = document.getElementById('bearerTokenLabel');
const bearerTokenInput = document.getElementById('bearerTokenInput');
// Find the table with class "card credits"
var creditsTable = document.querySelector('table.card.credits');
if (creditsTable) {
// Function to retrieve the API key from the background script
function getApiKey() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ type: 'getApiKey' }, (apiKey) => {
if (apiKey) {
resolve(apiKey);
} else {
resolve(null);
}
});
});
}
// Function to retrieve the bearer token from the background script
function getBearerToken() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ type: 'getBearerToken' }, (bearerToken) => {
if (bearerToken) {
resolve(bearerToken);
} else {
resolve(null);
}
});
});
}
// Main function to handle the logic
async function main() {
// Get the API key from storage
const apiKey = await getApiKey();
if (apiKey) {
// API key exists, continue with your logic using the key
console.log('API key:', apiKey);
} else {
// API key doesn't exist, open the extension's popup for API key input
chrome.runtime.sendMessage({ type: 'promptApiKey' });
}
// Get the bearer token from storage
const bearerToken = await getBearerToken();
if (bearerToken) {
// Bearer token exists, continue with your logic using the token
console.log('Bearer token:', bearerToken);
}
else {
// Bearer token doesn't exist, open the extension's popup for bearer token input
chrome.runtime.sendMessage({ type: 'promptBearerToken' });
}
}
// Find all tables with class "credit_group" within the creditsTable
var creditTables = creditsTable.querySelectorAll('table.credit_group');
// Iterate over each table
creditTables.forEach(async function (table) {
// Find all table rows in the current table
var tableBodies = table.querySelectorAll('tbody');
tableBodies.forEach(async function (tableRow) {
var tableRows = tableRow.querySelectorAll('tr');
// Iterate over each table row
tableRows.forEach(async function (row) {
row.addEventListener('mouseenter', function () {
row.style.boxShadow = '0 4px 30px rgba(0, 0, 0, 0.1)';
row.style.transition = 'all .7s ease-in-out';
});
row.addEventListener('mouseleave', function () {
row.style.boxShadow = 'none';
row.style.transform = 'none';
row.style.transition = 'all .7s ease-in-out';
});
var tdRightElements = row.querySelectorAll('td.role.true.account_adult_false.item_adult_false');
tdRightElements.forEach(function (element) {
element.style.borderRadius = '0 12px 12px 0';
element.style.background = "rgba( 255, 255, 255, 0.25 )";
element.style.backdropFilter = "blur(5px)";
element.style.webkitBackdropFilter = "blur(5px)";
element.style.border = '1px rgba(3, 37, 65, 0.3)';
element.style.borderStyle = 'solid solid solid none';
});
var tdLeftElements = row.querySelectorAll('td.year');
tdLeftElements.forEach(function (element) {
element.style.background = "rgba( 255, 255, 255, 0.25 )";
element.style.borderRadius = '12px 0 0 12px';
element.style.backdropFilter = "blur(5px)";
element.style.webkitBackdropFilter = "blur(5px)";
element.style.border = '1px rgba(3, 37, 65, 0.3)';
element.style.borderStyle = 'solid none solid solid';
});
var tdCenterElements = row.querySelectorAll('td.seperator');
tdCenterElements.forEach(function (element) {
element.style.background = "rgba( 255, 255, 255, 0.25 )";
element.style.backdropFilter = "blur(5px)";
element.style.webkitBackdropFilter = "blur(5px)";
element.style.border = '1px rgba(3, 37, 65, 0.3)';
element.style.borderStyle = 'solid none';
});
// Create and append the <hr> element
var hrElement = document.createElement('hr');
hrElement.style.border = '0.5px solid white';
row.insertAdjacentElement('afterend', hrElement);
// Find all elements within the current row
var elements = row.querySelectorAll('*');
// Apply the white color to the text of each element
elements.forEach(function (element) {
// element.style.color = '#fff';
});
// Find the necessary data from the current row
var yearElement = row.querySelector('.year');
var movieUrlElement = row.querySelector('.tooltip');
var movieTitleElement = row.querySelector('.tooltip bdi');
var charactertextElement = row.querySelector('.group');
var characterElement = row.querySelector('.character');
var spanElement = row.querySelector('.glyphicons_v2.circle-empty.account_adult_false.item_adult_false');
spanElement.style.backgroundColor = '#fff';
spanElement.style.borderRadius = '50%';
// Check if all required elements exist
// var year = yearElement.textContent;
var movieUrl = movieUrlElement.getAttribute('href');
var movieTitle = movieTitleElement.textContent;
// var character = characterElement.textContent;
// Function to get the poster URL for a movie ID
async function getMoviePoster(movieId) {
const apiKey = await getApiKey();
const url = `https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiKey}`;
try {
const response = await fetch(url);
const data = await response.json();
// Check if the poster path exists
if (data.poster_path) {
// Extract the poster URL from the data
const posterUrl = `https://image.tmdb.org/t/p/w130_and_h195_bestv2${data.poster_path}`;
return posterUrl;
} else {
return null;
}
} catch (error) {
console.error('Error:', error);
return null;
}
}
// Function to get the rating for a movie ID
async function getMovieRating(movieId) {
const apiKey = await getApiKey();
const url = `https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiKey}`;
try {
const response = await fetch(url);
const data = await response.json();
// Check if the rating exists
if (data.vote_average) {
// Extract the rating from the data
const rating = data.vote_average;
return rating;
} else {
return null;
}
} catch (error) {
console.error('Error:', error);
return null;
}
}
// Check if movieUrl is not null or empty before splitting
if (movieUrl) {
// Get the movie ID from the movie URL
const movieId = movieUrl.split('/').pop();
// Fetch the poster URL and rating for the movie ID
const posterUrl = await getMoviePoster(movieId);
const rating = await getMovieRating(movieId);
// Create a new HTML element with the provided code if posterUrl exists
if (posterUrl) {
// Create a new tooltip container
var tooltipContainer = document.createElement('div');
tooltipContainer.className = 'tooltip-container';
// Create a new div for the movie details
var movieDetails = document.createElement('div');
movieDetails.className = 'movie-details';
// Create a new anchor element for the movie URL
var anchorElement = document.createElement('a');
anchorElement.href = movieUrl;
// Create a new img element for the poster image
var imgElement = document.createElement('img');
imgElement.className = 'poster';
imgElement.src = posterUrl;
imgElement.alt = movieTitle;
imgElement.width = 130;
imgElement.height = 195;
imgElement.style.borderRadius = '7%';
imgElement.style.marginTop = '5px';
// Append the img element and rating element to the anchor element
anchorElement.appendChild(imgElement);
if (rating) {
// Create a new span element for the rating
var ratingElement = document.createElement('span');
ratingElement.className = 'rating';
var starElement = document.createElement('span');
starElement.className = 'star';
starElement.textContent = '★ ';
ratingElement.textContent = starElement.textContent + rating;
ratingElement.style.float = 'right';
// Apply the CSS styles to the rating element
ratingElement.style.backgroundColor = '#01B4E4';
ratingElement.style.color = '#fff';
ratingElement.style.padding = '5px';
ratingElement.style.fontSize = '14.4px';
ratingElement.style.fontFamily = 'Source Sans Pro, Arial, sans-serif';
ratingElement.style.fontWeight = 'bold';
ratingElement.style.borderRadius = '3px';
ratingElement.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.2)';
// Append the rating element to the anchor element
anchorElement.appendChild(ratingElement);
}
// Find the necessary <td> element
var tdElement = row.querySelector('td.role.true.account_adult_false.item_adult_false');
// Append the anchor element to the <td> element
tdElement.appendChild(anchorElement);
}
}
});
});
});
}