parent
25fe0c5849
commit
8723154371
@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,44 +1,47 @@
|
|||||||
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 = {
|
v: 1,
|
||||||
v: 1,
|
u: encodeURIComponent(data.url),
|
||||||
u: encodeURIComponent(data.url),
|
t: encodeURIComponent(data.title),
|
||||||
t: encodeURIComponent(data.title),
|
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)
|
||||||
var queryString = Object.keys(params).map((key) => {
|
.map(key => {
|
||||||
return key + '=' + params[key];
|
return key + "=" + params[key];
|
||||||
}).join('&');
|
|
||||||
fetch(url + "/api/links", {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Accept': 'application/json'
|
|
||||||
},
|
|
||||||
body: new URLSearchParams(queryString)
|
|
||||||
})
|
})
|
||||||
.then(function(response) {
|
.join("&");
|
||||||
if (200 === response.status) {
|
fetch(url + "/api/links", {
|
||||||
sendResponse({action: "saved"});
|
method: "POST",
|
||||||
} else {
|
headers: {
|
||||||
sendResponse({action: "error", status: response.status, error: response.statusText});
|
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
|
return true; // https://stackoverflow.com/a/20077854
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
});
|
||||||
|
@ -16,33 +16,39 @@ 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']");
|
||||||
} else {
|
if (imgTag) {
|
||||||
imgTag = document.querySelector("link[rel=icon]") || document.querySelector("link[rel=apple-touch-icon]");
|
data.image = imgTag.getAttribute("content");
|
||||||
if (imgTag) {
|
} else {
|
||||||
data.image = imgTag.getAttribute("href");
|
imgTag =
|
||||||
}
|
document.querySelector("link[rel=icon]") ||
|
||||||
|
document.querySelector("link[rel=apple-touch-icon]");
|
||||||
|
if (imgTag) {
|
||||||
|
data.image = imgTag.getAttribute("href");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
function onRequest(request, sender, sendResponse) {
|
function onRequest(request, sender, sendResponse) {
|
||||||
if (request.action === 'process-page') {
|
if (request.action === "process-page") {
|
||||||
sendResponse(extractTags())
|
sendResponse(extractTags());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,28 +5,28 @@ 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;
|
||||||
});
|
});
|
||||||
|
|
||||||
urlInput.addEventListener("blur", function(e) {
|
urlInput.addEventListener("blur", function(e) {
|
||||||
var value = this.value;
|
var value = this.value;
|
||||||
storage.set({ url: value }, function() {
|
storage.set({ url: value }, function() {
|
||||||
message.textContent = ext.i18n.getMessage("URLChanged");
|
message.textContent = ext.i18n.getMessage("URLChanged");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tokenInput.addEventListener("blur", function(e) {
|
tokenInput.addEventListener("blur", function(e) {
|
||||||
var value = this.value;
|
var value = this.value;
|
||||||
storage.set({ token: value }, function() {
|
storage.set({ token: value }, function() {
|
||||||
message.textContent = ext.i18n.getMessage("tokenSaved");
|
message.textContent = ext.i18n.getMessage("tokenSaved");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
document.querySelectorAll("[data-message]").forEach(function(elt) {
|
document.querySelectorAll("[data-message]").forEach(function(elt) {
|
||||||
elt.textContent = ext.i18n.getMessage(elt.dataset.message);
|
elt.textContent = ext.i18n.getMessage(elt.dataset.message);
|
||||||
});
|
});
|
||||||
|
@ -2,69 +2,80 @@ import ext from "./utils/ext";
|
|||||||
import storage from "./utils/storage";
|
import storage from "./utils/storage";
|
||||||
|
|
||||||
var page_data = {
|
var page_data = {
|
||||||
title: "",
|
title: "",
|
||||||
description: "",
|
description: "",
|
||||||
image: "",
|
image: "",
|
||||||
comment: "",
|
comment: "",
|
||||||
url: document.location.href
|
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;
|
||||||
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;
|
||||||
if ("" !== img) {
|
document.querySelector("[data-template=description]").textContent =
|
||||||
var img = document.createElement("image");
|
page_data.description;
|
||||||
img.src = page_data.image;
|
if ("" !== img) {
|
||||||
document.querySelector("[data-template=image]").appendChild(img);
|
var img = document.createElement("image");
|
||||||
}
|
img.src = page_data.image;
|
||||||
document.querySelector("[data-template=url]").textContent = page_data.url;
|
document.querySelector("[data-template=image]").appendChild(img);
|
||||||
document.querySelector("[data-template=url]").setAttribute("href", page_data.url);
|
}
|
||||||
|
document.querySelector("[data-template=url]").textContent = 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(
|
||||||
if (response && response.action === "saved") {
|
{ action: "perform-save", data: page_data },
|
||||||
renderMessage(ext.i18n.getMessage("savedSuccessfully"));
|
function(response) {
|
||||||
} else {
|
if (response && response.action === "saved") {
|
||||||
renderMessage(response.error + " ("+response.status+")");
|
renderMessage(ext.i18n.getMessage("savedSuccessfully"));
|
||||||
}
|
} else {
|
||||||
});
|
renderMessage(response.error + " (" + response.status + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var dataMessages = document.querySelectorAll("[data-message]");
|
var dataMessages = document.querySelectorAll("[data-message]");
|
||||||
[].forEach.call(dataMessages, function(elt) {
|
[].forEach.call(dataMessages, function(elt) {
|
||||||
elt.textContent = ext.i18n.getMessage(elt.dataset.message);
|
elt.textContent = ext.i18n.getMessage(elt.dataset.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") });
|
||||||
});
|
});
|
||||||
|
@ -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();
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user