From b94b88e6898492e2b1b6632bfb941a560f586f73 Mon Sep 17 00:00:00 2001 From: ikipatang <5412352+ikipatang@users.noreply.github.com> Date: Sun, 26 Nov 2017 23:22:20 +0100 Subject: [PATCH 1/7] Fix share current tab function to handle given tab in parameter --- src/background.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/background.js b/src/background.js index 3227082..9c61344 100644 --- a/src/background.js +++ b/src/background.js @@ -17,21 +17,16 @@ function openInPopup(url) { * 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) => { - if(storage.url){ - const shareUrl = `${storage.url}?post=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&source=bookmarklet`; - openInPopup(shareUrl); - }else{ - browser.runtime.openOptionsPage() - } - }); +function shareCurrentTab(tab) { + const url = tab.url; + const title = tab.title || url; + browser.storage.local.get('url').then((storage) => { + if(!storage.url){ + browser.runtime.openOptionsPage(); + return; + } + const shareUrl = `${storage.url}?post=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&source=bookmarklet`; + openInPopup(shareUrl); }); } From ff775a9c7d378d705ba5e5ce0110244a94f9db0e Mon Sep 17 00:00:00 2001 From: ikipatang <5412352+ikipatang@users.noreply.github.com> Date: Sun, 26 Nov 2017 23:40:29 +0100 Subject: [PATCH 2/7] Fix storage init and usage --- src/background.js | 2 +- src/storage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/background.js b/src/background.js index 9c61344..e9e3b61 100644 --- a/src/background.js +++ b/src/background.js @@ -20,7 +20,7 @@ function openInPopup(url) { function shareCurrentTab(tab) { const url = tab.url; const title = tab.title || url; - browser.storage.local.get('url').then((storage) => { + browser.storage.local.get().then((storage) => { if(!storage.url){ browser.runtime.openOptionsPage(); return; diff --git a/src/storage.js b/src/storage.js index 20ae517..942fd9b 100644 --- a/src/storage.js +++ b/src/storage.js @@ -18,7 +18,7 @@ If we don't, then store the default settings. */ function checkStoredSettings(storedSettings) { if (!storedSettings.storage) { - browser.storage.local.set({storage}); + browser.storage.local.set(storage); } } From 75b40ece5235e08ffef3ae136ea8ea522b5fb7e1 Mon Sep 17 00:00:00 2001 From: ikipatang <5412352+ikipatang@users.noreply.github.com> Date: Sun, 26 Nov 2017 23:24:10 +0100 Subject: [PATCH 3/7] PageAction: init in config, storage and background click listener --- manifest.json | 5 +++++ src/background.js | 4 ++++ src/storage.js | 1 + 3 files changed, 10 insertions(+) diff --git a/manifest.json b/manifest.json index b70501c..0eb1e34 100644 --- a/manifest.json +++ b/manifest.json @@ -20,5 +20,10 @@ "browser_action": { "default_icon": "resources/icons/icon-24.png", "default_title": "Share" + }, + "page_action": { + "browser_style": false, + "default_icon": "resources/icons/icon-24.png", + "default_title": "Share" } } diff --git a/src/background.js b/src/background.js index e9e3b61..4ec9323 100644 --- a/src/background.js +++ b/src/background.js @@ -30,4 +30,8 @@ function shareCurrentTab(tab) { }); } +/* +Listeners + */ browser.browserAction.onClicked.addListener(shareCurrentTab); +browser.pageAction.onClicked.addListener(shareCurrentTab); diff --git a/src/storage.js b/src/storage.js index 942fd9b..238a76a 100644 --- a/src/storage.js +++ b/src/storage.js @@ -3,6 +3,7 @@ Default settings. Initialize storage to these values. */ var storage = { url: '', + pageAction: false, } /* From 56131220a2be29e856286eb49f0f9ea7c7ddf351 Mon Sep 17 00:00:00 2001 From: ikipatang <5412352+ikipatang@users.noreply.github.com> Date: Sun, 26 Nov 2017 23:54:55 +0100 Subject: [PATCH 4/7] PageAction: display on init and tab switch if activated --- src/background.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/background.js b/src/background.js index 4ec9323..1e4d8c7 100644 --- a/src/background.js +++ b/src/background.js @@ -30,8 +30,38 @@ function shareCurrentTab(tab) { }); } +/** + * Show Page Action on current page if activated + * @param {string} tabId - Current tab id + * @return {void} - + */ +function showPageAction(tabId){ + browser.storage.local.get().then((storage) => { + if(storage.pageAction){ + browser.pageAction.show(tabId); + } + }); +} + +/** + * Show page action with given active information + * @param {object} activeInfo - + * @return {void} - + */ +function tabActivation(activeInfo){ + showPageAction(activeInfo.tabId); +} + +/* +Initialize + */ +browser.tabs.query({active: true, currentWindow: true}).then((tabs) => { + showPageAction(tabs[0].id); +}); + /* Listeners */ +browser.tabs.onActivated.addListener(tabActivation); browser.browserAction.onClicked.addListener(shareCurrentTab); browser.pageAction.onClicked.addListener(shareCurrentTab); From 308d9585783d793aad34f04fe2c89e96149510cb Mon Sep 17 00:00:00 2001 From: ikipatang <5412352+ikipatang@users.noreply.github.com> Date: Mon, 27 Nov 2017 23:04:10 +0100 Subject: [PATCH 5/7] PageAction: create settings input --- src/background.js | 4 +--- src/options/options.html | 1 + src/options/options.js | 42 ++++++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/background.js b/src/background.js index 1e4d8c7..bc5dfe8 100644 --- a/src/background.js +++ b/src/background.js @@ -37,9 +37,7 @@ function shareCurrentTab(tab) { */ function showPageAction(tabId){ browser.storage.local.get().then((storage) => { - if(storage.pageAction){ - browser.pageAction.show(tabId); - } + storage.pageAction ? browser.pageAction.show(tabId) : browser.pageAction.hide(tabId); }); } diff --git a/src/options/options.html b/src/options/options.html index 681b6de..20aeb68 100644 --- a/src/options/options.html +++ b/src/options/options.html @@ -9,6 +9,7 @@