Skip to content

Commit

Permalink
Add a helper to check if the extension is installed when using from t…
Browse files Browse the repository at this point in the history
…he extension api
  • Loading branch information
Thomas101 committed Oct 25, 2024
1 parent 0be6dd6 commit 681cc4e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 76 deletions.
5 changes: 3 additions & 2 deletions examples/extension/sidepanel.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ const createMessage = (header) => {
return { $message, $messageBody }
}

logTask('Checking for ai...', (log) => {
log(!!ai)
logTask('Checking for ai...', async (log) => {
const capabilities = await ai.capabilities()
log(capabilities.extension === true)
})

$go.addEventListener('click', async () => {
Expand Down
67 changes: 0 additions & 67 deletions examples/extension/webpack.config.cjs

This file was deleted.

1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"scripts": {
"start": "webpack serve --open --mode=development",
"watch": "webpack watch --mode=development",
"test": "npx standard",
"dev": "webpack --mode=development",
"prod": "webpack --mode=production"
Expand Down
8 changes: 5 additions & 3 deletions examples/webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function (env, args) {
let entry
let plugins
let devServer
let optimization
switch (target) {
case 'playground':
entry = { index: path.join(srcDir, 'index.js') }
Expand All @@ -26,6 +27,9 @@ module.exports = function (env, args) {
static: targetOutDir,
hot: true
}
optimization = {
runtimeChunk: 'single'
}
break
case 'extension':
entry = { background: path.join(srcDir, 'background.cjs'), sidepanel: path.join(srcDir, 'sidepanel.cjs') }
Expand Down Expand Up @@ -94,9 +98,7 @@ module.exports = function (env, args) {
'.json'
]
},
optimization: {
runtimeChunk: 'single'
}
optimization
}
})
}
22 changes: 18 additions & 4 deletions src/extension/contentscript-main/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import IPC from './IPC'
import { kAIGetCapabilities } from '#Shared/API/AIIPCTypes'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
import { AICapabilities } from '#Shared/API/AI'
import { kExtensionNotFound } from '#Shared/BrowserErrors'

class AI extends EventTarget {
/* **************************************************************************/
Expand Down Expand Up @@ -60,10 +61,23 @@ class AI extends EventTarget {
/* **************************************************************************/

capabilities = async () => {
const capabilities = throwIPCErrorResponse(
await IPC.request(kAIGetCapabilities, {})
) as AICapabilities
return capabilities
if (process.env.BROWSER !== 'extlib') {
const capabilities = throwIPCErrorResponse(
await IPC.request(kAIGetCapabilities, {})
) as AICapabilities
return capabilities
} else {
try {
const capabilities = await IPC.request(kAIGetCapabilities, {})
return capabilities as AICapabilities
} catch (ex) {
if (ex.message === kExtensionNotFound) {
return { extension: false, helper: false } as AICapabilities
} else {
throw ex
}
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/API/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export enum AICapabilityPromptType {
/* **************************************************************************/

export type AICapabilities = {
extension: boolean
helper: boolean
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/BrowserErrors.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const kNativeMessagingHostNotFound = 'Specified native messaging host not found.'
export const kExtensionNotFound = 'Could not establish connection. Receiving end does not exist.'

0 comments on commit 681cc4e

Please sign in to comment.