2018-04-08 22:33:36 +02:00
|
|
|
import ext from "./utils/ext";
|
|
|
|
import storage from "./utils/storage";
|
|
|
|
|
2020-07-07 11:28:51 +02:00
|
|
|
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 + ")");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-06 17:01:54 +02:00
|
|
|
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)
|
|
|
|
};
|
2020-07-02 13:05:21 +02:00
|
|
|
if (data.extract) {
|
|
|
|
params["e"] = true;
|
|
|
|
}
|
2019-05-06 17:01:54 +02:00
|
|
|
var queryString = Object.keys(params)
|
|
|
|
.map(key => {
|
|
|
|
return key + "=" + params[key];
|
2018-10-17 08:14:51 +02:00
|
|
|
})
|
2019-05-06 17:01:54 +02:00
|
|
|
.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
|
|
|
|
});
|
|
|
|
}
|
2018-04-08 22:33:36 +02:00
|
|
|
});
|
2019-05-06 17:01:54 +02:00
|
|
|
});
|
2018-09-30 23:34:12 +02:00
|
|
|
|
2019-05-06 17:01:54 +02:00
|
|
|
return true; // https://stackoverflow.com/a/20077854
|
2018-04-08 22:33:36 +02:00
|
|
|
}
|
2019-05-06 17:01:54 +02:00
|
|
|
});
|