-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
165 lines (146 loc) · 4.23 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
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
const browser = require("webextension-polyfill");
const path = require("path");
const ID3Writer = require("browser-id3-writer");
let state = {
album: {},
tabId: null,
intervalId: "",
downloads: {},
};
function onStartedDownload(id) {
state.downloads[id] = true;
console.log(`Started downloading: ${id}`);
}
function onFailed(error) {
state.downloads[Math.random().toString()] = false;
clearInterval(state.intervalId);
const { tabId } = state;
browser.pageAction.setIcon({ tabId, path: `./icons/camper-died.png` });
console.log(`Download failed: ${error.message}`);
}
function sanitize(str) {
return str.replaceAll(/[\\\/$'"]/g, "").replaceAll(":", "");
}
function loadFile(url, processResponse) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
if (xhr.status === 200) {
processResponse(xhr.response);
} else {
onFailed(xhr.statusText + " (" + xhr.status + ")");
}
};
xhr.onerror = function () {
onFailed("Network error");
};
xhr.send();
}
// bugs out on second download?
function animateProgressIcon() {
const { tabId, downloads } = state;
const frames = [0, 1, 2, 1];
let index = 0;
setActiveIcon(index);
state.intervalId = setInterval(() => {
index++;
console.log(index);
setActiveIcon(frames[index % 4]);
}, 250);
}
function setActiveIcon(frame = 1) {
const { tabId } = state;
browser.pageAction.setIcon({
tabId,
path: `./icons/camper-active-${frame}.png`,
});
console.log("frame", frame, Date.now());
}
function downloadOnChanged(event) {
const { id, state: { current } = {} } = event;
state.downloads[id] = current === "complete";
if (
Object.keys(state.downloads).length === state.album.tracks.length + 1 &&
Object.values(state.downloads).every((download) => download)
) {
onComplete();
}
}
function onComplete() {
clearInterval(state.intervalId);
const setActiveFrame = setTimeout(setActiveIcon, 250);
browser.downloads.onChanged.removeListener(downloadOnChanged);
state.downloads = {};
}
function download(message, sender) {
const {
album: { tracks, cover, folder, artist, date, title: albumTitle },
} = state;
animateProgressIcon();
browser.downloads.onChanged.addListener(downloadOnChanged);
loadFile(cover, (coverData) => {
tracks.forEach(({ track_num: number, title, file: { "mp3-128": url } }) => {
const filename = `${sanitize(folder)}/${sanitize(title)}.mp3`;
loadFile(url, (trackData) => {
const writer = new ID3Writer(trackData);
writer
.setFrame("TIT2", title)
.setFrame("TPE2", artist)
.setFrame("TALB", albumTitle)
.setFrame("TRCK", number)
.setFrame("TYER", date)
.setFrame("APIC", {
type: 3,
data: coverData,
description: "Super picture",
});
writer.addTag();
taggedUrl = writer.getURL();
const downloading = browser.downloads.download({
filename,
url: taggedUrl,
});
console.log(filename);
downloading.then(onStartedDownload, onFailed);
});
});
});
const downloading = browser.downloads.download({
filename: `${sanitize(folder)}/cover.jpg`,
url: cover,
});
downloading.then(onStartedDownload, onFailed);
return true;
}
function onReceiveAlbum(album) {
if (Object.keys(state.album).length === 0) {
state.album = album;
browser.pageAction.show(state.tabId);
browser.pageAction.onClicked.addListener(download);
setActiveIcon();
}
return true;
}
browser.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
console.log("yo sup");
if (
changeInfo.status === "complete" &&
tab.url.match(/https\:\/\/.*\.bandcamp\.com/)
) {
console.log("aw yeah");
state.album = {};
state.tabId = tabId;
browser.pageAction.onClicked.removeListener(download);
browser.runtime.onMessage.addListener(onReceiveAlbum);
browser.tabs.executeScript({
file: path.resolve(
__dirname,
"node_modules/webextension-polyfill/dist/browser-polyfill.min.js"
),
});
browser.tabs.executeScript({
file: path.resolve(__dirname, "./dist/content.js"),
});
}
});