Skip to content

Commit 38603fa

Browse files
committed
fix: implement executescript in browser api
1 parent e940a98 commit 38603fa

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/services/firefox.service.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,20 @@ class FirefoxService implements AdapterBrowser<browser.tabs.Tab> {
2323
return this.currentTab;
2424
}
2525

26-
async executeScript<R>(callback: () => Promise<R>): Promise<R> {
26+
async executeScript<R>(callback: () => void): Promise<R> {
2727
const tab = await this.getBrowserTab();
28-
return new Promise<R>((resolve, reject) => {
29-
browser.scripting
30-
.executeScript({
31-
target: { tabId: tab.id! },
32-
func: () => {
33-
try {
34-
const result = callback();
35-
if (result instanceof Promise) {
36-
result.then(resolve).catch(reject);
37-
} else {
38-
resolve(result);
39-
}
40-
} catch (error) {
41-
reject(error);
42-
}
43-
},
44-
})
45-
.then(() => {});
46-
});
28+
return browser.scripting
29+
.executeScript({
30+
target: { tabId: tab.id! },
31+
func: callback,
32+
})
33+
.then(injectionResults =>
34+
injectionResults.length ? (injectionResults[0].result as R) : <R>null,
35+
)
36+
.catch(error => {
37+
console.error(error);
38+
return <R>null;
39+
});
4740
}
4841
}
4942

0 commit comments

Comments
 (0)