♻️ Simplify usage of object and JSON

This commit is contained in:
Clement Desmidt 2018-10-03 22:58:12 +02:00
parent 3943e86d0b
commit 30655fc21c
3 changed files with 15 additions and 11 deletions

View File

@ -4,7 +4,7 @@ import storage from "./utils/storage";
ext.runtime.onMessage.addListener( ext.runtime.onMessage.addListener(
function(request, sender, sendResponse) { function(request, sender, sendResponse) {
if(request.action === "perform-save") { if(request.action === "perform-save") {
var data = JSON.parse(request.data); var data = request.data;
var url; var url;
var token; var token;
storage.get(['url', 'token'], function(resp) { storage.get(['url', 'token'], function(resp) {

View File

@ -8,7 +8,7 @@ var extractTags = () => {
title: "", title: "",
description: "", description: "",
url: document.location.href url: document.location.href
} };
var ogTitle = document.querySelector("meta[property='og:title']"); var ogTitle = document.querySelector("meta[property='og:title']");
if(ogTitle) { if(ogTitle) {
@ -23,7 +23,7 @@ var extractTags = () => {
} }
return data; return data;
} };
function onRequest(request, sender, sendResponse) { function onRequest(request, sender, sendResponse) {
if (request.action === 'process-page') { if (request.action === 'process-page') {

View File

@ -1,6 +1,12 @@
import ext from "./utils/ext"; import ext from "./utils/ext";
import storage from "./utils/storage"; import storage from "./utils/storage";
var page_data = {
title: "",
description: "",
url: document.location.href
};
var popup = document.getElementById("app"); var popup = document.getElementById("app");
storage.get('color', function(resp) { storage.get('color', function(resp) {
var color = resp.color; var color = resp.color;
@ -10,7 +16,6 @@ storage.get('color', function(resp) {
}); });
var template = (data) => { var template = (data) => {
var json = JSON.stringify(data);
return (` return (`
<div class="site-description"> <div class="site-description">
<h3 class="title">${data.title}</h3> <h3 class="title">${data.title}</h3>
@ -18,7 +23,7 @@ var template = (data) => {
<a href="${data.url}" target="_blank" class="url">${data.url}</a> <a href="${data.url}" target="_blank" class="url">${data.url}</a>
</div> </div>
<div class="action-container"> <div class="action-container">
<button data-bookmark='${json}' id="save-btn" class="btn btn-primary">Save</button> <button id="save-btn" class="btn btn-primary">Save</button>
</div> </div>
`); `);
}; };
@ -30,12 +35,12 @@ var renderMessage = (message) => {
var renderBookmark = (data) => { var renderBookmark = (data) => {
var displayContainer = document.getElementById("display-container") var displayContainer = document.getElementById("display-container")
if(data) { if(data) {
var tmpl = template(data); page_data = data;
displayContainer.innerHTML = tmpl; displayContainer.innerHTML = template(data);
} else { } else {
renderMessage(browser.i18n.getMessage("cantExtractTitle")); renderMessage(browser.i18n.getMessage("cantExtractTitle"));
} }
} };
ext.tabs.query({active: true, currentWindow: true}, function(tabs) { ext.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0]; var activeTab = tabs[0];
@ -45,8 +50,8 @@ ext.tabs.query({active: true, currentWindow: true}, function(tabs) {
popup.addEventListener("click", function(e) { popup.addEventListener("click", function(e) {
if(e.target && e.target.matches("#save-btn")) { if(e.target && e.target.matches("#save-btn")) {
e.preventDefault(); e.preventDefault();
var data = e.target.getAttribute("data-bookmark"); // var data = e.target.getAttribute("data-bookmark");
ext.runtime.sendMessage({action: "perform-save", data: data}, function (response) { ext.runtime.sendMessage({action: "perform-save", data: page_data}, function (response) {
if (response && response.action === "saved") { if (response && response.action === "saved") {
renderMessage(browser.i18n.getMessage("savedSuccessfully")); renderMessage(browser.i18n.getMessage("savedSuccessfully"));
} else { } else {
@ -57,7 +62,6 @@ popup.addEventListener("click", function(e) {
}); });
var dataMessages = document.querySelectorAll("[data-message]"); var dataMessages = document.querySelectorAll("[data-message]");
console.dir(dataMessages);
[].forEach.call(dataMessages, function(elt) { [].forEach.call(dataMessages, function(elt) {
console.log(elt.dataset.message); console.log(elt.dataset.message);
elt.innerHTML = browser.i18n.getMessage(elt.dataset.message); elt.innerHTML = browser.i18n.getMessage(elt.dataset.message);