Skip to content

Commit

Permalink
Locally stored plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cattn committed Sep 16, 2024
1 parent 54080dc commit dfe9e53
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"clsx": "^2.1.1",
"events": "^3.3.0",
"idb": "^8.0.0",
"idb-keyval": "^6.2.1",
"lucide-react": "^0.298.0",
"million": "^2.6.4",
"nanostores": "^0.9.5",
Expand Down
24 changes: 24 additions & 0 deletions src/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import Viewer from '@/internal/Viewer'
import Status from '@/internal/Status'
import gba from '@/internal/GBA'
import Updater from '@/internal/Updater'
import bunker from '@/lib/bunker'
import { get, set } from 'idb-keyval';

export function registerDefaultPlugins() {
registerPlugin(Status)
Expand All @@ -70,12 +72,30 @@ export function removePlugin(id: string) {
}

getSavedPlugins().forEach(async (url) => {
if (bunker.pluginLocation == 'internal') {
get(url).then(async (value) => {
if (!value) return
const path = URL.createObjectURL(value)
const module = await import(/* @vite-ignore */ path)
const plugin = module.default as Plugin
registerPlugin({
...plugin,
source: url
})

console.log("Loaded plugin from storage.")
})
} else {
const loadedPlugin = await fetchExternalPlugin(url)
if (!loadedPlugin) return
registerPlugin({
...loadedPlugin,
source: url
})

console.log("Loaded plugin from URL")
}

})

export function registerPlugin(plugin: Plugin): Plugin | undefined | void {
Expand All @@ -100,6 +120,10 @@ export async function fetchExternalPlugin(url: string): Promise<Plugin | undefin
const code = await response.text()
const blob = new Blob([code], { type: 'text/javascript' })
const path = URL.createObjectURL(blob)
if (bunker.pluginLocation == 'internal') {
set(url, blob);
}


const module = await import(/* @vite-ignore */ path)

Expand Down

0 comments on commit dfe9e53

Please sign in to comment.