|
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", {
|
|
29 | 34 | }
|
30 | 35 | });
|
31 | 36 | };
|
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; |
| 37 | + |
| 38 | + const getClipboard = async (tabId: number) => { |
| 39 | + const result = await chrome.scripting.executeScript({ |
| 40 | + target: { tabId: tabId }, |
| 41 | + func: () => navigator.clipboard.readText(), |
| 42 | + }); |
| 43 | + return result[0].result; |
48 | 44 | };
|
49 | 45 |
|
50 | 46 | const canonicalizeUrl = (url: string) => {
|
|
66 | 62 | };
|
67 | 63 |
|
68 | 64 | chrome.contextMenus.create({
|
| 65 | + id: CONTEXT_MENUS.SEND_THIS_PAGE, |
69 | 66 | title: "SendVRC this page",
|
70 | 67 | type: "normal",
|
71 | 68 | contexts: ["page"],
|
72 |
| - onclick: (info) => { |
73 |
| - if (!info || !info["pageUrl"]) { |
74 |
| - return; |
75 |
| - } |
76 |
| - const pageURL = info["pageUrl"]; |
77 |
| - toVRC(pageURL); |
78 |
| - }, |
79 | 69 | });
|
80 | 70 |
|
81 | 71 | chrome.contextMenus.create({
|
| 72 | + id: CONTEXT_MENUS.SEND_WITH_CLIPBOARD, |
82 | 73 | title: "SendVRC with clipboard",
|
83 | 74 | type: "normal",
|
84 |
| - contexts: ["browser_action"], |
85 |
| - onclick: () => { |
86 |
| - toVRC(getClipboard()); |
87 |
| - }, |
| 75 | + contexts: ["action"], |
| 76 | + }); |
| 77 | + |
| 78 | + chrome.contextMenus.onClicked.addListener(async (info, tab) => { |
| 79 | + switch (info.menuItemId) { |
| 80 | + case CONTEXT_MENUS.SEND_THIS_PAGE: { |
| 81 | + if (!info || !info["pageUrl"]) { |
| 82 | + return; |
| 83 | + } |
| 84 | + const pageURL = info["pageUrl"]; |
| 85 | + toVRC(pageURL); |
| 86 | + break; |
| 87 | + } |
| 88 | + case CONTEXT_MENUS.SEND_WITH_CLIPBOARD: { |
| 89 | + if (!tab || tab.id === undefined) { |
| 90 | + return; |
| 91 | + } |
| 92 | + const clipboardText = await getClipboard(tab.id); |
| 93 | + toVRC(clipboardText); |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
88 | 97 | });
|
89 | 98 |
|
90 |
| - chrome.browserAction.onClicked.addListener((e) => { |
| 99 | + chrome.action.onClicked.addListener((e) => { |
91 | 100 | if (!e || !e["url"]) {
|
92 | 101 | return;
|
93 | 102 | }
|
|
0 commit comments