-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
109 lines (92 loc) · 4.09 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
var urlRegex = /\/(RDProcessAction\/ProcessActionEdit|RDProcessByUser\/ProcessEdit|ProcessByOffice\/ProcessEdit|ProcessAction\/ProcessActionEdit)/;
var urlRegexETDR = /\/(.*etdr.gov.hu|localhost:59057)/;
// When the browser-action button is clicked...
chrome.browserAction.onClicked.addListener(function (tab) {
// ...check the URL of the active tab against our pattern and...
if (urlRegex.test(tab.url)) {
// ...if it matches, send a message specifying a callback to do the download
chrome.tabs.sendMessage(tab.id, { text: 'report_back' }, dLoad);
} else if (!urlRegex.test(tab.url) && urlRegexETDR.test(tab.url)) {
// ...if not on the required page, then notify the user, that the download isn't available
chrome.tabs.sendMessage(tab.id, { text: 'download_not_available' });
}
});
// A function to use as callback
async function dLoad(jsonData) {
var infos = JSON.parse(jsonData);
// Set the folder name
var downloadFolder = '# Letöltött ÉTDR dokumentumok/';
var downloadPrefix = infos.processNumber === ""
? downloadFolder + currentDateTimeAsFolderName()
: `${downloadFolder}${infos.processNumber.toString().replace("/", "_")}_${currentDateTimeAsFolderName()}`;
// Iterate through elements and start the download
for (var i = 0; i < infos.docList.length; i++) {
await dLoadJob(infos.docList[i][1], downloadPrefix + infos.docList[i][0]);
}
// Download method
async function dLoadJob(url, fileName) {
// If the filename is specified, the saveAs parameter won't work if the user had set the saveas function previously in the browser settings
var downloading = await chrome.downloads.download({
url: url,
filename: fileName,
saveAs: false,
conflictAction: 'uniquify'
});
}
// Get the local storage to determine if a new install or an update occured
var gettingItem = chrome.storage.local.get(['ETDR_ExtVersion', 'ETDR_ShowChangeLog'], function (res) {
detectVersionChange(res.ETDR_ExtVersion, res.ETDR_ShowChangeLog);
});
}
// When a new install or an update occured, show a changelog page to the user
function detectVersionChange(storedVersion, showChangelog) {
// Get the extensions actual version from the manifest file
var extVersion = chrome.runtime.getManifest().version.replace(/\./g, '');
// Check if the extension's version is stored in the local storage and if it is older than the current one. If yes, then store it and set to show the changelog.
if (storedVersion === undefined || storedVersion < extVersion) {
storeCurrentVersion(extVersion);
storeShowChangelog(true);
showChangelog = true;
}
// If the showing the changelog window isn't stored in the local storage, then store it and set it to show
if (showChangelog === undefined) {
storeShowChangelog(true);
showChangelog = true;
}
// Show the changelog if necessary and store that next time the changelog window won't be displayed
if (showChangelog) {
openChangelog();
storeShowChangelog(false);
}
}
// Changelog window show value store
function storeShowChangelog(value) {
chrome.storage.local.set({
ETDR_ShowChangeLog: value
});
}
// Current extension's version store
function storeCurrentVersion(value) {
chrome.storage.local.set({
ETDR_ExtVersion: value
});
}
function openChangelog() {
let page = {
type: 'popup',
url: 'backgroundpage.html',
width: 800,
height: 400
};
let creating = chrome.windows.create(page);
}
function currentDateTimeAsFolderName() {
var dt = new Date();
var year = ('0' + dt.getFullYear().toString()).slice(-4);
var month = ('0' + (dt.getMonth() + 1).toString()).slice(-2);
var day = ('0' + dt.getDate().toString()).slice(-2);
var hour = ('0' + dt.getHours().toString()).slice(-2);
var minutes = ('0' + dt.getMinutes().toString()).slice(-2);
var seconds = ('0' + dt.getSeconds().toString()).slice(-2);
return `${year} ${month} ${day}_${hour + minutes + seconds}/`;
}