-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackground.js
110 lines (100 loc) · 4.34 KB
/
background.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
importScripts('js/storage.js');
function encodeURIComponentSafe(value) {
return encodeURIComponent(value)
.replace(/!/g, '%21')
.replace(/\~/g, '%7E')
.replace(/\*/g, '%2A')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29');
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.contentScriptQuery === 'queryMedia') {
console.log(`Querying ${request.mediaType} '${request.tmdbId}'`);
pullStoredData(function() {
const options = {headers: {'X-Api-Key': serverAPIKey}};
fetch(`${origin}/api/v1/${request.mediaType}/${encodeURIComponent(request.tmdbId)}`, options)
.then(response => response.json())
.then(json => sendResponse(json))
.catch(error => console.error(error))
});
return true;
}
else if (request.contentScriptQuery === 'requestMedia') {
console.log(`Requesting media '${request.tmdbId}' of type '${request.mediaType}'`);
pullStoredData(function() {
const options = {
method: 'POST',
headers: {'Content-Type': 'application/json', 'X-Api-Key': serverAPIKey},
body: JSON.stringify({mediaType: request.mediaType, mediaId: request.tmdbId, seasons: request.seasons})
};
fetch(`${origin}/api/v1/request`, options)
.then(response => response.json())
.then(json => sendResponse(json))
.catch(error => console.error(error));
});
return true;
}
else if (request.contentScriptQuery === 'search') {
console.log(`Searching movie '${request.title}'`);
pullStoredData(function() {
const options = {headers: {'X-Api-Key': serverAPIKey}};
fetch(`${origin}/api/v1/search?query=${encodeURIComponentSafe(request.title)}`, options)
.then(response => response.json())
.then(json => sendResponse(json))
.catch(error => console.error(error));
});
return true;
}
else if (request.contentScriptQuery === 'plexQueryMedia') {
let mediaKey = encodeURIComponentSafe(request.mediaKey);
let plexToken = encodeURIComponentSafe(request.plexToken);
console.log(`Requesting Plex media '${mediaKey}'`);
const options = {headers: {'Accept': 'application/json'}};
fetch(`https://metadata.provider.plex.tv/library/metadata/${mediaKey}?X-Plex-Token=${plexToken}`, options)
.then(response => response.json())
.then(json => sendResponse(json))
.catch(error => console.error(error));
return true;
}
else if (request.contentScriptQuery === 'getOverseerrVersion') {
console.log(`Getting Overseerr version`);
pullStoredData(function() {
const options = {headers: {'X-Api-Key': serverAPIKey}};
fetch(`${origin}/api/v1/status`, options)
.then(response => response.json())
.then(json => sendResponse(json))
.catch(error => console.error(error));
});
return true;
}
else if (request.contentScriptQuery === 'checkJellyseerr') {
console.log(`Checking if instance is Jellyseerr`);
pullStoredData(function() {
const options = {headers: {'X-Api-Key': serverAPIKey, 'Accept': 'application/json'}};
fetch(`${origin}/api/v1/auth/me`, options)
.then(response => response.json())
.then(json => sendResponse(Object.keys(json).filter((key) => /jellyfin/.test(key)).length > 0))
.catch(error => console.error(error));
});
return true;
}
else if (request.contentScriptQuery === 'openOptionsPage') {
chrome.runtime.openOptionsPage();
return true;
}
else if (request.contentScriptQuery === 'listenForUrlChange') {
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
changeInfo.status === 'complete' &&
tab.status === 'complete' &&
tab.url &&
tab.url.startsWith('https://www.senscritique.com')
) {
chrome.tabs.sendMessage(tab.id, {
newUrl: tab.url
});
}
});
}
return false;
});