Update version
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
},
|
},
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"js": ["scripts/jquery.js", "scripts/content.js", "scripts/utils.js"],
|
"js": ["scripts/jquery.js", "scripts/content.js"],
|
||||||
"matches": ["https://*.overleaf.com/project/*"]
|
"matches": ["https://*.overleaf.com/project/*"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -36,8 +36,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"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,
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
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) {
|
||||||
@@ -25,12 +23,11 @@ async function refreshStorage() {
|
|||||||
.map(({ key, shortcut }) => `${shortcut} for ${key}`)
|
.map(({ key, shortcut }) => `${shortcut} for ${key}`)
|
||||||
.join(', ')
|
.join(', ')
|
||||||
if (bindingFailures.length > 0) {
|
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>.`)
|
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 = status === 'error' ? 'not set' : shortcut
|
||||||
console.log(`writing ${shortcut2} to shortcut-${key}`)
|
|
||||||
$(`#shortcut-${key}`).html(`<span>${shortcut2}</span>`)
|
$(`#shortcut-${key}`).html(`<span>${shortcut2}</span>`)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -51,12 +48,9 @@ async function handleAPITokenSet(event) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
chrome.storage.local.set({ openAIAPIKey })
|
||||||
chrome.storage.local.set({ openAIAPIKey }).then(refreshStorage)
|
.then(refreshStorage)
|
||||||
} catch (error) {
|
.catch((error) => addErrorMessage(`Failed to remove API Token. Error: ${error}`))
|
||||||
addErrorMessage('Failed to set API Token.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleAPITokenClear(event) {
|
async function handleAPITokenClear(event) {
|
||||||
@@ -65,29 +59,24 @@ async function handleAPITokenClear(event) {
|
|||||||
|
|
||||||
clearMessages()
|
clearMessages()
|
||||||
|
|
||||||
try {
|
chrome.storage.local.remove('openAIAPIKey')
|
||||||
chrome.storage.local.remove('openAIAPIKey').then(refreshStorage)
|
.then(refreshStorage)
|
||||||
} catch (error) {
|
.catch((error) => addErrorMessage(`Failed to remove API Token. Error: ${error}`))
|
||||||
console.log(error)
|
|
||||||
addErrorMessage('Failed to remove API Token.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeHandleSettingChange(key) {
|
async function makeHandleSettingChange(key) {
|
||||||
return async (event) => {
|
return async (event) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
clearMessages()
|
clearMessages()
|
||||||
|
|
||||||
const value = event.target.checked
|
const value = event.target.checked
|
||||||
chrome.storage.local.get(key).then(setting => {
|
const setting = await chrome.storage.local.get(key)
|
||||||
if(setting[key].status !== 'error') {
|
if (setting[key].status !== 'error') {
|
||||||
setting[key].status = value ? 'enabled' : 'disabled'
|
setting[key].status = value ? 'enabled' : 'disabled'
|
||||||
setSetting(setting[key].key, setting[key])
|
await chrome.storage.local.set({ [key]: setting[key] })
|
||||||
}
|
}
|
||||||
return refreshStorage()
|
return refreshStorage()
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,5 +88,5 @@ $(document).ready(async function () {
|
|||||||
commands.forEach((key) => {
|
commands.forEach((key) => {
|
||||||
$(`#settings-form input[name='text-${key}']:checkbox`).on('change', makeHandleSettingChange(key))
|
$(`#settings-form input[name='text-${key}']:checkbox`).on('change', makeHandleSettingChange(key))
|
||||||
})
|
})
|
||||||
await refreshStorage()
|
return refreshStorage()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -39,9 +39,8 @@ class OpenAIAPI {
|
|||||||
temperature: 0.5
|
temperature: 0.5
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.query('completions', data)
|
return this.query('completions', data)
|
||||||
|
.then(result => result[0].text)
|
||||||
return result[0].text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async improveText(text) {
|
async improveText(text) {
|
||||||
@@ -54,9 +53,8 @@ class OpenAIAPI {
|
|||||||
temperature: 0.5
|
temperature: 0.5
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.query('edits', data)
|
return this.query('edits', data)
|
||||||
|
.then(result => result[0].text)
|
||||||
return result[0].text
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,14 +68,10 @@ function replaceSelectedText(replacementText, selection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function settingIsEnabled(setting) {
|
async function settingIsEnabled(key) {
|
||||||
let result
|
return chrome.storage.local.get(key)
|
||||||
try {
|
.then(setting => 'enabled' === setting[key].status)
|
||||||
result = await chrome.storage.local.get(setting)
|
.catch(() => false)
|
||||||
} catch (error) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return result[setting]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function commentText(text) {
|
function commentText(text) {
|
||||||
@@ -91,7 +85,7 @@ function commentText(text) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function improveTextHandler(openAI) {
|
async function improveTextHandler(openAI) {
|
||||||
if (!(await settingIsEnabled('textImprovement'))) throw new Error('Text improvement is not enabled.')
|
if (!(await settingIsEnabled('Improve'))) throw new Error('Text improvement is not enabled.')
|
||||||
const selection = window.getSelection()
|
const selection = window.getSelection()
|
||||||
const selectedText = selection.toString()
|
const selectedText = selection.toString()
|
||||||
if (!selectedText) return
|
if (!selectedText) return
|
||||||
@@ -101,7 +95,7 @@ async function improveTextHandler(openAI) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function completeTextHandler(openAI) {
|
async function completeTextHandler(openAI) {
|
||||||
if (!(await settingIsEnabled('textCompletion'))) throw new Error('Text completion is not enabled.')
|
if (!(await settingIsEnabled('Complete'))) throw new Error('Text completion is not enabled.')
|
||||||
const selection = window.getSelection()
|
const selection = window.getSelection()
|
||||||
const selectedText = selection.toString()
|
const selectedText = selection.toString()
|
||||||
if (!selectedText) return
|
if (!selectedText) return
|
||||||
@@ -110,7 +104,7 @@ async function completeTextHandler(openAI) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function askHandler(openAI) {
|
async function askHandler(openAI) {
|
||||||
if (!(await settingIsEnabled('textAsk'))) throw new Error('Text ask is not enabled.')
|
if (!(await settingIsEnabled('Ask'))) throw new Error('Ask is not enabled.')
|
||||||
const selection = window.getSelection()
|
const selection = window.getSelection()
|
||||||
const selectedText = selection.toString()
|
const selectedText = selection.toString()
|
||||||
if (!selectedText) return
|
if (!selectedText) return
|
||||||
@@ -130,28 +124,38 @@ function setAPIKey(key) {
|
|||||||
currentAPIKey = key
|
currentAPIKey = key
|
||||||
if (currentAPIKey) {
|
if (currentAPIKey) {
|
||||||
openAI = new OpenAIAPI(currentAPIKey)
|
openAI = new OpenAIAPI(currentAPIKey)
|
||||||
console.log('LeafLLM: OpenAI API key set, enabling LeafLLM features.')
|
log('OpenAI API key set, enabling LeafLLM features.')
|
||||||
} else {
|
} else {
|
||||||
openAI = undefined
|
openAI = undefined
|
||||||
console.log('LeafLLM: OpenAI API key is not set, LeafLLM features are disabled.')
|
log('OpenAI API key is not set, LeafLLM features are disabled.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleCommand(command) {
|
function handleCommand(command) {
|
||||||
if (command === 'Improve') {
|
if (command === 'Improve') {
|
||||||
improveTextHandler(openAI).catch(e => error(`Failed to execute the '${command}' command. Error message: ${e}`))
|
improveTextHandler(openAI).catch(e => error(`Failed to execute the '${command}' command.`, e))
|
||||||
} else if (command === 'Complete') {
|
} else if (command === 'Complete') {
|
||||||
completeTextHandler(openAI).catch(e => error(`Failed to execute the '${command}' command. Error message: ${e}`))
|
completeTextHandler(openAI).catch(e => error(`Failed to execute the '${command}' command.`, e))
|
||||||
} else if (command === 'Ask') {
|
} else if (command === 'Ask') {
|
||||||
askHandler(openAI).catch(e => error(`Failed to execute the '${command}' command. Error message: ${e}`))
|
askHandler(openAI).catch(e => error(`Failed to execute the '${command}' command.`, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function error(msg) {
|
function error(msg, error) {
|
||||||
|
if(error) {
|
||||||
|
msg += ` Error message: ${error.message}`
|
||||||
|
if(error.cause) {
|
||||||
|
console.error(`\nCause: ${JSON.stringify(error.cause)}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
customAlert(msg)
|
customAlert(msg)
|
||||||
console.error(`LeafLLM: ${msg}`)
|
console.error(`LeafLLM: ${msg}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function log(msg) {
|
||||||
|
console.log(`LeafLLM: ${msg}`)
|
||||||
|
}
|
||||||
|
|
||||||
function customAlert(msg,duration)
|
function customAlert(msg,duration)
|
||||||
{
|
{
|
||||||
if(!duration) duration = 4000;
|
if(!duration) duration = 4000;
|
||||||
@@ -168,7 +172,7 @@ function customAlert(msg,duration)
|
|||||||
|
|
||||||
chrome.runtime.onMessage.addListener(
|
chrome.runtime.onMessage.addListener(
|
||||||
function (request, sender, sendResponse) {
|
function (request, sender, sendResponse) {
|
||||||
console.log(`Received request: ${JSON.stringify(request)}`)
|
log(`Received request: ${JSON.stringify(request)}`)
|
||||||
if (request.command === 'setup') {
|
if (request.command === 'setup') {
|
||||||
setAPIKey(request.apiKey)
|
setAPIKey(request.apiKey)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
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' },
|
||||||
@@ -9,7 +7,7 @@ const settings = [
|
|||||||
async function sendMessage(message) {
|
async function sendMessage(message) {
|
||||||
const [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true })
|
const [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true })
|
||||||
if (tab == null || tab.url?.startsWith('chrome://')) return undefined
|
if (tab == null || tab.url?.startsWith('chrome://')) return undefined
|
||||||
const response = await chrome.tabs.sendMessage(tab.id, message)
|
chrome.tabs.sendMessage(tab.id, message)
|
||||||
// do something with response here, not outside the function
|
// do something with response here, not outside the function
|
||||||
// console.log(response)
|
// console.log(response)
|
||||||
}
|
}
|
||||||
@@ -41,7 +39,7 @@ async function checkCommandShortcuts() {
|
|||||||
if (shortcut === '') {
|
if (shortcut === '') {
|
||||||
command.status = 'error'
|
command.status = 'error'
|
||||||
}
|
}
|
||||||
setSetting(command.key, command)
|
chrome.storage.local.set({ [command.key]: command })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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