-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle-tlink-api.ts
More file actions
67 lines (50 loc) · 2.14 KB
/
handle-tlink-api.ts
File metadata and controls
67 lines (50 loc) · 2.14 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
import { getTwitterHandle } from "@/lib/get-twitter-handle"
import {TS_VIEWER_URL} from "@repo/tlinks/utils";
export async function handleTlinkApiRequest(method: string, payload: any) {
switch (method) {
case "getTlinkContext":
return {
handle: getTwitterHandle(),
API_KEY:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcm9qZWN0IjoibXVsdGktY2hhbm5lbC1yZW5kZXJpbmctdGxpbmsiLCJpYXQiOjE3MjcxNzE1MDl9.9864en0XJbVgzACE_gHrQ00mr6fgctNRXWZ0Nex3DcQ"
}
case "getTurnstileToken":
case "getRecaptchaToken":
return await handleTlinkApiViaTSViewerWindow(method, payload);
}
return null
}
async function handleTlinkApiViaTSViewerWindow(method: string, payload: any){
return new Promise(async (resolve, reject) => {
let popup: WindowProxy|null;
let closedTimer;
const requestUrl = `${TS_VIEWER_URL}?viewType=tlink-api&method=${method}&payload=${encodeURIComponent(JSON.stringify(payload))}`
function handleMessage(event: MessageEvent){
if (event.source !== popup || event.data.type !== "TLINK_API_RESPONSE") {
return
}
if (closedTimer)
clearInterval(closedTimer)
if (event.data.response){
resolve(event.data.response);
} else {
reject(new Error(event.data.error));
}
popup?.close();
}
window.addEventListener("message", handleMessage);
popup = popupCenter(requestUrl, "TLink Request", 400, 400);
if (!popup)
reject(new Error("Failed to open the popup window"));
closedTimer = setInterval(() => {
if (!popup || popup.closed){
reject(new Error("Popup closed"));
}
}, 1000)
});
}
export function popupCenter(url: string, title: string, w: number, h: number) {
const left = (screen.width/2)-(w/2);
const top = (screen.height/2)-(h/2);
return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}