From 39ba18438cb79c06e7c80d4568007cc77c6b4f01 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Thu, 23 Aug 2018 15:00:16 +0300 Subject: [PATCH] Fix notifications --- postify.js | 64 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/postify.js b/postify.js index 5cf5518..5790460 100644 --- a/postify.js +++ b/postify.js @@ -1,36 +1,52 @@ const APP_NAME = 'Postify'; -function sendUrl(url) { - // browser.notifications.create({ - // "type": "basic", - // "iconUrl": browser.extension.getURL("icons/postify-48.png"), - // "title": APP_NAME, - // "message": url, - // }); - - function sendUrlToServers(patterns) { - patterns.forEach(item => { - var regex = new RegExp(item.pattern || '.*', 'i'); - if (regex.test(url) && item.server) { - console.log('SEND', url, 'TO', item.server); - sendUrlToServer(url, item.server); - } - }); - } - - var patterns = PatternStorage.get().then(sendUrlToServers); -} - function sendUrlToServer(url, addr) { - fetch(addr, { + return fetch(addr, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ - url: url - }) + url: url, + }), + }); +} + +function showNotification(url, addr) { + console.log('NOTIFY', url, addr); + + var id = Math.random().toString(36).substr(2); + + browser.notifications.create(id, { + "type": "basic", + "iconUrl": browser.extension.getURL("icons/postify-48.png"), + "title": APP_NAME, + "message": `Sent "${url}" to ${addr}.`, + }); + + var close = function () { + browser.notifications.clear(id); + } + + setTimeout(close, 2000); +} + +function sendUrlToServers(url, patterns) { + patterns.forEach(item => { + var regex = new RegExp(item.pattern || '.*', 'i'); + if (regex.test(url) && item.server) { + sendUrlToServer(url, item.server).then( + () => showNotification(url, item.server), + (e) => console.log('SEND ERROR', e) + ); + } + }); +} + +function sendUrl(url) { + PatternStorage.get().then(patterns => { + sendUrlToServers(url, patterns); }); }