🚧 Make it work with async work

This commit is contained in:
Clement Desmidt 2018-09-30 23:34:12 +02:00
parent 21d1e6f0ef
commit e7aee8fbaa
2 changed files with 22 additions and 28 deletions

View File

@ -7,25 +7,19 @@ ext.runtime.onMessage.addListener(
var data = JSON.parse(request.data); var data = JSON.parse(request.data);
var url; var url;
var token; var token;
storage.get('url', function(resp) { storage.get(['url', 'token'], function(resp) {
url = resp.url; url = resp.url;
storage.get('token', function(resp) {
token = resp.token; 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); var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token);
console.log("Destination: ", destination); console.log("Destination: ", destination);
fetch(destination) fetch(destination).then(function(response) {
.then(function(response) {
console.log(response); console.log(response);
});
sendResponse({ action: "saved" }); sendResponse({ action: "saved" });
}); });
}); });
return true; // https://stackoverflow.com/a/20077854
} }
} }
); );

View File

@ -21,11 +21,11 @@ var template = (data) => {
<button data-bookmark='${json}' id="save-btn" class="btn btn-primary">Save</button> <button data-bookmark='${json}' id="save-btn" class="btn btn-primary">Save</button>
</div> </div>
`); `);
} };
var renderMessage = (message) => { var renderMessage = (message) => {
var displayContainer = document.getElementById("display-container"); var displayContainer = document.getElementById("display-container");
displayContainer.innerHTML = `<p class='message'>${message}</p>`; displayContainer.innerHTML = `<p class='message'>${message}</p>`;
} };
var renderBookmark = (data) => { var renderBookmark = (data) => {
var displayContainer = document.getElementById("display-container") var displayContainer = document.getElementById("display-container")
@ -52,7 +52,7 @@ popup.addEventListener("click", function(e) {
} else { } else {
renderMessage("Sorry, there was an error while saving your bookmark."); 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) { optionsLink.addEventListener("click", function(e) {
e.preventDefault(); e.preventDefault();
ext.tabs.create({'url': ext.extension.getURL('options.html')}); ext.tabs.create({'url': ext.extension.getURL('options.html')});
}) });