Skip to content

Commit 0f442a7

Browse files
authored
chore: setup webpack (#149)
Signed-off-by: koyashiro <[email protected]>
1 parent dab93c9 commit 0f442a7

File tree

8 files changed

+2489
-174
lines changed

8 files changed

+2489
-174
lines changed

Diff for: .eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"env": {
33
"browser": true,
44
"webextensions": true,
5-
"node": false
5+
"node": true
66
},
77
"extends": [
88
"eslint:recommended",

Diff for: package-lock.json

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

Diff for: package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "One click URL to VRChat",
55
"main": "send.js",
66
"scripts": {
7-
"build": "tsc && cp ./static/* ./dist",
7+
"build": "tsc --noEmit && webpack --mode production",
8+
"dev": "webpack --watch",
89
"lint": "eslint .",
910
"fix": "eslint --fix .",
1011
"format": "prettier --write .",
@@ -24,9 +25,13 @@
2425
"@types/chrome": "0.0.213",
2526
"@typescript-eslint/eslint-plugin": "5.52.0",
2627
"@typescript-eslint/parser": "5.52.0",
28+
"copy-webpack-plugin": "11.0.0",
2729
"eslint": "8.34.0",
2830
"eslint-config-prettier": "8.6.0",
2931
"prettier": "2.8.4",
30-
"typescript": "4.9.5"
32+
"ts-loader": "9.4.2",
33+
"typescript": "4.9.5",
34+
"webpack": "5.75.0",
35+
"webpack-cli": "5.0.1"
3136
}
3237
}

Diff for: send.ts

-116
This file was deleted.

Diff for: send_test.ts

-44
This file was deleted.

Diff for: src/background.ts

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const CONTEXT_MENUS = {
2+
SEND_THIS_PAGE: "send_this_page",
3+
SEND_WITH_CLIPBOARD: "send_with_clipboard",
4+
};
5+
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+
}
20+
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+
}
44+
});
45+
};
46+
47+
const getClipboard = async (tabId: number) => {
48+
const result = await chrome.scripting.executeScript({
49+
target: { tabId: tabId },
50+
func: () => navigator.clipboard.readText(),
51+
});
52+
return result[0].result;
53+
};
54+
55+
const canonicalizeUrl = (url: string) => {
56+
const u = new URL(url);
57+
switch (u.hostname) {
58+
case "youtube.com":
59+
case "www.youtube.com":
60+
case "m.youtube.com": {
61+
const v = u.searchParams.get("v");
62+
const allowURL = new URL("https://www.youtube.com/watch");
63+
if (v) {
64+
allowURL.searchParams.append("v", v);
65+
}
66+
return allowURL.toString();
67+
}
68+
default:
69+
return u.toString();
70+
}
71+
};
72+
73+
chrome.contextMenus.create({
74+
id: CONTEXT_MENUS.SEND_THIS_PAGE,
75+
title: "SendVRC this page",
76+
type: "normal",
77+
contexts: ["page"],
78+
});
79+
80+
chrome.contextMenus.create({
81+
id: CONTEXT_MENUS.SEND_WITH_CLIPBOARD,
82+
title: "SendVRC with clipboard",
83+
type: "normal",
84+
contexts: ["action"],
85+
});
86+
87+
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
88+
switch (info.menuItemId) {
89+
case CONTEXT_MENUS.SEND_THIS_PAGE: {
90+
if (!info || !info["pageUrl"]) {
91+
return;
92+
}
93+
const pageURL = info["pageUrl"];
94+
toVRC(pageURL);
95+
break;
96+
}
97+
case CONTEXT_MENUS.SEND_WITH_CLIPBOARD: {
98+
if (!tab || tab.id === undefined) {
99+
return;
100+
}
101+
const clipboardText = await getClipboard(tab.id);
102+
toVRC(clipboardText);
103+
break;
104+
}
105+
}
106+
});
107+
108+
chrome.action.onClicked.addListener((e) => {
109+
if (!e || !e["url"]) {
110+
return;
111+
}
112+
const url = canonicalizeUrl(e["url"]);
113+
toVRC(url);
114+
});

Diff for: static/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"128": "icon.png"
1212
},
1313
"background": {
14-
"service_worker": "send.js"
14+
"service_worker": "background.js"
1515
},
1616
"permissions": [
1717
"activeTab",

Diff for: webpack.config.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const path = require("path");
3+
const CopyPlugin = require("copy-webpack-plugin");
4+
5+
/** @type {import('webpack').Configuration} */
6+
module.exports = {
7+
mode: process.env.NODE_ENV || "development",
8+
entry: {
9+
background: path.join(__dirname, "src/background.ts"),
10+
},
11+
output: {
12+
path: path.join(__dirname, "dist"),
13+
filename: "[name].js",
14+
clean: true,
15+
},
16+
module: {
17+
rules: [
18+
{
19+
test: /\.ts$/,
20+
use: "ts-loader",
21+
exclude: /node_modules/,
22+
},
23+
],
24+
},
25+
resolve: {
26+
extensions: [".ts", ".js"],
27+
},
28+
plugins: [
29+
new CopyPlugin({ patterns: [{ from: ".", to: ".", context: "static" }] }),
30+
],
31+
devtool: "cheap-module-source-map",
32+
};

0 commit comments

Comments
 (0)