From a2b2ad44df5a3673cefe6ae1cccf3317b3f9c1ee Mon Sep 17 00:00:00 2001
From: Rain <Rainyan@users.noreply.github.com>
Date: Thu, 2 Nov 2023 17:18:41 +0000
Subject: [PATCH] Feat/toolbar (#10)

* Add toolbar button for focus on latest audible tab

Fix #5

For now, this is just a simple implementation of targeting the latest
audible tab, without any additional options.

This will allow users to have a big, easily reachable button for
accessing the addon's main function.

* Update README.md
---
 README.md            | 10 ++++++++++
 jump_to_audio_tab.js | 39 +++++++++++++++++++++++++++++++++++++++
 manifest.json        | 12 ++++++++++--
 3 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index cfffcda..91fe694 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,16 @@ Firefox add-on. Quickly jump to a tab that is making noise. Convenient if you ha
 * [Currently unsupported](https://github.com/Rainyan/jump-to-audible-tabs/issues/1), but if you are a developer you may be able to load it manually by following [the dev docs](https://developer.chrome.com/docs/extensions/), and substituting Chromium specific calls with Polyfill.
 
 ## Usage
+### Toolbar icon
+For Firefox, the main toolbar button is available from the *Extensions* button:
+
+![jat_example](https://github.com/Rainyan/jump-to-audible-tabs/assets/6595066/306a4860-dbde-4561-83f7-97e9192da620)
+
+From this list, you can pin it to your browser toolbar if you'd like. Pressing the button will focus on the latest audible tab.
+
+### Context menu
+For more control, the addon also offers a context menu with some additional options.
+
 Right-click on any browser page or a tab (Firefox only), and choose the corresponding action from the "Jump to Audible Tabs" context menu:
 
 ![Example image of the context menu](https://user-images.githubusercontent.com/6595066/234595280-1a239852-5c34-4db2-a8c3-8a4152e7c33f.png)
diff --git a/jump_to_audio_tab.js b/jump_to_audio_tab.js
index 8749ea3..1c5ad7f 100644
--- a/jump_to_audio_tab.js
+++ b/jump_to_audio_tab.js
@@ -10,6 +10,45 @@ function errorLog() {
   console.error(arguments);
 }
 
+browser.browserAction.onClicked.addListener((tab, info) => {
+  let targetTabs = AUDIBLE_TABS.sort((a, b) => b.lastAccessed - a.lastAccessed);
+  if (targetTabs.length === 0) {
+    return;
+  }
+  let targetTabId = targetTabs[0].id;
+  console.assert(targetTabId !== -1);
+
+  browser.tabs.update(targetTabId, { active: true }).then(() => {
+    browser.tabs
+      .get(targetTabId)
+      .catch((e) => {
+        console.error(e);
+      })
+      .then((tab) => {
+        browser.windows
+          .getAll()
+          .catch((e) => {
+            console.error(e);
+          })
+          .then((windows) => {
+            for (const [_, wnd] of Object.entries(windows)) {
+              if (wnd.id === tab.windowId) {
+                browser.windows
+                  .update(wnd.id, {
+                    drawAttention: true,
+                    focused: true,
+                  })
+                  .catch((e) => {
+                    console.error(e);
+                  });
+                break;
+              }
+            }
+          });
+      });
+  });
+});
+
 browser.contextMenus.onClicked.addListener((info, tab) => {
   if (!info.menuItemId.startsWith(EXT_TAB_ID)) {
     return;
diff --git a/manifest.json b/manifest.json
index e4b9ae5..17a51b0 100644
--- a/manifest.json
+++ b/manifest.json
@@ -1,7 +1,7 @@
 {
     "manifest_version":2,
     "name":"Jump to Audible Tabs",
-    "version":"0.1.2",
+    "version":"0.2.0",
     "description":"Jump to tabs that are playing audio. Activate with the mouse right-click context menu.",
     "icons":{
         "48":"icons/Volume_Mute.svg",
@@ -18,5 +18,13 @@
             "jump_to_audio_tab.js"
         ],
         "persistent":true
+    },
+
+    "browser_action": {
+        "default_icon": {
+            "16": "icons/Volume_Mute.svg",
+            "32": "icons/Volume_Mute.svg",
+            "64": "icons/Volume_Mute.svg"
+        }
     }
-}
\ No newline at end of file
+}