Skip to content

Commit 209129f

Browse files
committed
implmented Periodical CSV download as requested from #89d
1 parent 0d53673 commit 209129f

File tree

5 files changed

+184
-3
lines changed

5 files changed

+184
-3
lines changed

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"idle",
2424
"chrome://favicon/*",
2525
"webNavigation",
26-
"unlimitedStorage"
26+
"unlimitedStorage",
27+
"alarms"
2728
],
2829
"optional_permissions": [
2930
"https://www.youtube.com/*",

src/options.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@
9898
<div class="margin-top-10">
9999
<input type="button" value="Export to CSV" id='exportToCsv'>
100100
</div>
101+
<div class="margin-top-10">
102+
<label class="setting-header">Periodic CSV download:</label>
103+
</div>
104+
<div class="margin-top-10">
105+
<label for="hour">Hour:</label>
106+
<select select id="hour" name="hour" class="option time-selector">
107+
<option>select</option>
108+
</select>
109+
110+
<label for="minute">Minute:</label>
111+
<select id="minute" name="minute" class="option time-selector">
112+
<option>select</option>
113+
</select>
114+
<input
115+
type="button"
116+
value="Save"
117+
class="time-save-btn"
118+
id="time-save-btn"
119+
/>
120+
101121
<div class="separator"></div>
102122
<div class="margin-top-10">
103123
<input type="button" value="Backup" id='backup'>
@@ -113,6 +133,12 @@
113133
<div class="notify warning" id='notify-restore-failed' hidden>
114134
Backup file is not valid
115135
</div>
136+
<div class="notify warning" id="notify-periodic-save-failed" hidden>
137+
Please select hour and minute
138+
</div>
139+
<div class="notify " id="notify-periodic-saved" hidden>
140+
Saved!
141+
</div>
116142
<div class="separator"></div>
117143
<!-- YouTube -->
118144
<div class="margin-top-10">

src/scripts/background.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,76 @@ function checkPermissionsForNotifications(callback, ...props) {
399399
});
400400
}
401401

402+
function createFile(data, type, fileName) {
403+
var file = new Blob([data], { type: type });
404+
var downloadLink;
405+
downloadLink = document.createElement("a");
406+
downloadLink.download = fileName;
407+
downloadLink.href = window.URL.createObjectURL(file);
408+
downloadLink.style.display = "none";
409+
document.body.appendChild(downloadLink);
410+
downloadLink.click();
411+
}
412+
413+
function toCsv(tabsData) {
414+
var str = "domain,date,time(sec)\r\n";
415+
for (var i = 0; i < tabsData.length; i++) {
416+
for (var y = 0; y < tabsData[i].days.length; y++) {
417+
var line =
418+
tabsData[i].url +
419+
"," +
420+
new Date(tabsData[i].days[y].date).toLocaleDateString() +
421+
"," +
422+
tabsData[i].days[y].summary;
423+
str += line + "\r\n";
424+
}
425+
}
426+
427+
createFile(str, "text/csv", "domains.csv");
428+
}
429+
430+
function exportToCSV() {
431+
storage.getValue(STORAGE_TABS, function (item) {
432+
toCsv(item);
433+
});
434+
}
435+
436+
storage.getValue("SETTINGS_PERIODIC_HOUR_DOWNLOAD", function (hour) {
437+
storage.getValue("SETTINGS_PERIODIC_MINUTE_DOWNLOAD", function (minute) {
438+
if (typeof hour !== "number" || typeof minute !== "number") {
439+
return;
440+
} else {
441+
autoDownloadCsv(hour, minute);
442+
}
443+
});
444+
});
445+
446+
function autoDownloadCsv(hour, minute) {
447+
var now = new Date();
448+
var triggerTime = new Date(
449+
now.getFullYear(),
450+
now.getMonth(),
451+
now.getDate(),
452+
hour,
453+
minute,
454+
0,
455+
0
456+
);
457+
458+
if (triggerTime < now) {
459+
triggerTime.setDate(triggerTime.getDate() + 1);
460+
}
461+
chrome.alarms.create("periodic_Download", {
462+
when: triggerTime.getTime(),
463+
periodInMinutes: 1440, // 24 hours
464+
});
465+
chrome.alarms.onAlarm.addListener(function (alarm) {
466+
if (alarm.name === "periodic_Download") {
467+
exportToCSV();
468+
}
469+
});
470+
}
471+
402472
loadPermissions();
403473
addListener();
404474
loadAddDataFromStorage();

src/scripts/settings.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var restrictionList = [];
44
var notifyList = [];
55
var blockBtnList = ['settingsBtn', 'restrictionsBtn', 'notifyBtn', 'aboutBtn'];
66
var blockList = ['settingsBlock', 'restrictionsBlock', 'notifyBlock', 'aboutBlock'];
7+
var changed_Periodic_Download_Hour
8+
var changed_Periodic_Download_Minutes
79

810
document.addEventListener('DOMContentLoaded', function () {
911
document.getElementById('settingsBtn').addEventListener('click', function () {
@@ -72,6 +74,19 @@ document.addEventListener('DOMContentLoaded', function () {
7274
});
7375
$('.clockpicker').clockpicker();
7476

77+
document.getElementById("hour").addEventListener("change", function () {
78+
changed_Periodic_Download_Hour = this.value;
79+
});
80+
document.getElementById("minute").addEventListener("change", function () {
81+
changed_Periodic_Download_Minutes = this.value;
82+
});
83+
84+
document
85+
.getElementById("time-save-btn")
86+
.addEventListener("click", function () {
87+
saveAutoDownloadTime();
88+
});
89+
7590
loadSettings();
7691
});
7792

@@ -136,6 +151,8 @@ function loadSettings() {
136151
checkPermissionsForYT();
137152
checkPermissionsForNetflix();
138153
checkPermissionsForNotifications();
154+
createHourOptions()
155+
createMinutesOptions()
139156
}
140157

141158
function checkPermissionsForYT() {
@@ -531,4 +548,63 @@ function updateNotificationList() {
531548

532549
function updateNotificationMessage() {
533550
storage.saveValue(STORAGE_NOTIFICATION_MESSAGE, document.getElementById('notifyMessage').value);
534-
}
551+
}
552+
553+
function createHourOptions() {
554+
var select = document.querySelector("#hour");
555+
for (let i = 0; i <= 23; i++) {
556+
var option = document.createElement("option");
557+
option.value = i;
558+
option.text = i;
559+
select.appendChild(option);
560+
}
561+
storage.getValue("SETTINGS_PERIODIC_HOUR_DOWNLOAD", function (item) {
562+
if (item >= 0) {
563+
document.getElementById("hour").value = item;
564+
}
565+
});
566+
}
567+
568+
function createMinutesOptions() {
569+
var select = document.querySelector("#minute");
570+
for (var i = 0; i <= 59; i++) {
571+
var option = document.createElement("option");
572+
option.value = i;
573+
option.text = i;
574+
select.appendChild(option);
575+
}
576+
storage.getValue("SETTINGS_PERIODIC_MINUTE_DOWNLOAD", function (item) {
577+
if (item >= 0) {
578+
document.getElementById("minute").value = item;
579+
}
580+
});
581+
}
582+
583+
function saveAutoDownloadTime() {
584+
var hour = changed_Periodic_Download_Hour;
585+
var minute = changed_Periodic_Download_Minutes;
586+
587+
var _eminut = document.getElementById("minute").value;
588+
var _ehour = document.getElementById("hour").value;
589+
console.log(minute, hour, _ehour, _eminut);
590+
if (
591+
(Number(hour) >= 0 || Number(_ehour) >= 0) &&
592+
(Number(minute) >= 0 || Number(_eminut) >= 0)
593+
) {
594+
storage.saveValue(
595+
"SETTINGS_PERIODIC_MINUTE_DOWNLOAD",
596+
Number(_eminut) || Number(minute)
597+
);
598+
storage.saveValue(
599+
"SETTINGS_PERIODIC_HOUR_DOWNLOAD",
600+
Number(_ehour) || Number(hour)
601+
);
602+
viewNotify("notify-periodic-saved");
603+
} else if (hour === "select" || minute === "select") {
604+
viewNotify("notify-periodic-save-failed");
605+
storage.saveValue("SETTINGS_PERIODIC_MINUTE_DOWNLOAD", "select");
606+
storage.saveValue("SETTINGS_PERIODIC_HOUR_DOWNLOAD", "select");
607+
}
608+
609+
610+
}

src/style/settings.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,12 @@ input[type="button"]:hover {
466466

467467
.separator{
468468
margin: 30px 0;
469-
}
469+
}
470+
471+
.time-save-btn{
472+
max-width: 20px !important;
473+
margin-left: 5px !important;
474+
}
475+
.time-selector{
476+
min-width: 10px !important;
477+
}

0 commit comments

Comments
 (0)