|
1 | 1 | (() => {
|
| 2 | + const CONTEXT_MENUS = { |
| 3 | + SEND_THIS_PAGE: "send_this_page", |
| 4 | + SEND_WITH_CLIPBOARD: "send_with_clipboard", |
| 5 | + }; |
| 6 | + |
2 | 7 | const defaultMethod = "POST";
|
3 | 8 | const toVRC = (url: string, method: string = defaultMethod) => {
|
4 | 9 | fetch("http://localhost:11400/url", {
|
|
15 | 20 | }
|
16 | 21 |
|
17 | 22 | if (!res.ok && method === "PUT") {
|
18 |
| - window.alert("please start send_vrc_desktop."); |
| 23 | + chrome.notifications.create({ |
| 24 | + type: "basic", |
| 25 | + iconUrl: "./icon.png", |
| 26 | + title: "send_vrc", |
| 27 | + message: "please start send_vrc_desktop.", |
| 28 | + }); |
19 | 29 | }
|
20 | 30 | })
|
21 | 31 | .catch((e) => {
|
|
25 | 35 | toVRC(url, "PUT");
|
26 | 36 | }
|
27 | 37 | if (method === "PUT") {
|
28 |
| - window.alert("please start send_vrc_desktop."); |
| 38 | + chrome.notifications.create({ |
| 39 | + type: "basic", |
| 40 | + iconUrl: "./icon.png", |
| 41 | + title: "send_vrc", |
| 42 | + message: "please start send_vrc_desktop.", |
| 43 | + }); |
29 | 44 | }
|
30 | 45 | });
|
31 | 46 | };
|
32 |
| - const getClipboard = () => { |
33 |
| - const pasteTarget = document.createElement("div"); |
34 |
| - pasteTarget.contentEditable = "true"; |
35 |
| - if (!document.activeElement) { |
36 |
| - throw new Error("`document.activeElement` is null"); |
37 |
| - } |
38 |
| - const activeElement = |
39 |
| - document.activeElement.appendChild(pasteTarget).parentNode; |
40 |
| - pasteTarget.focus(); |
41 |
| - document.execCommand("Paste"); |
42 |
| - const paste = pasteTarget.innerText; |
43 |
| - if (!activeElement) { |
44 |
| - throw new Error("`activeElement` is null"); |
45 |
| - } |
46 |
| - activeElement.removeChild(pasteTarget); |
47 |
| - return paste; |
| 47 | + |
| 48 | + const getClipboard = async (tabId: number) => { |
| 49 | + const result = await chrome.scripting.executeScript({ |
| 50 | + target: { tabId: tabId }, |
| 51 | + func: () => navigator.clipboard.readText(), |
| 52 | + }); |
| 53 | + return result[0].result; |
48 | 54 | };
|
49 | 55 |
|
50 | 56 | const canonicalizeUrl = (url: string) => {
|
|
66 | 72 | };
|
67 | 73 |
|
68 | 74 | chrome.contextMenus.create({
|
| 75 | + id: CONTEXT_MENUS.SEND_THIS_PAGE, |
69 | 76 | title: "SendVRC this page",
|
70 | 77 | type: "normal",
|
71 | 78 | contexts: ["page"],
|
72 |
| - onclick: (info) => { |
73 |
| - if (!info || !info["pageUrl"]) { |
74 |
| - return; |
75 |
| - } |
76 |
| - const pageURL = info["pageUrl"]; |
77 |
| - toVRC(pageURL); |
78 |
| - }, |
79 | 79 | });
|
80 | 80 |
|
81 | 81 | chrome.contextMenus.create({
|
| 82 | + id: CONTEXT_MENUS.SEND_WITH_CLIPBOARD, |
82 | 83 | title: "SendVRC with clipboard",
|
83 | 84 | type: "normal",
|
84 |
| - contexts: ["browser_action"], |
85 |
| - onclick: () => { |
86 |
| - toVRC(getClipboard()); |
87 |
| - }, |
| 85 | + contexts: ["action"], |
| 86 | + }); |
| 87 | + |
| 88 | + chrome.contextMenus.onClicked.addListener(async (info, tab) => { |
| 89 | + switch (info.menuItemId) { |
| 90 | + case CONTEXT_MENUS.SEND_THIS_PAGE: { |
| 91 | + if (!info || !info["pageUrl"]) { |
| 92 | + return; |
| 93 | + } |
| 94 | + const pageURL = info["pageUrl"]; |
| 95 | + toVRC(pageURL); |
| 96 | + break; |
| 97 | + } |
| 98 | + case CONTEXT_MENUS.SEND_WITH_CLIPBOARD: { |
| 99 | + if (!tab || tab.id === undefined) { |
| 100 | + return; |
| 101 | + } |
| 102 | + const clipboardText = await getClipboard(tab.id); |
| 103 | + toVRC(clipboardText); |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
88 | 107 | });
|
89 | 108 |
|
90 |
| - chrome.browserAction.onClicked.addListener((e) => { |
| 109 | + chrome.action.onClicked.addListener((e) => { |
91 | 110 | if (!e || !e["url"]) {
|
92 | 111 | return;
|
93 | 112 | }
|
|
0 commit comments