import ext from "./utils/ext"; import storage from "./utils/storage"; ext.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.action === "perform-save") { var data = request.data; var url; var token; storage.get(["url", "token"], function(resp) { url = resp.url; token = resp.token; var params = { v: 1, u: encodeURIComponent(data.url), t: encodeURIComponent(data.title), d: encodeURIComponent(data.description), api_token: encodeURIComponent(token), i: encodeURIComponent(data.image), c: encodeURIComponent(data.comment) }; var queryString = Object.keys(params) .map(key => { return key + "=" + params[key]; }) .join("&"); fetch(url + "/api/links", { method: "POST", headers: { Accept: "application/json" }, body: new URLSearchParams(queryString) }).then(function(response) { if (200 === response.status) { sendResponse({ action: "saved" }); } else { sendResponse({ action: "error", status: response.status, error: response.statusText }); } }); }); return true; // https://stackoverflow.com/a/20077854 } });