From 02734b5d76c6e602ed30e66486e85835e18f25cf Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Fri, 7 Jun 2024 00:51:55 +0200 Subject: [PATCH] make it a toolbar button --- background.js | 53 +++++++++++++++++++++++---------------------------- manifest.json | 3 ++- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/background.js b/background.js index e2187df..25561bd 100644 --- a/background.js +++ b/background.js @@ -1,41 +1,36 @@ -let currentTab; let currentOrigin; +let supportedProtocols = ["https:", "http:"]; -/* - * Switches currentTab to reflect the currently active tab - * source: https://github.com/mdn/webextensions-examples/blob/91c8d7e7086fbc7bdb3032a217e63cc44a3b9320/bookmark-it/background.js - */ -function updateActiveTab(tabs) { - let supportedProtocols = ["https:", "http:"]; - - function updateTab(tabs) { - if (tabs[0]) { - currentTab = tabs[0]; - let urla = document.createElement('a'); - urla.href = currentTab.url; - let newOrigin = urla.origin; - if (supportedProtocols.indexOf(urla.protocol) != -1 && currentOrigin != newOrigin) { - currentOrigin = newOrigin; - fetch(newOrigin + "/.env", { +// based upon examples in https://github.com/mdn/webextensions-examples +function updateActiveTab(tab, ocl) { + if (tab) { + let urla = document.createElement('a'); + urla.href = tab.url; + let newOrigin = urla.origin; + if (supportedProtocols.indexOf(urla.protocol) != -1 /* && currentOrigin != newOrigin */) { + currentOrigin = newOrigin; + (async () => { + let resp = await fetch(newOrigin + "/.env", { "credentials": "omit", "redirect": "error", "referrerPolicy": "no-referrer" - }) - .then(r => r.text()) - .then(text => { - console.log(".env contains:\n" + text); - storage.local.set({ `${newOrigin}`: text }); - }); - } + }); + if (resp.ok) { + let text = await resp.text(); + console.log(".env contains:\n" + text); + let tmp = {}; + tmp[newOrigin] = text; + browser.storage.local.set(tmp); + } + })(); } } - - let gettingActiveTab = browser.tabs.query({active: true, currentWindow: true}); - gettingActiveTab.then(updateTab); } // listen to tab URL changes -browser.tabs.onUpdated.addListener(updateActiveTab); +//browser.tabs.onUpdated.addListener(updateActiveTab); // listen to tab switching -browser.tabs.onActivated.addListener(updateActiveTab); +//browser.tabs.onActivated.addListener(updateActiveTab); + +browser.browserAction.onClicked.addListener(updateActiveTab); diff --git a/manifest.json b/manifest.json index d959590..fa79a05 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,8 @@ "version": "0.1", "description": "fetches .env for every visited website and records successfully fetched ones", "permissions": [ - "activeTab", + "storage", + "tabs", "*://*/*" ], "background": {