keyboard shortcuts

This commit is contained in:
Kendall Werts 2020-02-21 09:26:08 -06:00
parent 313fe8892c
commit bd40dfbcfb
6 changed files with 113 additions and 19 deletions

View file

@ -1,3 +1,4 @@
const NUMBER_OF_KEYBOARD_SHORTCUTS = 2;
const DEFAULT_TAB = "about:newtab"; const DEFAULT_TAB = "about:newtab";
const backgroundLogic = { const backgroundLogic = {
NEW_TAB_PAGES: new Set([ NEW_TAB_PAGES: new Set([
@ -7,6 +8,19 @@ const backgroundLogic = {
"about:blank" "about:blank"
]), ]),
unhideQueue: [], 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() { async getExtensionInfo() {
const manifestPath = browser.extension.getURL("manifest.json"); const manifestPath = browser.extension.getURL("manifest.json");
@ -334,12 +348,5 @@ const backgroundLogic = {
} }
}; };
browser.commands.onCommand.addListener(function (command) {
if (command === "open_container_2") { backgroundLogic.init();
browser.tabs.create({cookieStoreId: "firefox-container-2"});
}
if (command === "open_container_1") {
console.log("Toggling the feature!");
browser.tabs.create({cookieStoreId: "firefox-container-1"});
}
});

View file

@ -1,4 +1,7 @@
const NUM_OF_KEYBOARD_SHORTCUTS = 2;
window.identityState = { window.identityState = {
keyboardShortcut: {},
storageArea: { storageArea: {
area: browser.storage.local, area: browser.storage.local,
@ -42,6 +45,19 @@ window.identityState = {
return this.area.remove([storeKey]); 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 * Looks for abandoned identity keys in local storage, and makes sure all
* identities registered in the browser are also in local storage. (this * identities registered in the browser are also in local storage. (this
@ -72,7 +88,8 @@ window.identityState = {
} }
} }
} }
} },
}, },
_createTabObject(tab) { _createTabObject(tab) {
@ -153,8 +170,14 @@ window.identityState = {
macAddonUUID: uuidv4() macAddonUUID: uuidv4()
}; };
}, },
init() {
this.storageArea.loadKeyboardShortcuts();
}
}; };
identityState.init();
function uuidv4() { function uuidv4() {
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript // https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>

View file

@ -10,6 +10,13 @@ const messageHandler = {
let response; let response;
switch (m.method) { 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": case "resetSync":
response = sync.resetSync(); response = sync.resetSync();
break; break;
@ -84,6 +91,7 @@ const messageHandler = {
); );
break; break;
} }
console.log(response)
return response; return response;
}); });

View file

@ -1,3 +1,5 @@
const NUMBER_OF_KEYBOARD_SHORTCUTS = 2;
async function requestPermissions() { async function requestPermissions() {
const checkbox = document.querySelector("#bookmarksPermissions"); const checkbox = document.querySelector("#bookmarksPermissions");
if (checkbox.checked) { if (checkbox.checked) {
@ -22,7 +24,8 @@ async function enableDisableSync() {
browser.runtime.sendMessage({ method: "resetSync" }); browser.runtime.sendMessage({ method: "resetSync" });
} }
async function restoreOptions() { async function setupOptions() {
console.log("setup")
const hasPermission = await browser.permissions.contains({permissions: ["bookmarks"]}); const hasPermission = await browser.permissions.contains({permissions: ["bookmarks"]});
const { syncEnabled } = await browser.storage.local.get("syncEnabled"); const { syncEnabled } = await browser.storage.local.get("syncEnabled");
if (hasPermission) { if (hasPermission) {
@ -33,9 +36,50 @@ async function restoreOptions() {
} else { } else {
document.querySelector("#syncCheck").checked = false; 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);
document.addEventListener("DOMContentLoaded", restoreOptions); 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", setupOptions);
document.querySelector("#bookmarksPermissions").addEventListener( "change", requestPermissions); document.querySelector("#bookmarksPermissions").addEventListener( "change", requestPermissions);
document.querySelector("#syncCheck").addEventListener( "change", enableDisableSync); document.querySelector("#syncCheck").addEventListener( "change", enableDisableSync);
for (let i=0; i < NUMBER_OF_KEYBOARD_SHORTCUTS; i++) {
document.querySelector("#open_container_"+i)
.addEventListener("change", storeShortcutChoice);
}

View file

@ -40,17 +40,17 @@
}, },
"description": "Open containers panel" "description": "Open containers panel"
}, },
"open_container_0": {
"suggested_key": {
"default": "Ctrl+Shift+0"
},
"description": "Container Shortcut 0"
},
"open_container_1": { "open_container_1": {
"suggested_key": { "suggested_key": {
"default": "Ctrl+Shift+1" "default": "Ctrl+Shift+1"
}, },
"description": "Container Shortcut 1" "description": "Container Shortcut 1"
},
"open_container_2": {
"suggested_key": {
"default": "Ctrl+Shift+2"
},
"description": "Container Shortcut 2"
} }
}, },
"browser_action": { "browser_action": {

View file

@ -17,6 +17,18 @@
Enable Sync Enable Sync
</label> </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> </form>
<script src="js/options.js"></script> <script src="js/options.js"></script>
</body> </body>