Skip to content

Commit

Permalink
keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
kendallcorner committed Feb 28, 2020
1 parent a33e358 commit 486072b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 19 deletions.
25 changes: 16 additions & 9 deletions src/js/background/backgroundLogic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const NUMBER_OF_KEYBOARD_SHORTCUTS = 2;
const DEFAULT_TAB = "about:newtab";
const backgroundLogic = {
NEW_TAB_PAGES: new Set([
Expand All @@ -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");
Expand Down Expand Up @@ -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"});
}
});

backgroundLogic.init();
25 changes: 24 additions & 1 deletion src/js/background/identityState.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const NUM_OF_KEYBOARD_SHORTCUTS = 2;

window.identityState = {
keyboardShortcut: {},
storageArea: {
area: browser.storage.local,

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +88,8 @@ window.identityState = {
}
}
}
}
},

},

_createTabObject(tab) {
Expand Down Expand Up @@ -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 =>
Expand Down
8 changes: 8 additions & 0 deletions src/js/background/messageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +91,7 @@ const messageHandler = {
);
break;
}
console.log(response)
return response;
});

Expand Down
48 changes: 46 additions & 2 deletions src/js/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const NUMBER_OF_KEYBOARD_SHORTCUTS = 2;

async function requestPermissions() {
const checkbox = document.querySelector("#bookmarksPermissions");
if (checkbox.checked) {
Expand All @@ -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) {
Expand All @@ -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);
}
12 changes: 6 additions & 6 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 13 additions & 1 deletion src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@
<input type="checkbox" id="syncCheck">
Enable Sync
</label>
<p>This setting allows you to sync your containers and site assignments across devices.</p>
<p>This setting allows you to sync your containers and site assignments across devices.</p>
<p><label>
Container to open with Keyboard Shortcut 0
<select id="open_container_0">
<!-- <option value="none">None</option> -->
</select>
</label></p>
<p><label>
Container to open with Keyboard Shortcut 1
<select id="open_container_1">
<!-- <option value="none">None</option> -->
</select>
</label></p>
</form>
<script src="js/options.js"></script>
</body>
Expand Down

0 comments on commit 486072b

Please sign in to comment.