Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions apps/extension/src/inject/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { deserializeError } from "serialize-error"
import { onMessage, sendMessage } from "webext-bridge/content-script"
import {
allowWindowMessaging,
onMessage,
sendMessage,
} from "webext-bridge/content-script"
import { runtime } from "webextension-polyfill"

allowWindowMessaging("pallad")

onMessage("pallad_event", async ({ data }) => {
const palladEvent = new CustomEvent("pallad_event", {
detail: data,
})
window.dispatchEvent(palladEvent)
return sendMessage("pallad_event", data, "window")
})

onMessage("rpc", async ({ data }) => {
const { method, ...payload } = data as { method: string; params: any[] }
const response = await sendMessage(method, payload, "background")
return response
})

const inject = () => {
Expand All @@ -15,34 +23,6 @@ const inject = () => {
script.src = runtime.getURL("/rpc.js")
script.type = "module"
document.documentElement.appendChild(script)
const channel = new BroadcastChannel("pallad")
channel.addEventListener("message", async ({ data }) => {
const origin = window.location.origin
const responseChannel = new BroadcastChannel(data.respondAt)
if (!data.isPallad)
return responseChannel.postMessage({ error: "Wrong context" })
let response
try {
const result: any = await sendMessage(
data.method,
{ params: data.params, context: { origin } },
"background",
)
if (result?.error) {
response = { jsonrpc: "2.0", error: deserializeError(result.error) }
} else {
response = { jsonrpc: "2.0", result }
}
return responseChannel.postMessage({
response,
})
} catch (error) {
response = { jsonrpc: "2.0", error }
return responseChannel.postMessage({
response,
})
}
})
}

inject()
27 changes: 14 additions & 13 deletions apps/extension/src/inject/rpc/provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import mitt from "mitt"
import { debouncedCall } from "./utils"
import { onMessage, sendMessage, setNamespace } from "webext-bridge/window"

setNamespace("pallad")

type EmitterEvents = {
connect: { chainId: string }
Expand All @@ -8,10 +10,8 @@ type EmitterEvents = {

const _events = mitt<EmitterEvents>()

window.addEventListener("pallad_event", (event) => {
event.stopImmediatePropagation()
const { detail } = event as CustomEvent
_events.emit(detail.type, detail.data)
onMessage("pallad_event", ({ data }) => {
_events.emit(data as never)
})

export const info = {
Expand All @@ -23,14 +23,15 @@ export const info = {

export const provider = {
_events,
request: async ({ method, params }: { method: string; params: any }) =>
await debouncedCall({
method,
params,
context: {
origin: window.location.origin,
},
}),
request: async ({ method, params }: { method: string; params: any }) => {
const context = { origin: window.location.origin }
const result = await sendMessage(
"rpc",
{ method, params, context },
"content-script",
)
return { jsonrpc: "2.0", result }
},
isPallad: true,
on: _events.on,
off: _events.off,
Expand Down
44 changes: 0 additions & 44 deletions apps/extension/src/inject/rpc/utils.ts

This file was deleted.

14 changes: 7 additions & 7 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading