From a9ad1533c91d029572a0bd0cec48bb842053a50e Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Wed, 17 Oct 2018 08:14:51 +0200 Subject: [PATCH] :alien: Move from GET to POST --- src/scripts/background.js | 28 +++++++++++++++++++++++++--- src/scripts/contentscript.js | 11 +++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index 8aa4aeb..4ef71f3 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -10,10 +10,32 @@ ext.runtime.onMessage.addListener( storage.get(['url', 'token'], function(resp) { url = resp.url; token = resp.token; - var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token); - fetch(destination).then(function(response) { - sendResponse({ action: "saved" }); + 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) { + sendResponse({ action: "saved" }); }); + // var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token); + // fetch(destination).then(function(response) { + // sendResponse({ action: "saved" }); + // }); }); return true; // https://stackoverflow.com/a/20077854 diff --git a/src/scripts/contentscript.js b/src/scripts/contentscript.js index 30fd69d..c45ded7 100644 --- a/src/scripts/contentscript.js +++ b/src/scripts/contentscript.js @@ -2,20 +2,23 @@ import ext from "./utils/ext"; var extractTags = () => { var url = document.location.href; - if(!url || !url.match(/^http/)) return; + if (!url || !url.match(/^http/)) { + console.error("Invalid URL : " + url); + return; + } var data = { title: "", description: "", image: "", - url: document.location.href + url: url }; var ogTitle = document.querySelector("meta[property='og:title']"); if(ogTitle) { - data.title = ogTitle.getAttribute("content") + data.title = ogTitle.getAttribute("content"); } else { - data.title = document.title + data.title = document.title; } var descriptionTag = document.querySelector("meta[property='og:description']") || document.querySelector("meta[name='description']");