From cc32abf47bcfc7f00ed51a453cd90044f053f99a Mon Sep 17 00:00:00 2001 From: Author Name ikipatang <5412352+ikipatang@users.noreply.github.com> Date: Sun, 19 Nov 2017 02:05:28 +0100 Subject: [PATCH] Add share link and URL settings --- background.js | 24 ++++++++++++++++++++++++ options/options.css | 18 ++++++++++++++++++ options/options.html | 17 +++++++++++++++++ options/options.js | 38 ++++++++++++++++++++++++++++++++++++++ storage.js | 26 ++++++++++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 background.js create mode 100644 options/options.css create mode 100644 options/options.html create mode 100644 options/options.js create mode 100644 storage.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..0977326 --- /dev/null +++ b/background.js @@ -0,0 +1,24 @@ +/** + * Share Current Tab + * @return {void} - + */ +function shareCurrentTab() { + browser.tabs.query({ + active: true, + currentWindow: true, + }).then((tabs) => { + const url = tabs[0].url; + const title = tabs[0].title || url; + browser.storage.local.get('url').then((storage) => { + const shareUrl = `${storage.url}?post=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&source=bookmarklet`; + + browser.windows.create({ + url: shareUrl, + type: 'popup', + allowScriptsToClose: true, + }); + }); + }); +} + +browser.browserAction.onClicked.addListener(shareCurrentTab); diff --git a/options/options.css b/options/options.css new file mode 100644 index 0000000..19f36be --- /dev/null +++ b/options/options.css @@ -0,0 +1,18 @@ + +body { + width: 25em; + font-family: "Open Sans Light", sans-serif; + font-size: 0.9em; + font-weight: 300; +} + +input#url { + margin: 0.5em; + width: 50em; + height: 2.5em; +} + +input { + width: 20em; + height: 2.5em; +} diff --git a/options/options.html b/options/options.html new file mode 100644 index 0000000..681b6de --- /dev/null +++ b/options/options.html @@ -0,0 +1,17 @@ + + + + + + + + + + +
URL:
+ + + + + + diff --git a/options/options.js b/options/options.js new file mode 100644 index 0000000..481e2f5 --- /dev/null +++ b/options/options.js @@ -0,0 +1,38 @@ +/* +Store options UI using browser.storage.local. +*/ +function storeSettings() { + const url = document.querySelector("#url"); + // console.log('storeSettings url'); + // console.log(url); + browser.storage.local.set({ + url: url.value, + }); +} + +/* +Update the options UI with the settings values retrieved from storage, +or the default settings if the stored settings are empty. +*/ +function updateUI(restoredSettings) { + const url = document.querySelector("#url"); + // console.log('restoredSettings url'); + // console.log(url); + url.value = restoredSettings.url || ""; +} + +function onError(e) { + console.error(e); +} + +/* +On opening the options page, fetch stored settings and update the UI with them. +*/ +const gettingStoredSettings = browser.storage.local.get(); +gettingStoredSettings.then(updateUI, onError); + +/* +On clicking the save button, save the currently selected settings. +*/ +const saveButton = document.querySelector("#save-button"); +saveButton.addEventListener("click", storeSettings); diff --git a/storage.js b/storage.js new file mode 100644 index 0000000..20ae517 --- /dev/null +++ b/storage.js @@ -0,0 +1,26 @@ +/* +Default settings. Initialize storage to these values. +*/ +var storage = { + url: '', +} + +/* +Generic error logger. +*/ +function onError(e) { + console.error(e); +} + +/* +On startup, check whether we have stored settings. +If we don't, then store the default settings. +*/ +function checkStoredSettings(storedSettings) { + if (!storedSettings.storage) { + browser.storage.local.set({storage}); + } +} + +const gettingStoredSettings = browser.storage.local.get(); +gettingStoredSettings.then(checkStoredSettings, onError);