-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
63 lines (53 loc) · 1.71 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
console.log('running background.js for the Musician extension');
let autoDownload;
let isConfig = window.localStorage.getItem("config");
if (!!isConfig) {
autoDownload = JSON.parse(isConfig);
console.log(autoDownload)
} else {
window.localStorage.setItem("config", JSON.stringify({ "autoDownload": false, "disableShelf": false }));
}
chrome.tabs.query({ url: "*://*.jiosaavn.com/*" }, function callback(tabs) {
for (let tab of tabs) {
// chrome.tabs.executeScript(tab.id, { file: "inject.js" });
}
});
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if ('meta' in request) {
config = JSON.parse(window.localStorage.getItem("config"));
if (config) {
if (config.autoDownload === true) {
createDownloadIfNotExist(request, sendResponse);
} else {
console.log(config);
sendResponse(` Auto save disabled -> musician/${request.meta.album}/${request.meta.title}.mp3`);
}
}
} else {
chrome.downloads.setShelfEnabled(!request.data);
}
}
);
function createDownloadIfNotExist(request, sendResponse) {
filename = `musician/${request.meta.album}/${request.meta.title}.mp3`;
list = JSON.parse(window.localStorage.getItem("list"));
if (!list) {
list = {
songs: []
};
}
songExist = list.songs.find((song) => song === filename);
if (songExist) {
sendResponse(` Already Exist -> musician/${request.meta.album}/${request.meta.title}.mp3`);
} else {
list.songs.push(filename);
window.localStorage.setItem('list', JSON.stringify({ "songs": list.songs }));
chrome.downloads.download({
url: request.source,
filename,
conflictAction: 'overwrite'
});
sendResponse(` saved to -> musician/${request.meta.album}/${request.meta.title}.mp3`);
}
}