Skip to content

Commit

Permalink
feat: migrate to Manifest v3
Browse files Browse the repository at this point in the history
Signed-off-by: koyashiro <[email protected]>
  • Loading branch information
koyashiro committed Apr 22, 2022
1 parent 124460c commit cf103e8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
65 changes: 37 additions & 28 deletions send.ts
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
}
Expand Down
16 changes: 6 additions & 10 deletions static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
{
"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"
},
"icons": {
"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/*"]
}

0 comments on commit cf103e8

Please sign in to comment.