🚨 Add prettier

Fix #2
This commit is contained in:
Clement Desmidt 2019-05-06 17:01:54 +02:00
parent 25fe0c5849
commit 8723154371
9 changed files with 6328 additions and 144 deletions

View File

@ -47,8 +47,16 @@
"gulp-sourcemaps": "^1.6.0", "gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.5.4", "gulp-uglify": "^1.5.4",
"gulp-zip": "^2.0.3", "gulp-zip": "^2.0.3",
"husky": "^2.2.0",
"preprocessify": "^1.0.1", "preprocessify": "^1.0.1",
"prettier": "^1.17.0",
"pretty-quick": "^1.10.0",
"vinyl-buffer": "^1.0.0", "vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0" "vinyl-source-stream": "^1.1.0"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
} }
} }

View File

@ -1,13 +1,12 @@
import ext from "./utils/ext"; import ext from "./utils/ext";
import storage from "./utils/storage"; 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 = 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) {
url = resp.url; url = resp.url;
token = resp.token; token = resp.token;
var params = { var params = {
@ -17,28 +16,32 @@ ext.runtime.onMessage.addListener(
d: encodeURIComponent(data.description), d: encodeURIComponent(data.description),
api_token: encodeURIComponent(token), api_token: encodeURIComponent(token),
i: encodeURIComponent(data.image), i: encodeURIComponent(data.image),
c: encodeURIComponent(data.comment), c: encodeURIComponent(data.comment)
}; };
var queryString = Object.keys(params).map((key) => { var queryString = Object.keys(params)
return key + '=' + params[key]; .map(key => {
}).join('&'); return key + "=" + params[key];
})
.join("&");
fetch(url + "/api/links", { fetch(url + "/api/links", {
method: 'POST', method: "POST",
headers: { headers: {
'Accept': 'application/json' Accept: "application/json"
}, },
body: new URLSearchParams(queryString) body: new URLSearchParams(queryString)
}) }).then(function(response) {
.then(function(response) {
if (200 === response.status) { if (200 === response.status) {
sendResponse({action: "saved"}); sendResponse({ action: "saved" });
} else { } else {
sendResponse({action: "error", status: response.status, error: response.statusText}); sendResponse({
action: "error",
status: response.status,
error: response.statusText
});
} }
}); });
}); });
return true; // https://stackoverflow.com/a/20077854 return true; // https://stackoverflow.com/a/20077854
} }
} });
);

View File

@ -16,22 +16,28 @@ var extractTags = () => {
}; };
var ogTitle = document.querySelector("meta[property='og:title']"); var ogTitle = document.querySelector("meta[property='og:title']");
if(ogTitle) { if (ogTitle) {
data.title = ogTitle.getAttribute("content"); data.title = ogTitle.getAttribute("content");
} else { } else {
data.title = document.title; data.title = document.title;
} }
var descriptionTag = document.querySelector("meta[property='og:description']") || document.querySelector("meta[name='description']"); var descriptionTag =
if(descriptionTag) { document.querySelector("meta[property='og:description']") ||
data.description = descriptionTag.getAttribute("content") document.querySelector("meta[name='description']");
if (descriptionTag) {
data.description = descriptionTag.getAttribute("content");
} }
var imgTag = document.querySelector("meta[property='og:image']") || document.querySelector("meta[property='twitter-image']"); var imgTag =
if(imgTag) { document.querySelector("meta[property='og:image']") ||
data.image = imgTag.getAttribute("content") document.querySelector("meta[property='twitter-image']");
if (imgTag) {
data.image = imgTag.getAttribute("content");
} else { } else {
imgTag = document.querySelector("link[rel=icon]") || document.querySelector("link[rel=apple-touch-icon]"); imgTag =
document.querySelector("link[rel=icon]") ||
document.querySelector("link[rel=apple-touch-icon]");
if (imgTag) { if (imgTag) {
data.image = imgTag.getAttribute("href"); data.image = imgTag.getAttribute("href");
} }
@ -41,8 +47,8 @@ var extractTags = () => {
}; };
function onRequest(request, sender, sendResponse) { function onRequest(request, sender, sendResponse) {
if (request.action === 'process-page') { if (request.action === "process-page") {
sendResponse(extractTags()) sendResponse(extractTags());
} }
} }

View File

@ -1,19 +1,21 @@
'use strict'; "use strict";
import ext from "./utils/ext"; import ext from "./utils/ext";
var LIVERELOAD_HOST = 'localhost:'; var LIVERELOAD_HOST = "localhost:";
var LIVERELOAD_PORT = 35729; var LIVERELOAD_PORT = 35729;
var connection = new WebSocket('ws://' + LIVERELOAD_HOST + LIVERELOAD_PORT + '/livereload'); var connection = new WebSocket(
"ws://" + LIVERELOAD_HOST + LIVERELOAD_PORT + "/livereload"
);
connection.onerror = function (error) { connection.onerror = function(error) {
console.log('reload connection got error:', error); console.log("reload connection got error:", error);
}; };
connection.onmessage = function (e) { connection.onmessage = function(e) {
if (e.data) { if (e.data) {
var data = JSON.parse(e.data); var data = JSON.parse(e.data);
if (data && data.command === 'reload') { if (data && data.command === "reload") {
ext.runtime.reload(); ext.runtime.reload();
} }
} }

View File

@ -5,11 +5,11 @@ var urlInput = document.querySelector("[name=url]");
var tokenInput = document.querySelector("[name=token]"); var tokenInput = document.querySelector("[name=token]");
var message = document.getElementById("msg"); var message = document.getElementById("msg");
storage.get('url', function(resp) { storage.get("url", function(resp) {
urlInput.value = resp.url || "https://app.readlater.shikiryu.com"; urlInput.value = resp.url || "https://app.readlater.shikiryu.com";
}); });
storage.get('token', function(resp) { storage.get("token", function(resp) {
tokenInput.value = resp.token; tokenInput.value = resp.token;
}); });

View File

@ -10,51 +10,62 @@ var page_data = {
}; };
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;
if(color) { if (color) {
popup.style.backgroundColor = color popup.style.backgroundColor = color;
} }
}); });
var renderMessage = (message) => { var renderMessage = message => {
var displayContainer = document.getElementById("display-container"); var displayContainer = document.getElementById("display-container");
displayContainer.textContent = message; displayContainer.textContent = message;
}; };
var renderBookmark = (data) => { var renderBookmark = data => {
if (data) { if (data) {
page_data = data; page_data = data;
document.querySelector("[data-template=title]").textContent = page_data.title; document.querySelector("[data-template=title]").textContent =
document.querySelector("[data-template=description]").textContent = page_data.description; page_data.title;
document.querySelector("[data-template=description]").textContent =
page_data.description;
if ("" !== img) { if ("" !== img) {
var img = document.createElement("image"); var img = document.createElement("image");
img.src = page_data.image; img.src = page_data.image;
document.querySelector("[data-template=image]").appendChild(img); document.querySelector("[data-template=image]").appendChild(img);
} }
document.querySelector("[data-template=url]").textContent = page_data.url; document.querySelector("[data-template=url]").textContent = page_data.url;
document.querySelector("[data-template=url]").setAttribute("href", page_data.url); document
.querySelector("[data-template=url]")
.setAttribute("href", page_data.url);
} else { } else {
renderMessage(ext.i18n.getMessage("cantExtractTitle")); renderMessage(ext.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];
chrome.tabs.sendMessage(activeTab.id, { action: 'process-page' }, renderBookmark); chrome.tabs.sendMessage(
activeTab.id,
{ action: "process-page" },
renderBookmark
);
}); });
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();
page_data.comment = document.getElementById("comment").value; page_data.comment = document.getElementById("comment").value;
ext.runtime.sendMessage({action: "perform-save", data: page_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(ext.i18n.getMessage("savedSuccessfully")); renderMessage(ext.i18n.getMessage("savedSuccessfully"));
} else { } else {
renderMessage(response.error + " ("+response.status+")"); renderMessage(response.error + " (" + response.status + ")");
} }
}); }
);
} }
}); });
@ -66,5 +77,5 @@ var dataMessages = document.querySelectorAll("[data-message]");
var optionsLink = document.querySelector(".js-options"); var optionsLink = document.querySelector(".js-options");
optionsLink.addEventListener("click", function(e) { optionsLink.addEventListener("click", function(e) {
e.preventDefault(); e.preventDefault();
ext.tabs.create({'url': ext.extension.getURL('options.html')}); ext.tabs.create({ url: ext.extension.getURL("options.html") });
}); });

View File

@ -1,68 +1,66 @@
const apis = [ const apis = [
'alarms', "alarms",
'bookmarks', "bookmarks",
'browserAction', "browserAction",
'commands', "commands",
'contextMenus', "contextMenus",
'cookies', "cookies",
'downloads', "downloads",
'events', "events",
'extension', "extension",
'extensionTypes', "extensionTypes",
'history', "history",
'i18n', "i18n",
'idle', "idle",
'notifications', "notifications",
'pageAction', "pageAction",
'runtime', "runtime",
'storage', "storage",
'tabs', "tabs",
'webNavigation', "webNavigation",
'webRequest', "webRequest",
'windows', "windows"
] ];
function Extension () { function Extension() {
const _this = this const _this = this;
apis.forEach(function (api) { apis.forEach(function(api) {
_this[api] = null;
_this[api] = null
try { try {
if (chrome[api]) { if (chrome[api]) {
_this[api] = chrome[api] _this[api] = chrome[api];
} }
} catch (e) {} } catch (e) {}
try { try {
if (window[api]) { if (window[api]) {
_this[api] = window[api] _this[api] = window[api];
} }
} catch (e) {} } catch (e) {}
try { try {
if (browser[api]) { if (browser[api]) {
_this[api] = browser[api] _this[api] = browser[api];
} }
} catch (e) {} } catch (e) {}
try { try {
_this.api = browser.extension[api] _this.api = browser.extension[api];
} catch (e) {} } catch (e) {}
}) });
try { try {
if (browser && browser.runtime) { if (browser && browser.runtime) {
this.runtime = browser.runtime this.runtime = browser.runtime;
} }
} catch (e) {} } catch (e) {}
try { try {
if (browser && browser.browserAction) { if (browser && browser.browserAction) {
this.browserAction = browser.browserAction this.browserAction = browser.browserAction;
} }
} catch (e) {} } catch (e) {}
} }
module.exports = new Extension(); module.exports = new Extension();

View File

@ -1,3 +1,3 @@
import ext from "./ext"; import ext from "./ext";
module.exports = (ext.storage.sync ? ext.storage.sync : ext.storage.local); module.exports = ext.storage.sync ? ext.storage.sync : ext.storage.local;

6156
yarn.lock Normal file

File diff suppressed because it is too large Load Diff