Skip to content

Commit d8e7d10

Browse files
authored
Merge pull request #25 from vrc-plugin/manifest-v3
feat: migrate to Manifest v3
2 parents 124460c + 75f05b4 commit d8e7d10

File tree

4 files changed

+60
-39
lines changed

4 files changed

+60
-39
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.7",
44
"description": "One click URL to VRChat",
55
"main": "send.js",
66
"scripts": {

Diff for: send.ts

+49-30
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", {
@@ -15,7 +20,12 @@
1520
}
1621

1722
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+
});
1929
}
2030
})
2131
.catch((e) => {
@@ -25,26 +35,22 @@
2535
toVRC(url, "PUT");
2636
}
2737
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+
});
2944
}
3045
});
3146
};
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;
4854
};
4955

5056
const canonicalizeUrl = (url: string) => {
@@ -66,28 +72,41 @@
6672
};
6773

6874
chrome.contextMenus.create({
75+
id: CONTEXT_MENUS.SEND_THIS_PAGE,
6976
title: "SendVRC this page",
7077
type: "normal",
7178
contexts: ["page"],
72-
onclick: (info) => {
73-
if (!info || !info["pageUrl"]) {
74-
return;
75-
}
76-
const pageURL = info["pageUrl"];
77-
toVRC(pageURL);
78-
},
7979
});
8080

8181
chrome.contextMenus.create({
82+
id: CONTEXT_MENUS.SEND_WITH_CLIPBOARD,
8283
title: "SendVRC with clipboard",
8384
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+
}
88107
});
89108

90-
chrome.browserAction.onClicked.addListener((e) => {
109+
chrome.action.onClicked.addListener((e) => {
91110
if (!e || !e["url"]) {
92111
return;
93112
}

Diff for: static/manifest.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
{
2-
"manifest_version": 2,
2+
"manifest_version": 3,
33
"name": "SendVRC | VRChatにURLを送るやつ",
4-
"version": "1.0.5",
4+
"version": "1.0.7",
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
},
1616
"permissions": [
1717
"activeTab",
1818
"contextMenus",
1919
"clipboardRead",
20-
"http://localhost:11400/*"
21-
]
20+
"notifications",
21+
"scripting"
22+
],
23+
"host_permissions": ["http://localhost:11400/*"]
2224
}

0 commit comments

Comments
 (0)