-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
55 lines (47 loc) · 2.07 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
/**
KIT Ilias OpenCast Downloader - a browser extension to simplify downloading videos from the KIT Ilias system
Copyright (C) 2020-present Alexander Linder
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/SeineEloquenz/kit-ilias-opencast-downloader
*/
let urls = new Map();
//Remove download urls when tabs are closed
chrome.tabs.onRemoved.addListener((tabId) => urls.delete(tabId));
//Toggle our buttons and manage download urls
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request['name'] === 'recording') {
let videoUrl = request['videoUrl'];
let title = request['title']
showPageAction(sender.tab.id);
if (!urls.has(sender.tab.id)) {
chrome.pageAction.onClicked.addListener((tab) => download(tab.id, sender.tab.id, title));
}
urls.set(sender.tab.id, videoUrl);
} else if (request['name'] === 'no-recording') {
hidePageAction(sender.tab.id);
urls.delete(sender.tab.id);
}
});
//Download file if on the correct tab
function download(tabId, wantedTabId, title) {
if (tabId === wantedTabId) {
chrome.downloads.download({url: urls.get(tabId), filename: title.replace(/[/\\?%*:|"<>]/g, '-')});
}
}
function showPageAction(tabId) {
chrome.pageAction.show(tabId);
chrome.pageAction.setIcon({tabId: tabId, path: 'icon128_green.png'});
}
function hidePageAction(tabId) {
chrome.pageAction.hide(tabId);
chrome.pageAction.setIcon({tabId: tabId, path: 'icon128.png'});
}