Skip to content

Commit 3233c50

Browse files
committed
fix: Cleanup redundant code, update TS viewer URLs & fix types
1 parent a3a5fad commit 3233c50

File tree

4 files changed

+9
-67
lines changed

4 files changed

+9
-67
lines changed

apps/extension/entrypoints/background.ts

-63
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {getTwitterHandle} from "@/lib/get-twitter-handle.ts";
21

32
export default defineBackground(() => {
43
// never mark the function here async
@@ -7,20 +6,6 @@ export default defineBackground(() => {
76
return null
87
}
98

10-
if (msg.type === "TLINK_API_REQUEST"){
11-
12-
console.log("Handling TLink API request from background: ", msg);
13-
14-
handleTlinkApiRequest(msg.method, msg.payload).then((res) => {
15-
sendResponse(res);
16-
}).catch((err) => {
17-
// TODO: Error handling
18-
console.error("error handling message", err, msg);
19-
})
20-
21-
return true;
22-
}
23-
249
let rpcMethod: string = ""
2510
let params: any = []
2611

@@ -59,54 +44,6 @@ export default defineBackground(() => {
5944
return true
6045
})
6146

62-
async function handleTlinkApiRequest(method: string, payload: any) {
63-
switch (method) {
64-
case "getTurnstileToken":
65-
case "getRecaptchaToken":
66-
return handleTlinkApiViaTSViewerWindow(method, payload);
67-
}
68-
}
69-
70-
async function handleTlinkApiViaTSViewerWindow(method: string, payload: any){
71-
72-
return new Promise(async (resolve, reject) => {
73-
74-
let popup: WindowProxy|null;
75-
76-
const requestUrl = `http://localhost:3333/?viewType=tlink-api&method=${method}&payload=${encodeURIComponent(JSON.stringify(payload))}`
77-
78-
function handleMessage(event: MessageEvent){
79-
if (event.source !== popup) {
80-
return
81-
}
82-
83-
popup!.onclose = null;
84-
85-
resolve(event.data);
86-
}
87-
88-
window.addEventListener("message", handleMessage);
89-
90-
91-
//popup = window.open(requestUrl, "", 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550px,height=800px`');
92-
popup = await chrome.windows.create({
93-
url: requestUrl,
94-
type: 'popup',
95-
focused: true,
96-
setSelfAsOpener: true
97-
// incognito, top, left, ...
98-
}) as WindowProxy;
99-
100-
if (!popup)
101-
reject("Failed to open the popup window");
102-
103-
popup!.onclose = () => {
104-
reject("Popup closed");
105-
}
106-
107-
});
108-
}
109-
11047
async function handleWalletCommunication({
11148
tabId,
11249
rpcMethod,

apps/extension/lib/handle-tlink-api.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getTwitterHandle } from "@/lib/get-twitter-handle"
2+
import {TS_VIEWER_URL} from "@repo/tlinks/utils";
23

34
export async function handleTlinkApiRequest(method: string, payload: any) {
45
switch (method) {
@@ -23,7 +24,7 @@ async function handleTlinkApiViaTSViewerWindow(method: string, payload: any){
2324
let popup: WindowProxy|null;
2425
let closedTimer;
2526

26-
const requestUrl = `http://localhost:3333/?viewType=tlink-api&method=${method}&payload=${encodeURIComponent(JSON.stringify(payload))}`
27+
const requestUrl = `${TS_VIEWER_URL}?viewType=tlink-api&method=${method}&payload=${encodeURIComponent(JSON.stringify(payload))}`
2728

2829
function handleMessage(event: MessageEvent){
2930

@@ -59,7 +60,7 @@ async function handleTlinkApiViaTSViewerWindow(method: string, payload: any){
5960
});
6061
}
6162

62-
function popupCenter (url, title, w, h) {
63+
function popupCenter(url: string, title: string, w: number, h: number) {
6364
const left = (screen.width/2)-(w/2);
6465
const top = (screen.height/2)-(h/2);
6566
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);

packages/tlinks/src/utils/build-ts-ifram-url.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {TS_VIEWER_URL} from "./constants.ts";
2+
13
export const buildTsIframeUrl = ({
24
chainId,
35
contract,
@@ -26,7 +28,7 @@ export const buildTsIframeUrl = ({
2628
if (tokenId) dAppUrlParams.append('tokenId', tokenId);
2729
if (scriptId) dAppUrlParams.append('scriptId', scriptId);
2830

29-
return `https://viewer-staging.tokenscript.org/?${dAppUrlParams.toString()}${
31+
return `${TS_VIEWER_URL}?${dAppUrlParams.toString()}${
3032
(hash || '').length > 1 ? hash : ''
3133
}`;
3234
};

packages/tlinks/src/utils/constants.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,6 @@ export class SimpleActionComponent extends AbstractActionComponent {
116116
account,
117117
};
118118
}
119-
}
119+
}
120+
121+
export const TS_VIEWER_URL = "https://viewer.tokenscript.org/";

0 commit comments

Comments
 (0)