phase 2
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
},
|
},
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"js": ["scripts/jquery.js", "scripts/content.js"],
|
"js": ["scripts/jquery.js", "scripts/content.js", "scripts/utils.js"],
|
||||||
"matches": ["https://*.overleaf.com/project/*"]
|
"matches": ["https://*.overleaf.com/project/*"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -36,11 +36,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"background": {
|
"background": {
|
||||||
"service_worker": "scripts/service-worker.js"
|
"service_worker": "scripts/service-worker.js",
|
||||||
|
"type": "module"
|
||||||
},
|
},
|
||||||
"permissions": ["storage", "tabs"],
|
"permissions": ["storage", "tabs"],
|
||||||
"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.1.0"
|
"version": "1.2.0"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<script src="/scripts/jquery.js"></script>
|
<script src="/scripts/jquery.js"></script>
|
||||||
<script src="/popup/popup.js"></script>
|
<script src="/scripts/utils.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>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import {setSetting} from '../scripts/utils.js'
|
||||||
|
|
||||||
const apiKeyRegex = /sk-[a-zA-Z0-9]{48}/
|
const apiKeyRegex = /sk-[a-zA-Z0-9]{48}/
|
||||||
|
|
||||||
function addMessage(message) {
|
function addMessage(message) {
|
||||||
@@ -12,42 +14,23 @@ function clearMessages() {
|
|||||||
$('#message-box').empty()
|
$('#message-box').empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a setting in storage {@link https://developer.chrome.com/docs/extensions/reference/storage/#type-StorageArea:~:text=to%20the%20callback.-,set,-void}
|
|
||||||
* @param key
|
|
||||||
* @param value
|
|
||||||
* @param callback
|
|
||||||
*/
|
|
||||||
async function setSetting(key, value, callback = null) {
|
|
||||||
let obj = {}
|
|
||||||
obj[key] = value
|
|
||||||
chrome.storage.local.set(obj, callback).catch(error => {
|
|
||||||
console.log(`Failed to set ${key} setting. Error: ${error}`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a setting from storage
|
|
||||||
* @param {string | string[] | object} [keys=null] - The keys to get (see {@link https://developer.chrome.com/docs/extensions/reference/storage/#usage})
|
|
||||||
* @param {function} [callback=null] - Callback function
|
|
||||||
*/
|
|
||||||
async function getSetting(keys = null, callback = null) {
|
|
||||||
return chrome.storage.local.get(keys, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function refreshStorage() {
|
async function refreshStorage() {
|
||||||
getSetting('openAIAPIKey').then(({ openAIAPIKey }) => {
|
chrome.storage.local.get('openAIAPIKey').then(({ openAIAPIKey }) => {
|
||||||
$('#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')
|
||||||
})
|
})
|
||||||
|
|
||||||
getSetting(['Improve', 'Complete', 'Ask']).then(settings =>
|
chrome.storage.local.get(['Improve', 'Complete', 'Ask']).then((settings) => {
|
||||||
settings.forEach(({ key, shortcut, status }) => {
|
let bindingFailures = Object.values(settings)
|
||||||
if (status === 'error') {
|
.filter(({ status }) => status === 'error')
|
||||||
addErrorMessage(`${shortcut} could not be bound for the ${key} command. You can set it manually at chrome://extensions/shortcuts.`)
|
.map(({ key, shortcut }) => `${shortcut} for ${key}`)
|
||||||
|
.join(', ')
|
||||||
|
if (bindingFailures.length > 0) {
|
||||||
|
addErrorMessage(`Could not bound the following shortcuts:\n${bindingFailures}.\nYou can set it manually at <a href="chrome://extensions/shortcuts">chrome://extensions/shortcuts</a>.`)
|
||||||
}
|
}
|
||||||
$(`#settings-form input[name='text-${key}']:checkbox`).prop('checked', status === 'checked')
|
Object.values(settings).forEach(({ key, status }) => {
|
||||||
}))
|
$(`#settings-form input[name='text-${key}']:checkbox`).prop('checked', status === 'enabled')
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleAPITokenSet(event) {
|
async function handleAPITokenSet(event) {
|
||||||
@@ -66,14 +49,12 @@ async function handleAPITokenSet(event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await chrome.storage.local.set({ openAIAPIKey })
|
chrome.storage.local.set({ openAIAPIKey }).then(refreshStorage)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
addErrorMessage('Failed to set API Token.')
|
addErrorMessage('Failed to set API Token.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await refreshStorage()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleAPITokenClear(event) {
|
async function handleAPITokenClear(event) {
|
||||||
@@ -83,14 +64,12 @@ async function handleAPITokenClear(event) {
|
|||||||
clearMessages()
|
clearMessages()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await chrome.storage.local.remove('openAIAPIKey')
|
chrome.storage.local.remove('openAIAPIKey').then(refreshStorage)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
addErrorMessage('Failed to remove API Token.')
|
addErrorMessage('Failed to remove API Token.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await refreshStorage()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeHandleSettingChange(key) {
|
function makeHandleSettingChange(key) {
|
||||||
@@ -100,11 +79,13 @@ function makeHandleSettingChange(key) {
|
|||||||
clearMessages()
|
clearMessages()
|
||||||
|
|
||||||
const value = event.target.checked
|
const value = event.target.checked
|
||||||
getSetting(key).then(setting => {
|
chrome.storage.local.get(key).then(setting => {
|
||||||
setting.status = value ? 'checked' : 'unchecked'
|
if(setting[key].status !== 'error') {
|
||||||
setSetting(setting.key, setting)
|
setting[key].status = value ? 'enabled' : 'disabled'
|
||||||
|
setSetting(setting[key].key, setting[key])
|
||||||
|
}
|
||||||
|
return refreshStorage()
|
||||||
})
|
})
|
||||||
// await refreshStorage()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,8 +93,9 @@ $(document).ready(async function () {
|
|||||||
$('#api-token-form .submit').on('click', handleAPITokenSet)
|
$('#api-token-form .submit').on('click', handleAPITokenSet)
|
||||||
$('#api-token-form .clear').on('click', handleAPITokenClear)
|
$('#api-token-form .clear').on('click', handleAPITokenClear)
|
||||||
|
|
||||||
getSetting(['Improve', 'Complete', 'Ask']).forEach(key => {
|
let commands = ['Improve', 'Complete', 'Ask']
|
||||||
$(`#settings-form input[name='text-${name}']:checkbox`).on('change', makeHandleSettingChange(key))
|
commands.forEach((key) => {
|
||||||
|
$(`#settings-form input[name='text-${key}']:checkbox`).on('change', makeHandleSettingChange(key))
|
||||||
})
|
})
|
||||||
return refreshStorage()
|
await refreshStorage()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
class OpenAIAPI {
|
class OpenAIAPI {
|
||||||
static defaultModel = 'text-davinci-003'
|
static defaultModel = 'text-davinci-003'
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import {setSetting} from './utils.js'
|
||||||
|
|
||||||
const settings = [
|
const settings = [
|
||||||
{ key: 'Complete', shortcut: 'Alt+C', status: 'enabled', type: 'Command' },
|
{ key: 'Complete', shortcut: 'Alt+C', status: 'enabled', type: 'Command' },
|
||||||
{ key: 'Improve', shortcut: 'Alt+I', status: 'enabled', type: 'Command' },
|
{ key: 'Improve', shortcut: 'Alt+I', status: 'enabled', type: 'Command' },
|
||||||
@@ -45,29 +47,6 @@ async function checkCommandShortcuts() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a setting in storage {@link https://developer.chrome.com/docs/extensions/reference/storage/#type-StorageArea:~:text=to%20the%20callback.-,set,-void}
|
|
||||||
* @param key
|
|
||||||
* @param value
|
|
||||||
* @param callback
|
|
||||||
*/
|
|
||||||
async function setSetting(key, value, callback = null) {
|
|
||||||
let obj = {}
|
|
||||||
obj[key] = value
|
|
||||||
chrome.storage.local.set(obj, callback).catch(error => {
|
|
||||||
console.log(`Failed to set ${key} setting. Error: ${error}`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a setting from storage
|
|
||||||
* @param {string | string[] | object} [keys=null] - The keys to get (see {@link https://developer.chrome.com/docs/extensions/reference/storage/#usage})
|
|
||||||
* @param {function} [callback=null] - Callback function
|
|
||||||
*/
|
|
||||||
async function getSetting(keys = null, callback = null) {
|
|
||||||
return chrome.storage.local.get(keys, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function setup() {
|
async function setup() {
|
||||||
addListener('Improve')
|
addListener('Improve')
|
||||||
addListener('Complete')
|
addListener('Complete')
|
||||||
|
|||||||
32
scripts/utils.js
Normal file
32
scripts/utils.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Set a setting in storage {@link https://developer.chrome.com/docs/extensions/reference/storage/#type-StorageArea:~:text=to%20the%20callback.-,set,-void}
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
* @param {function}[callback] Optional callback function
|
||||||
|
*/
|
||||||
|
export async function setSetting(key, value, callback) {
|
||||||
|
let obj = {}
|
||||||
|
obj[key] = value
|
||||||
|
if (typeof callback === 'undefined') {
|
||||||
|
chrome.storage.local.set(obj).catch(error => {
|
||||||
|
console.log(`Failed to set ${key} setting. Error: ${error}`)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
chrome.storage.local.set(obj, callback).catch(error => {
|
||||||
|
console.log(`Failed to set ${key} setting. Error: ${error}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a setting from storage
|
||||||
|
* @param {string | string[] | object} [keys=null] - The keys to get (see {@link https://developer.chrome.com/docs/extensions/reference/storage/#usage})
|
||||||
|
* @param {function}[callback] Optional callback function
|
||||||
|
*/
|
||||||
|
export async function getSetting(keys = null, callback) {
|
||||||
|
if (typeof callback === 'undefined') {
|
||||||
|
return chrome.storage.local.get(keys, (items) => Object.values(items))
|
||||||
|
}else {
|
||||||
|
return chrome.storage.local.get(keys, (items) => callback(Object.values(items)))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user