Skip to content

Commit cf103e8

Browse files
committed
feat: migrate to Manifest v3
Signed-off-by: koyashiro <[email protected]>
1 parent 124460c commit cf103e8

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "send_vrc",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "One click URL to VRChat",
55
"main": "send.js",
66
"scripts": {

Diff for: send.ts

+37-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
(() => {
2+
const CONTEXT_MENUS = {
3+
SEND_THIS_PAGE: "send_this_page",
4+
SEND_WITH_CLIPBOARD: "send_with_clipboard",
5+
};
6+
27
const defaultMethod = "POST";
38
const toVRC = (url: string, method: string = defaultMethod) => {
49
fetch("http://localhost:11400/url", {
@@ -29,22 +34,13 @@
2934
}
3035
});
3136
};
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;
4844
};
4945

5046
const canonicalizeUrl = (url: string) => {
@@ -66,28 +62,41 @@
6662
};
6763

6864
chrome.contextMenus.create({
65+
id: CONTEXT_MENUS.SEND_THIS_PAGE,
6966
title: "SendVRC this page",
7067
type: "normal",
7168
contexts: ["page"],
72-
onclick: (info) => {
73-
if (!info || !info["pageUrl"]) {
74-
return;
75-
}
76-
const pageURL = info["pageUrl"];
77-
toVRC(pageURL);
78-
},
7969
});
8070

8171
chrome.contextMenus.create({
72+
id: CONTEXT_MENUS.SEND_WITH_CLIPBOARD,
8273
title: "SendVRC with clipboard",
8374
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+
}
8897
});
8998

90-
chrome.browserAction.onClicked.addListener((e) => {
99+
chrome.action.onClicked.addListener((e) => {
91100
if (!e || !e["url"]) {
92101
return;
93102
}

Diff for: static/manifest.json

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
{
2-
"manifest_version": 2,
2+
"manifest_version": 3,
33
"name": "SendVRC | VRChatにURLを送るやつ",
4-
"version": "1.0.5",
4+
"version": "1.0.6",
55
"description": "Sends the URL to VRChat. send_vrc_desktop is required for one-click operation.",
6-
"browser_action": {
6+
"action": {
77
"default_icon": "icon.png",
88
"default_title": "this page send to VRChat"
99
},
1010
"icons": {
1111
"128": "icon.png"
1212
},
1313
"background": {
14-
"scripts": ["send.js"]
14+
"service_worker": "send.js"
1515
},
16-
"permissions": [
17-
"activeTab",
18-
"contextMenus",
19-
"clipboardRead",
20-
"http://localhost:11400/*"
21-
]
16+
"permissions": ["activeTab", "contextMenus", "clipboardRead", "scripting"],
17+
"host_permissions": ["http://localhost:11400/*"]
2218
}

0 commit comments

Comments
 (0)