-
Notifications
You must be signed in to change notification settings - Fork 0
/
popularity.js
59 lines (52 loc) · 1.58 KB
/
popularity.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
let popularityStats = [];
function findObjectsWithKey(obj, key, result = []) {
if (obj instanceof Array) {
for (let i = 0; i < obj.length; i++) {
findObjectsWithKey(obj[i], key, result);
}
}
else if (obj instanceof Object) {
for (let prop in obj) {
if (prop == key) {
result.push(obj);
}
else if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
findObjectsWithKey(obj[prop], key, result);
}
}
}
return result;
}
function listener(details) {
// avoid scraping recommendations: only want the tracks in the album/playlist
if (details.url.includes("/recommendations/"))
return {};
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
filter.ondata = event => {
let str = decoder.decode(event.data, {stream: true});
try {
const responseJSON = JSON.parse(str);
const scraped = findObjectsWithKey(responseJSON, 'popularity');
popularityStats = scraped;
} catch (e) {
console.log('unparseable json', str);
console.log(e);
console.log(e.stack);
}
filter.write(encoder.encode(str));
filter.disconnect();
}
return {};
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: ["https://listen.tidal.com/v1/pages/*", "https://listen.tidal.com/v1/playlists/*/items*"]},
["blocking"]
);
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "getPopularityStats") {
sendResponse({ popularityStats });
}
});