Skip to content

Commit 2e296ee

Browse files
authored
Merge pull request #150 from vrc-plugin/remove-put-support
feat!: remove support for PUT method
2 parents 0f442a7 + 72df8c2 commit 2e296ee

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

Diff for: src/background.ts

+36-45
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,33 @@ const CONTEXT_MENUS = {
33
SEND_WITH_CLIPBOARD: "send_with_clipboard",
44
};
55

6-
const defaultMethod = "POST";
7-
const toVRC = (url: string, method: string = defaultMethod) => {
8-
fetch("http://localhost:11400/url", {
9-
method: method,
10-
mode: "cors",
11-
credentials: "omit",
12-
cache: "no-cache",
13-
headers: { "Content-Type": "application/json" },
14-
body: JSON.stringify({ url: url }),
15-
})
16-
.then((res) => {
17-
if (!res.ok && method !== "PUT") {
18-
toVRC(url, "PUT");
19-
}
6+
const ERROR_NOTIFICATION_OPTIONS: chrome.notifications.NotificationOptions<true> =
7+
{
8+
type: "basic",
9+
iconUrl: "./icon.png",
10+
title: "send_vrc",
11+
message: "please start send_vrc_desktop.",
12+
} as const;
2013

21-
if (!res.ok && method === "PUT") {
22-
chrome.notifications.create({
23-
type: "basic",
24-
iconUrl: "./icon.png",
25-
title: "send_vrc",
26-
message: "please start send_vrc_desktop.",
27-
});
28-
}
29-
})
30-
.catch((e) => {
31-
console.log(e, method);
32-
// old version compatible.
33-
if (method !== "PUT") {
34-
toVRC(url, "PUT");
35-
}
36-
if (method === "PUT") {
37-
chrome.notifications.create({
38-
type: "basic",
39-
iconUrl: "./icon.png",
40-
title: "send_vrc",
41-
message: "please start send_vrc_desktop.",
42-
});
43-
}
14+
const sendUrlToVRC = async (url: string) => {
15+
try {
16+
const res = await fetch("http://localhost:11400/url", {
17+
method: "POST",
18+
mode: "cors",
19+
credentials: "omit",
20+
cache: "no-cache",
21+
headers: { "Content-Type": "application/json" },
22+
body: JSON.stringify({ url: url }),
4423
});
24+
25+
if (!res.ok) {
26+
console.log(res.statusText);
27+
chrome.notifications.create(ERROR_NOTIFICATION_OPTIONS);
28+
}
29+
} catch (e) {
30+
console.log(e);
31+
chrome.notifications.create(ERROR_NOTIFICATION_OPTIONS);
32+
}
4533
};
4634

4735
const getClipboard = async (tabId: number) => {
@@ -87,28 +75,31 @@ chrome.contextMenus.create({
8775
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
8876
switch (info.menuItemId) {
8977
case CONTEXT_MENUS.SEND_THIS_PAGE: {
90-
if (!info || !info["pageUrl"]) {
78+
if (!info.pageUrl) {
9179
return;
9280
}
93-
const pageURL = info["pageUrl"];
94-
toVRC(pageURL);
81+
82+
const url = canonicalizeUrl(info.pageUrl);
83+
sendUrlToVRC(url);
9584
break;
9685
}
9786
case CONTEXT_MENUS.SEND_WITH_CLIPBOARD: {
98-
if (!tab || tab.id === undefined) {
87+
if (tab?.id === undefined) {
9988
return;
10089
}
90+
10191
const clipboardText = await getClipboard(tab.id);
102-
toVRC(clipboardText);
92+
sendUrlToVRC(clipboardText);
10393
break;
10494
}
10595
}
10696
});
10797

10898
chrome.action.onClicked.addListener((e) => {
109-
if (!e || !e["url"]) {
99+
if (!e.url) {
110100
return;
111101
}
112-
const url = canonicalizeUrl(e["url"]);
113-
toVRC(url);
102+
103+
const url = canonicalizeUrl(e.url);
104+
sendUrlToVRC(url);
114105
});

0 commit comments

Comments
 (0)