Skip to content

Commit 435291d

Browse files
committed
fix: return generic in executeScript
1 parent 203e7fc commit 435291d

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

src/services/browser.adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
interface BrowserAdapter<T> {
22
getBrowserTab(): Promise<T>;
3-
executeScript(callback: () => void): Promise<void>;
3+
executeScript<R>(callback: () => Promise<R>): Promise<R>;
44
}
55

66
export default BrowserAdapter;

src/services/chome.service.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ class ChromeService implements AdapterBrowser<chrome.tabs.Tab> {
2323
return this.currentTab;
2424
}
2525

26-
async executeScript(callback: () => void): Promise<void> {
26+
async executeScript<R>(callback: () => Promise<R>): Promise<R> {
2727
const tab = await this.getBrowserTab();
28-
chrome.scripting.executeScript({
29-
target: { tabId: tab.id! },
30-
func: callback,
31-
});
28+
29+
return chrome.scripting
30+
.executeScript({
31+
target: { tabId: tab.id! },
32+
func: callback,
33+
})
34+
.then(injectionResults =>
35+
injectionResults.length ? (injectionResults[0].result as R) : <R>null,
36+
)
37+
.catch(error => {
38+
console.error(error);
39+
return <R>null;
40+
});
3241
}
3342
}
3443

src/services/firefox.service.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
11
import AdapterBrowser from './browser.adapter';
22

33
class FirefoxService implements AdapterBrowser<browser.tabs.Tab> {
4-
private tab?: browser.tabs.Tab;
4+
private currentTab?: browser.tabs.Tab;
55

66
constructor() {}
77

8-
async getBrowserTab(): Promise<browser.tabs.Tab> {
9-
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
8+
private async getActiveTab(): Promise<browser.tabs.Tab> {
9+
const tabs = await new Promise<browser.tabs.Tab[]>(() => {
10+
browser.tabs.query({ active: true, currentWindow: true });
11+
});
1012
if (tabs.length) {
1113
return tabs[0];
1214
} else {
1315
throw new Error('No active tab found');
1416
}
1517
}
1618

17-
async getTab(): Promise<browser.tabs.Tab> {
18-
return this.getBrowserTab();
19+
async getBrowserTab(): Promise<browser.tabs.Tab> {
20+
if (!this.currentTab) {
21+
this.currentTab = await this.getActiveTab();
22+
}
23+
return this.currentTab;
1924
}
2025

21-
async executeScript(callback: () => void): Promise<void> {
22-
if (!this.tab) {
23-
this.tab = await this.getBrowserTab();
24-
}
25-
await browser.scripting.executeScript({
26-
target: { tabId: this.tab.id! },
27-
func: callback,
26+
async executeScript<R>(callback: () => Promise<R>): Promise<R> {
27+
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(() => {});
2846
});
2947
}
3048
}

0 commit comments

Comments
 (0)