From 704f29394c73f59a19b333b9d15293c23fa20116 Mon Sep 17 00:00:00 2001 From: Dilan Sachintha Nayanajith Date: Sun, 13 May 2018 11:50:57 +0530 Subject: [PATCH] Added reminders Added reminders, now when we schedule the reminder, it will notify us and if an url was saved, it will auto reload a tab with the saved url, since firefox doesn't let loading urls from javascript, it gives an error --- background.js | 23 +++++++++++++++++++++++ manifest.json | 2 +- script.js | 29 ++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/background.js b/background.js index 99cd013..ca3be85 100644 --- a/background.js +++ b/background.js @@ -1 +1,24 @@ browser.storage.sync.set({todos:[]}); +browser.alarms.onAlarm.addListener(listen); + +function listen(alarm){ + console.log(alarm); + var prom = browser.storage.sync.get(null); + prom.then((res) => { + console.log(res.todos[parseInt(alarm.name)]); + browser.notifications.create('notify', { + "type": "basic", + "iconUrl": 'icons/clock.jpg', + "title": "Todo4U Notifier", + "message": res.todos[parseInt(alarm.name)] + }); + browser.notifications.onClicked.addListener(function(notificationId) { + console.log('Notification ' + notificationId + ' was clicked by the user'); + if(res.todos[parseInt(alarm.name)]+3!=''){ + browser.tabs.create({ + url:res.todos[parseInt(alarm.name)]+3 + }); + } + }); + }); +} diff --git a/manifest.json b/manifest.json index f7ab2d5..f67aa02 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "icons": { "48":"icons/clock.jpg" }, - "permissions":["storage","contextMenus","notifications","tabs"], + "permissions":["storage","contextMenus","notifications","tabs","alarms"], "background":{ "scripts":["background.js"] }, diff --git a/script.js b/script.js index 8a108cc..0484949 100644 --- a/script.js +++ b/script.js @@ -24,7 +24,6 @@ function viewPrevious(){ document.getElementById('myBtn').addEventListener('click',function(){ if(document.getElementById('todoData').value!=""){ var prom = browser.storage.sync.get(null); - console.log(document.getElementById('checkURL').value); prom.then((res) => { var todos=res.todos; var todo=document.getElementById('todoData').value; @@ -34,10 +33,11 @@ document.getElementById('myBtn').addEventListener('click',function(){ var tab=browser.tabs.query({currentWindow:true,active:true}); tab.then((resd,resi)=>{ checkURL=resd[0].url; - console.log("asd"+checkURL); todos.push(todo); + var len=todos.length-1; todos.push(date); todos.push(time); + schedule(date,time,len); if(document.getElementById('checkURL').checked){ todos.push(checkURL); }else { @@ -51,10 +51,33 @@ document.getElementById('myBtn').addEventListener('click',function(){ }); }); var promi = browser.storage.sync.get(null); - prom.then((res) => { + promi.then((res) => { console.log(res.todos); }); }else{ alert('Please Enter Data'); } }); + +function schedule(date,time,len){ + console.log(date); + var myYear=parseInt(date.slice(0,4)); + var myMonth=parseInt(date.slice(5,7))-1; + var myDay=parseInt(date.slice(8,10)); + var myHour=parseInt(time.slice(0,2)); + var myMin=parseInt(time.slice(3,5)); + var newTimer=new Date(myYear,myMonth,myDay,myHour,myMin); + console.log(newTimer); + console.log(Date.parse(newTimer)); + console.log(Date.now()); + const when =Date.parse(newTimer); + console.log(len.toString()); + browser.alarms.create(len.toString(), { + when + }); + var x=browser.alarms.getAll(); + x.then((res)=>{ + console.log(res[0]); + }); + +}