-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.ts
42 lines (38 loc) · 994 Bytes
/
actions.ts
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
import browser from "webextension-polyfill";
declare global {
const chrome: {
clipboard: {
writeText: any;
};
};
}
import { NotifyMessageKey, notify } from "./notify";
export async function actionOpen(url: string, target: "new" | "current") {
if (target === "new") {
await browser.tabs.create({
url,
});
} else if (target === "current") {
const currentWindow = await browser.windows.getCurrent({ populate: true });
const currentTabs = currentWindow.tabs?.filter((tab) => tab.active);
if (currentTabs?.length !== 1) {
notify("current-tab-fail");
return;
}
await browser.tabs.update(currentTabs[0].id, {
url,
});
}
}
export async function actionCopy(
text: string,
successMessage: NotifyMessageKey,
failMessage: NotifyMessageKey
) {
try {
await navigator.clipboard.writeText(text);
await notify(successMessage);
} catch (error) {
await notify(failMessage, { error: error as Error });
}
}