From 486072bef82305187eed7b633a3aa6359097a195 Mon Sep 17 00:00:00 2001 From: Kendall Werts Date: Fri, 21 Feb 2020 09:26:08 -0600 Subject: [PATCH] keyboard shortcuts --- src/js/background/backgroundLogic.js | 25 +++++++++------ src/js/background/identityState.js | 25 ++++++++++++++- src/js/background/messageHandler.js | 8 +++++ src/js/options.js | 48 ++++++++++++++++++++++++++-- src/manifest.json | 12 +++---- src/options.html | 14 +++++++- 6 files changed, 113 insertions(+), 19 deletions(-) diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index 3d351bd9..996539fa 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -1,3 +1,4 @@ +const NUMBER_OF_KEYBOARD_SHORTCUTS = 2; const DEFAULT_TAB = "about:newtab"; const backgroundLogic = { NEW_TAB_PAGES: new Set([ @@ -7,6 +8,19 @@ const backgroundLogic = { "about:blank" ]), unhideQueue: [], + init() { + console.log("init") + browser.commands.onCommand.addListener(function (command) { + for (let i=0; i < NUMBER_OF_KEYBOARD_SHORTCUTS; i++) { + const key = "open_container_" + i; + const cookieStoreId = identityState.keyboardShortcut[key]; + console.log(cookieStoreId); + if (command === key) { + browser.tabs.create({cookieStoreId}); + } + } + }); + }, async getExtensionInfo() { const manifestPath = browser.extension.getURL("manifest.json"); @@ -334,12 +348,5 @@ const backgroundLogic = { } }; -browser.commands.onCommand.addListener(function (command) { - if (command === "open_container_2") { - browser.tabs.create({cookieStoreId: "firefox-container-2"}); - } - if (command === "open_container_1") { - console.log("Toggling the feature!"); - browser.tabs.create({cookieStoreId: "firefox-container-1"}); - } -}); \ No newline at end of file + +backgroundLogic.init(); \ No newline at end of file diff --git a/src/js/background/identityState.js b/src/js/background/identityState.js index 8c01eb79..afec9c5a 100644 --- a/src/js/background/identityState.js +++ b/src/js/background/identityState.js @@ -1,4 +1,7 @@ +const NUM_OF_KEYBOARD_SHORTCUTS = 2; + window.identityState = { + keyboardShortcut: {}, storageArea: { area: browser.storage.local, @@ -42,6 +45,19 @@ window.identityState = { return this.area.remove([storeKey]); }, + async setKeyboardShortcut(shortcutId, cookieStoreId) { + identityState.keyboardShortcut[shortcutId] = cookieStoreId; + return this.area.set({[shortcutId]: cookieStoreId}); + }, + + async loadKeyboardShortcuts () { + for (let i=0; i < NUM_OF_KEYBOARD_SHORTCUTS; i++) { + const key = "open_container_" + i; + const storageObject = await this.area.get(key); + identityState.keyboardShortcut[key] = storageObject[key]; + } + }, + /* * Looks for abandoned identity keys in local storage, and makes sure all * identities registered in the browser are also in local storage. (this @@ -72,7 +88,8 @@ window.identityState = { } } } - } + }, + }, _createTabObject(tab) { @@ -153,8 +170,14 @@ window.identityState = { macAddonUUID: uuidv4() }; }, + + init() { + this.storageArea.loadKeyboardShortcuts(); + } }; +identityState.init(); + function uuidv4() { // https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => diff --git a/src/js/background/messageHandler.js b/src/js/background/messageHandler.js index 3e93802c..fbd06a16 100644 --- a/src/js/background/messageHandler.js +++ b/src/js/background/messageHandler.js @@ -10,6 +10,13 @@ const messageHandler = { let response; switch (m.method) { + case "getShortcuts": + console.log(identityState.keyboardShortcut); + response = identityState.keyboardShortcut; + break; + case "setShortcut": + identityState.storageArea.setKeyboardShortcut(m.shortcut, m.cookieStoreId); + break; case "resetSync": response = sync.resetSync(); break; @@ -84,6 +91,7 @@ const messageHandler = { ); break; } + console.log(response) return response; }); diff --git a/src/js/options.js b/src/js/options.js index c225e63f..86d3a7ff 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -1,3 +1,5 @@ +const NUMBER_OF_KEYBOARD_SHORTCUTS = 2; + async function requestPermissions() { const checkbox = document.querySelector("#bookmarksPermissions"); if (checkbox.checked) { @@ -22,7 +24,8 @@ async function enableDisableSync() { browser.runtime.sendMessage({ method: "resetSync" }); } -async function restoreOptions() { +async function setupOptions() { + console.log("setup") const hasPermission = await browser.permissions.contains({permissions: ["bookmarks"]}); const { syncEnabled } = await browser.storage.local.get("syncEnabled"); if (hasPermission) { @@ -33,9 +36,50 @@ async function restoreOptions() { } else { document.querySelector("#syncCheck").checked = false; } + setupContainerShortcutSelects(); +} + +async function setupContainerShortcutSelects () { + const keyboardShortcut = browser.runtime.sendMessage({method: "getShortcuts"}); + console.log(keyboardShortcut); + const identities = await browser.contextualIdentities.query({}); + const fragment = document.createDocumentFragment(); + const noneOption = document.createElement("option"); + noneOption.value = "none"; + noneOption.textContent = "None"; + fragment.append(noneOption); + + for (const identity of identities) { + const option = document.createElement("option"); + option.value = identity.cookieStoreId; + option.id = identity.cookieStoreId; + option.textContent = identity.name; + fragment.append(option); + } + + for (let i=0; i < NUMBER_OF_KEYBOARD_SHORTCUTS; i++) { + const shortcutKey = "open_container_"+i; + const shortcutSelect = document.getElementById(shortcutKey); + shortcutSelect.appendChild(fragment.cloneNode(true)); + if (keyboardShortcut && keyboardShortcut[shortcutKey]) { + shortcutSelect.getElementById(keyboardShortcut[shortcutKey]).selected = true; + } + } } +function storeShortcutChoice (event) { + browser.runtime.sendMessage({ + method: "setShortcut", + shortcut: event.target.id, + cookieStoreId: event.target.value + }); +} -document.addEventListener("DOMContentLoaded", restoreOptions); +document.addEventListener("DOMContentLoaded", setupOptions); document.querySelector("#bookmarksPermissions").addEventListener( "change", requestPermissions); document.querySelector("#syncCheck").addEventListener( "change", enableDisableSync); + +for (let i=0; i < NUMBER_OF_KEYBOARD_SHORTCUTS; i++) { + document.querySelector("#open_container_"+i) + .addEventListener("change", storeShortcutChoice); +} \ No newline at end of file diff --git a/src/manifest.json b/src/manifest.json index ff55c8f4..76ffcada 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -40,17 +40,17 @@ }, "description": "Open containers panel" }, - "open_container_1": { + "open_container_0": { "suggested_key": { - "default": "Ctrl+Shift+1" + "default": "Ctrl+Shift+0" }, - "description": "Container Shortcut 1" + "description": "Container Shortcut 0" }, - "open_container_2": { + "open_container_1": { "suggested_key": { - "default": "Ctrl+Shift+2" + "default": "Ctrl+Shift+1" }, - "description": "Container Shortcut 2" + "description": "Container Shortcut 1" } }, "browser_action": { diff --git a/src/options.html b/src/options.html index 31a567ea..0476931c 100644 --- a/src/options.html +++ b/src/options.html @@ -16,7 +16,19 @@ Enable Sync -

This setting allows you to sync your containers and site assignments across devices.

+

This setting allows you to sync your containers and site assignments across devices.

+

+