-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventPage.js
79 lines (66 loc) · 2.61 KB
/
eventPage.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
var thisId = chrome.runtime.id;
var basePath = 'blob:chrome-extension://' + thisId;
var openWindows = {};
var openDownloads = {};
chrome.downloads.onDeterminingFilename.addListener(function(downloadItem, suggest) {
if (downloadItem.url.startsWith(basePath)) {
suggest({filename: 'urls.txt'});
} else {
// this isn't our download
suggest();
}
return true;
});
chrome.downloads.onCreated.addListener(function(downloadItem) {
// console.log(downloadItem);
if (!downloadItem.url.startsWith(basePath)) {
console.log('this isnt our download');
return;
}
openDownloads[downloadItem.id] = downloadItem.url;
});
chrome.downloads.onChanged.addListener(function(downloadItem) {
// console.log(downloadItem);
// downloadItem only returns filename and id, nothing else
if (openDownloads[downloadItem.id]) {
// this is one of our downloads
var url = openDownloads[downloadItem.id];
var windowId = openWindows[url];
// Delete the keys from the object
delete openDownloads[downloadItem.id];
delete openWindows[url];
// Since our download is started, close the window
chrome.windows.remove(windowId);
window.URL.revokeObjectURL(url);
}
});
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.task == 'openTabs') {
// Runs in the background, opening tabs for each URL in array
for (i = 0; i < msg.arr.length; i++) {
// chrome.tabs.create({url: msg.arr[i]});
chrome.tabs.create({ url: 'data:text/html,<title>' + msg.arr[i] });
};
} else if (msg.task == 'saveFile') {
// Workaround to download a .txt file containing the list of URLs
var file = new Blob([JSON.stringify(msg.arr)], {type: 'plain/text'});
var url = window.URL.createObjectURL(file);
var createData = {url: url};
chrome.windows.create(createData, function (newWindow) {
openWindows[url] = newWindow.id;
});
}
});
// https://stackoverflow.com/questions/67009241/google-chrome-extension-how-to-open-a-new-tab-in-a-discarded-hibernating-state
// When the new tab is selected, get the link in the title and load the page
chrome.tabs.onActivated.addListener(({ tabId, windowId }) => {
const splitText = 'data:text/html,<title>';
chrome.tabs.get(tabId, tab => {
if (tab.url.startsWith(splitText)) {
const realTabUrl = tab.url.replace(splitText, '');
chrome.tabs.update(tabId, {
url: realTabUrl,
});
}
});
});