import ext from "./utils/ext"; import storage from "./utils/storage"; let onMenuAdded = function() { if (ext.browser.runtime.lastError) { console.log(`Error: ${ext.browser.runtime.lastError}`); } else { console.log("Readlater menu created successfully"); } }; ext.contextMenus.create( { id: "dom-selection", title: ext.i18n.getMessage("selectDomToSend"), contexts: ["all"] }, onMenuAdded ); ext.contextMenus.onClicked.addListener(function(info, tab) { if ("dom-selection" === info.menuItemId) { var page_data = { title: "", description: "", image: "", comment: "", url: document.location.href }; ext.tabs.sendMessage(tab.id, { action: "process-page" }, function(data) { ext.tabs.sendMessage(tab.id, { action: "DOM", data: data }, function( response ) { if (response && response.action === "saved") { console.log(ext.i18n.getMessage("savedSuccessfully")); } else { console.log(response.error + " (" + response.status + ")"); } }); }); } }); 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) }; if (data.extract) { params["e"] = true; } 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 } });