fix: iframe communication
This commit is contained in:
@@ -31,10 +31,14 @@
|
|||||||
<script>
|
<script>
|
||||||
;(() => {
|
;(() => {
|
||||||
const bridge = {{ bridge | tojson | safe }}
|
const bridge = {{ bridge | tojson | safe }}
|
||||||
const frame = document.getElementById('lnbits-wasm-extension-frame')
|
|
||||||
|
|
||||||
function sendResponse(id, payload) {
|
function extensionFrameWindow() {
|
||||||
frame.contentWindow?.postMessage(
|
return document.getElementById('lnbits-wasm-extension-frame')
|
||||||
|
?.contentWindow
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendResponse(targetWindow, id, payload) {
|
||||||
|
targetWindow?.postMessage(
|
||||||
{
|
{
|
||||||
type: 'lnbits-extension:response',
|
type: 'lnbits-extension:response',
|
||||||
id,
|
id,
|
||||||
@@ -112,13 +116,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener('message', async event => {
|
window.addEventListener('message', async event => {
|
||||||
if (event.source !== frame.contentWindow) return
|
if (event.source !== extensionFrameWindow()) return
|
||||||
const message = event.data
|
const message = event.data
|
||||||
if (!message || message.type !== 'lnbits-extension:request') return
|
if (!message || message.type !== 'lnbits-extension:request') return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (message.action === 'context') {
|
if (message.action === 'context') {
|
||||||
sendResponse(message.id, {
|
sendResponse(event.source, message.id, {
|
||||||
ok: true,
|
ok: true,
|
||||||
data: {
|
data: {
|
||||||
extensionId: bridge.extensionId,
|
extensionId: bridge.extensionId,
|
||||||
@@ -131,7 +135,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (message.action === 'api') {
|
if (message.action === 'api') {
|
||||||
sendResponse(message.id, {
|
sendResponse(event.source, message.id, {
|
||||||
ok: true,
|
ok: true,
|
||||||
data: await callApi(message)
|
data: await callApi(message)
|
||||||
})
|
})
|
||||||
@@ -140,13 +144,16 @@
|
|||||||
|
|
||||||
if (message.action === 'ui.notify') {
|
if (message.action === 'ui.notify') {
|
||||||
notify(message)
|
notify(message)
|
||||||
sendResponse(message.id, {ok: true, data: {ok: true}})
|
sendResponse(event.source, message.id, {
|
||||||
|
ok: true,
|
||||||
|
data: {ok: true}
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('Unknown extension bridge action.')
|
throw new Error('Unknown extension bridge action.')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
sendResponse(message.id, {
|
sendResponse(event.source, message.id, {
|
||||||
ok: false,
|
ok: false,
|
||||||
error: error instanceof Error ? error.message : String(error)
|
error: error instanceof Error ? error.message : String(error)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user