-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen-ts-popup-window.ts
More file actions
72 lines (57 loc) · 1.6 KB
/
open-ts-popup-window.ts
File metadata and controls
72 lines (57 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {handleTlinkApiRequest, popupCenter} from "@/lib/handle-tlink-api.ts";
import {TokenscriptCardMetadata} from "@repo/tlinks/src";
export function openTsPopupWindow(dAppUrl: string, tsMetadata: TokenscriptCardMetadata){
let popup;
const handleMessage = async (event: MessageEvent) => {
// We only proxy messages that originate from the child iframe
if (!popup || event.source !== popup) {
return
}
console.log("Processing message from popup", event, popup);
if (event.data?.type === "TLINK_API_REQUEST") {
popup.postMessage(
{
type: "TLINK_API_RESPONSE",
source: "TLINK_API_RESPONSE",
data: {
uid: event.data.data.uid,
method: event.data.data.method,
response: await handleTlinkApiRequest(
event.data.data.method,
event.data.data.payload
)
}
},
"*"
)
}
if (event.data?.jsonrpc) {
const resp = await chrome.runtime.sendMessage({
type: "rpc",
data: event.data
})
sendResponse(event.data, resp)
}
function sendResponse(
messageData: MessageEvent["data"],
response: any | null
) {
const data = messageData
if (response?.error) {
data.error = response.error
} else {
data.result = response.result
}
popup!!.postMessage(
data,
"*"
)
}
}
const width = tsMetadata.fullScreen ? Math.round(screen.width * 0.9) : 550;
const height = tsMetadata.fullScreen ? Math.round(screen.height * 0.9) : 800;
popup = popupCenter(dAppUrl, "", width, height);
window.addEventListener("message", handleMessage);
if (!popup)
throw new Error("Failed to open the popup window");
}