Fix notifications

This commit is contained in:
Anton Vakhrushev 2018-08-23 15:00:16 +03:00
parent 48ce7ac0c1
commit 39ba18438c

View File

@ -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);
});
}