Fixed a bug in detecting manual changes of keyboard shortcuts

This commit is contained in:
Achiya Elyasaf
2023-11-19 15:35:15 +02:00
parent a640126c2c
commit 693f3f2bce
4 changed files with 31 additions and 15 deletions

View File

@@ -42,5 +42,5 @@
"manifest_version": 3, "manifest_version": 3,
"name": "LeafLLM", "name": "LeafLLM",
"homepage_url": "https://github.com/achiyae/LeafLLM", "homepage_url": "https://github.com/achiyae/LeafLLM",
"version": "1.2.1" "version": "1.2.2"
} }

View File

@@ -1,7 +1,6 @@
<html> <html>
<head> <head>
<script src="/scripts/jquery.js"></script> <script src="/scripts/jquery.js"></script>
<script src="/scripts/utils.js" type="module"></script>
<script src="/popup/popup.js" type="module"></script> <script src="/popup/popup.js" type="module"></script>
<link rel="stylesheet" href="/popup/popup.css"/> <link rel="stylesheet" href="/popup/popup.css"/>
</head> </head>

View File

@@ -17,17 +17,32 @@ async function refreshStorage() {
$('#api-token-form .api-token-status').text(chrome.runtime.lastError || !openAIAPIKey ? 'not set' : 'set') $('#api-token-form .api-token-status').text(chrome.runtime.lastError || !openAIAPIKey ? 'not set' : 'set')
}) })
const commands = await chrome.commands.getAll();
chrome.storage.local.get(['Improve', 'Complete', 'Ask']).then((settings) => { chrome.storage.local.get(['Improve', 'Complete', 'Ask']).then((settings) => {
Object.values(settings).forEach(setting => {
let command = commands.filter(({ name }) => name === setting.key)[0]
if(command.shortcut !== setting.shortcut) {
setting.shortcut = command.shortcut;
if(setting.status === 'enabled' && setting.shortcut === '') {
setting.status = 'error'
}
chrome.storage.local.set({ [setting.key]: setting });
} else if(setting.status === 'enabled' && setting.shortcut === '') {
setting.status = 'error'
chrome.storage.local.set({ [setting.key]: setting })
}
})
let bindingFailures = Object.values(settings) let bindingFailures = Object.values(settings)
.filter(({ status }) => status === 'error') .filter(({ status }) => status === 'error')
.map(({ key, shortcut }) => `${shortcut} for ${key}`) .map(({ key }) => `${key}`)
.join(', ') .join(', ');
if (bindingFailures.length > 0) { if (bindingFailures.length > 0) {
addErrorMessage(`Could not bind the following shortcuts:\n${bindingFailures}.\nYou can set it manually at <a href="chrome://extensions/shortcuts">chrome://extensions/shortcuts</a>.`) addErrorMessage(`Could not bind the following shortcuts:\n${bindingFailures}.\nYou can set it manually at <a href="chrome://extensions/shortcuts">chrome://extensions/shortcuts</a>.`)
} }
Object.values(settings).forEach(({ key, status, shortcut }) => { Object.values(settings).forEach(({ key, status, shortcut }) => {
$(`#settings-form input[name='text-${key}']:checkbox`).prop('checked', status === 'enabled') $(`#settings-form input[name='text-${key}']:checkbox`).prop('checked', status === 'enabled')
let shortcut2 = status === 'error' ? 'not set' : shortcut let shortcut2 = shortcut === '' ? 'not set' : shortcut
$(`#shortcut-${key}`).html(`<span>${shortcut2}</span>`) $(`#shortcut-${key}`).html(`<span>${shortcut2}</span>`)
}) })
}) })
@@ -64,7 +79,7 @@ async function handleAPITokenClear(event) {
.catch((error) => addErrorMessage(`Failed to remove API Token. Error: ${error}`)) .catch((error) => addErrorMessage(`Failed to remove API Token. Error: ${error}`))
} }
async function makeHandleSettingChange(key) { function makeHandleSettingChange(key) {
return async (event) => { return async (event) => {
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
@@ -72,10 +87,12 @@ async function makeHandleSettingChange(key) {
const value = event.target.checked const value = event.target.checked
const setting = await chrome.storage.local.get(key) const setting = await chrome.storage.local.get(key)
if (setting[key].status !== 'error') { /* let commandKey = await chrome.commands.getAll()
commandKey = commandKey.filter(({ name }) => name === key)[0]*/
// if (setting[key].status !== 'error') {
setting[key].status = value ? 'enabled' : 'disabled' setting[key].status = value ? 'enabled' : 'disabled'
await chrome.storage.local.set({ [key]: setting[key] }) await chrome.storage.local.set({ [key]: setting[key] })
} // }
return refreshStorage() return refreshStorage()
} }
} }

View File

@@ -20,13 +20,7 @@ function addListener(commandName) {
}) })
} }
chrome.runtime.onInstalled.addListener((reason) => { // Only use this function during the initial installation phase. After
if (reason.reason === chrome.runtime.OnInstalledReason.INSTALL) {
checkCommandShortcuts()
}
})
// Only use this function during the initial install phase. After
// installation the user may have intentionally unassigned commands. // installation the user may have intentionally unassigned commands.
// Example for install commands: [{"description":"","name":"_execute_action","shortcut":""},{"description":"Use the selected text to ask GPT. It adds to the beginning of the selected text: 'In Latex, '","name":"Ask","shortcut":""},{"description":"Complete selected text","name":"Complete","shortcut":""},{"description":"Improve selected text","name":"Improve","shortcut":""}] // Example for install commands: [{"description":"","name":"_execute_action","shortcut":""},{"description":"Use the selected text to ask GPT. It adds to the beginning of the selected text: 'In Latex, '","name":"Ask","shortcut":""},{"description":"Complete selected text","name":"Complete","shortcut":""},{"description":"Improve selected text","name":"Improve","shortcut":""}]
async function checkCommandShortcuts() { async function checkCommandShortcuts() {
@@ -45,6 +39,12 @@ async function checkCommandShortcuts() {
}) })
} }
chrome.runtime.onInstalled.addListener((reason) => {
if (reason.reason === chrome.runtime.OnInstalledReason.INSTALL) {
checkCommandShortcuts()
}
})
async function setup() { async function setup() {
addListener('Improve') addListener('Improve')
addListener('Complete') addListener('Complete')