-
Notifications
You must be signed in to change notification settings - Fork 0
/
netflix_ratings_setup.js
61 lines (56 loc) · 2.1 KB
/
netflix_ratings_setup.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
async function fetchJSON(url) {
const response = await fetch(url);
return response.status;
}
function saveSettings() {
let tmdbApiKey = document.getElementById('tmdbApiKey').value
let omdbApiKey = document.getElementById('omdbApiKey').value
let status = document.getElementById('status');
chrome.storage.sync.set({
netflixMediaRatingsTmdbApiKey: tmdbApiKey,
netflixMediaRatingsOmdbApiKey: omdbApiKey
}, async function() {
let errors = []
let omdbUrl = `https://www.omdbapi.com/?t=foo&apikey=${omdbApiKey}`
let omdbStatus = await fetchJSON(omdbUrl)
if (omdbStatus !== 200) errors.push("Invalid OMDB API key")
let tmdbUrl = `https://api.themoviedb.org/3/configuration?api_key=${tmdbApiKey}`
let tmdbStatus = await fetchJSON(tmdbUrl)
if (tmdbStatus !== 200) errors.push("Invalid TMDB API key")
if (errors.length !== 0){
status.textContent = errors.join(", ")
status.className = "error"
chrome.runtime.sendMessage({
action: 'setDisabledIcon',
value: true
});
return
}
// Update status to let user know options were saved.
status.textContent = 'API keys saved.';
status.className = "success"
chrome.runtime.sendMessage({
action: 'setDisabledIcon',
value: false
});
setTimeout(
function() {
status.textContent = '';
},
1000
);
});
}
function restoreSettings() {
chrome.storage.sync.get(
["netflixMediaRatingsTmdbApiKey", "netflixMediaRatingsOmdbApiKey"],
function(items) {
if (items.netflixMediaRatingsTmdbApiKey !== undefined){
document.getElementById('tmdbApiKey').value = items.netflixMediaRatingsTmdbApiKey;
document.getElementById('omdbApiKey').value = items.netflixMediaRatingsOmdbApiKey;
}
}
);
}
document.addEventListener('DOMContentLoaded', restoreSettings);
document.getElementById('save').addEventListener('click', saveSettings);