From e7aee8fbaa5631f10f3c96ee2e09978cc889c3c9 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Sun, 30 Sep 2018 23:34:12 +0200 Subject: [PATCH] :construction: Make it work with async work --- src/scripts/background.js | 26 ++++++++++---------------- src/scripts/popup.js | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index 82f08b8..60e294a 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -7,25 +7,19 @@ ext.runtime.onMessage.addListener( var data = JSON.parse(request.data); var url; var token; - storage.get('url', function(resp) { + storage.get(['url', 'token'], function(resp) { url = resp.url; - storage.get('token', function(resp) { - token = resp.token; - console.log("Extension Type: ", "/* @echo extension */"); - console.log("PERFORM AJAX", request.data); - var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token); - - console.log("Destination: ", destination); - fetch(destination) - .then(function(response) { - console.log(response); - }); - - sendResponse({ action: "saved" }); + token = resp.token; + var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token); + + console.log("Destination: ", destination); + fetch(destination).then(function(response) { + console.log(response); + sendResponse({ action: "saved" }); }); }); - - + + return true; // https://stackoverflow.com/a/20077854 } } ); \ No newline at end of file diff --git a/src/scripts/popup.js b/src/scripts/popup.js index f44abe6..1a0546c 100644 --- a/src/scripts/popup.js +++ b/src/scripts/popup.js @@ -21,11 +21,11 @@ var template = (data) => { `); -} +}; var renderMessage = (message) => { var displayContainer = document.getElementById("display-container"); displayContainer.innerHTML = `

${message}

`; -} +}; var renderBookmark = (data) => { var displayContainer = document.getElementById("display-container") @@ -44,15 +44,15 @@ ext.tabs.query({active: true, currentWindow: true}, function(tabs) { popup.addEventListener("click", function(e) { if(e.target && e.target.matches("#save-btn")) { - e.preventDefault(); - var data = e.target.getAttribute("data-bookmark"); - ext.runtime.sendMessage({ action: "perform-save", data: data }, function(response) { - if(response && response.action === "saved") { - renderMessage("Your bookmark was saved successfully!"); - } else { - renderMessage("Sorry, there was an error while saving your bookmark."); - } - }) + e.preventDefault(); + var data = e.target.getAttribute("data-bookmark"); + ext.runtime.sendMessage({action: "perform-save", data: data}, function (response) { + if (response && response.action === "saved") { + renderMessage("Your bookmark was saved successfully!"); + } else { + renderMessage("Sorry, there was an error while saving your bookmark."); + } + }); } }); @@ -60,4 +60,4 @@ var optionsLink = document.querySelector(".js-options"); optionsLink.addEventListener("click", function(e) { e.preventDefault(); ext.tabs.create({'url': ext.extension.getURL('options.html')}); -}) +});