diff --git a/package-lock.json b/package-lock.json index 2a553e0..c7eb8f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "send_vrc", - "version": "1.0.5", + "version": "1.0.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "send_vrc", - "version": "1.0.5", + "version": "1.0.6", "license": "Apache-2.0", "devDependencies": { "@types/chrome": "0.0.181", diff --git a/package.json b/package.json index 2dcacb6..312edfb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "send_vrc", - "version": "1.0.5", + "version": "1.0.6", "description": "One click URL to VRChat", "main": "send.js", "scripts": { diff --git a/send.ts b/send.ts index 270bcab..0501aa3 100644 --- a/send.ts +++ b/send.ts @@ -1,4 +1,9 @@ (() => { + const CONTEXT_MENUS = { + SEND_THIS_PAGE: "send_this_page", + SEND_WITH_CLIPBOARD: "send_with_clipboard", + }; + const defaultMethod = "POST"; const toVRC = (url: string, method: string = defaultMethod) => { fetch("http://localhost:11400/url", { @@ -29,22 +34,13 @@ } }); }; - const getClipboard = () => { - const pasteTarget = document.createElement("div"); - pasteTarget.contentEditable = "true"; - if (!document.activeElement) { - throw new Error("`document.activeElement` is null"); - } - const activeElement = - document.activeElement.appendChild(pasteTarget).parentNode; - pasteTarget.focus(); - document.execCommand("Paste"); - const paste = pasteTarget.innerText; - if (!activeElement) { - throw new Error("`activeElement` is null"); - } - activeElement.removeChild(pasteTarget); - return paste; + + const getClipboard = async (tabId: number) => { + const result = await chrome.scripting.executeScript({ + target: { tabId: tabId }, + func: () => navigator.clipboard.readText(), + }); + return result[0].result; }; const canonicalizeUrl = (url: string) => { @@ -66,28 +62,41 @@ }; chrome.contextMenus.create({ + id: CONTEXT_MENUS.SEND_THIS_PAGE, title: "SendVRC this page", type: "normal", contexts: ["page"], - onclick: (info) => { - if (!info || !info["pageUrl"]) { - return; - } - const pageURL = info["pageUrl"]; - toVRC(pageURL); - }, }); chrome.contextMenus.create({ + id: CONTEXT_MENUS.SEND_WITH_CLIPBOARD, title: "SendVRC with clipboard", type: "normal", - contexts: ["browser_action"], - onclick: () => { - toVRC(getClipboard()); - }, + contexts: ["action"], + }); + + chrome.contextMenus.onClicked.addListener(async (info, tab) => { + switch (info.menuItemId) { + case CONTEXT_MENUS.SEND_THIS_PAGE: { + if (!info || !info["pageUrl"]) { + return; + } + const pageURL = info["pageUrl"]; + toVRC(pageURL); + break; + } + case CONTEXT_MENUS.SEND_WITH_CLIPBOARD: { + if (!tab || tab.id === undefined) { + return; + } + const clipboardText = await getClipboard(tab.id); + toVRC(clipboardText); + break; + } + } }); - chrome.browserAction.onClicked.addListener((e) => { + chrome.action.onClicked.addListener((e) => { if (!e || !e["url"]) { return; } diff --git a/static/manifest.json b/static/manifest.json index 929b73d..99340b2 100644 --- a/static/manifest.json +++ b/static/manifest.json @@ -1,9 +1,9 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "SendVRC | VRChatにURLを送るやつ", - "version": "1.0.5", + "version": "1.0.6", "description": "Sends the URL to VRChat. send_vrc_desktop is required for one-click operation.", - "browser_action": { + "action": { "default_icon": "icon.png", "default_title": "this page send to VRChat" }, @@ -11,12 +11,8 @@ "128": "icon.png" }, "background": { - "scripts": ["send.js"] + "service_worker": "send.js" }, - "permissions": [ - "activeTab", - "contextMenus", - "clipboardRead", - "http://localhost:11400/*" - ] + "permissions": ["activeTab", "contextMenus", "clipboardRead", "scripting"], + "host_permissions": ["http://localhost:11400/*"] }