Skip to content

Commit

Permalink
Merge pull request #46 from moonlight-mod/browser
Browse files Browse the repository at this point in the history
Browser support
  • Loading branch information
Cynosphere authored Oct 4, 2024
2 parents dac42bd + 36cf758 commit ca8adee
Show file tree
Hide file tree
Showing 28 changed files with 1,397 additions and 337 deletions.
136 changes: 111 additions & 25 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const config = {

const prod = process.env.NODE_ENV === "production";
const watch = process.argv.includes("--watch");
const browser = process.argv.includes("--browser");
const mv2 = process.argv.includes("--mv2");

const external = [
"electron",
"fs",
"path",
"module",
"events",
"original-fs", // wtf asar?
"discord", // mappings

// Silence an esbuild warning
Expand Down Expand Up @@ -74,33 +74,86 @@ const taggedBuildLog = (tag) => ({
});

async function build(name, entry) {
const outfile = path.join("./dist", name + ".js");
let outfile = path.join("./dist", name + ".js");
if (name === "browser") outfile = path.join("./dist", "browser", "index.js");

const dropLabels = [];
if (name !== "injector") dropLabels.push("injector");
if (name !== "node-preload") dropLabels.push("nodePreload");
if (name !== "web-preload") dropLabels.push("webPreload");
const labels = {
injector: ["injector"],
nodePreload: ["node-preload"],
webPreload: ["web-preload"],
browser: ["browser"],

webTarget: ["web-preload", "browser"],
nodeTarget: ["node-preload", "injector"]
};
for (const [label, targets] of Object.entries(labels)) {
if (!targets.includes(name)) {
dropLabels.push(label);
}
}

const define = {
MOONLIGHT_ENV: `"${name}"`,
MOONLIGHT_PROD: prod.toString()
};

for (const iterName of Object.keys(config)) {
for (const iterName of [
"injector",
"node-preload",
"web-preload",
"browser"
]) {
const snake = iterName.replace(/-/g, "_").toUpperCase();
define[`MOONLIGHT_${snake}`] = (name === iterName).toString();
}

const nodeDependencies = ["glob"];
const ignoredExternal = name === "web-preload" ? nodeDependencies : [];

const plugins = [deduplicatedLogging, taggedBuildLog(name)];
if (name === "browser") {
plugins.push(
copyStaticFiles({
src: mv2
? "./packages/browser/manifestv2.json"
: "./packages/browser/manifest.json",
dest: "./dist/browser/manifest.json"
})
);

if (!mv2) {
plugins.push(
copyStaticFiles({
src: "./packages/browser/modifyResponseHeaders.json",
dest: "./dist/browser/modifyResponseHeaders.json"
})
);
plugins.push(
copyStaticFiles({
src: "./packages/browser/blockLoading.json",
dest: "./dist/browser/blockLoading.json"
})
);
}

plugins.push(
copyStaticFiles({
src: mv2
? "./packages/browser/src/background-mv2.js"
: "./packages/browser/src/background.js",
dest: "./dist/browser/background.js"
})
);
}

/** @type {import("esbuild").BuildOptions} */
const esbuildConfig = {
entryPoints: [entry],
outfile,

format: "cjs",
platform: name === "web-preload" ? "browser" : "node",
platform: ["web-preload", "browser"].includes(name) ? "browser" : "node",

treeShaking: true,
bundle: true,
Expand All @@ -113,9 +166,38 @@ async function build(name, entry) {
dropLabels,

logLevel: "silent",
plugins: [deduplicatedLogging, taggedBuildLog(name)]
plugins
};

if (name === "browser") {
const coreExtensionsJson = {};

// eslint-disable-next-line no-inner-declarations
function readDir(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = dir + "/" + file;
const normalizedPath = filePath.replace("./dist/core-extensions/", "");
if (fs.statSync(filePath).isDirectory()) {
readDir(filePath);
} else {
coreExtensionsJson[normalizedPath] = fs.readFileSync(
filePath,
"utf8"
);
}
}
}

readDir("./dist/core-extensions");

esbuildConfig.banner = {
js: `window._moonlight_coreExtensionsStr = ${JSON.stringify(
JSON.stringify(coreExtensionsJson)
)};`
};
}

if (watch) {
const ctx = await esbuild.context(esbuildConfig);
await ctx.watch();
Expand Down Expand Up @@ -211,23 +293,27 @@ async function buildExt(ext, side, copyManifest, fileExt) {

const promises = [];

for (const [name, entry] of Object.entries(config)) {
promises.push(build(name, entry));
}
if (browser) {
build("browser", "packages/browser/src/index.ts");
} else {
for (const [name, entry] of Object.entries(config)) {
promises.push(build(name, entry));
}

const coreExtensions = fs.readdirSync("./packages/core-extensions/src");
for (const ext of coreExtensions) {
let copiedManifest = false;

for (const fileExt of ["ts", "tsx"]) {
for (const type of ["index", "node", "host"]) {
if (
fs.existsSync(
`./packages/core-extensions/src/${ext}/${type}.${fileExt}`
)
) {
promises.push(buildExt(ext, type, !copiedManifest, fileExt));
copiedManifest = true;
const coreExtensions = fs.readdirSync("./packages/core-extensions/src");
for (const ext of coreExtensions) {
let copiedManifest = false;

for (const fileExt of ["ts", "tsx"]) {
for (const type of ["index", "node", "host"]) {
if (
fs.existsSync(
`./packages/core-extensions/src/${ext}/${type}.${fileExt}`
)
) {
promises.push(buildExt(ext, type, !copiedManifest, fileExt));
copiedManifest = true;
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"scripts": {
"build": "node build.mjs",
"dev": "node build.mjs --watch",
"browser": "node build.mjs --browser",
"browser-mv2": "node build.mjs --browser --mv2",
"lint": "eslint packages",
"lint:fix": "eslint packages",
"lint:report": "eslint --output-file eslint_report.json --format json packages",
Expand Down
13 changes: 13 additions & 0 deletions packages/browser/blockLoading.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"id": 2,
"priority": 1,
"action": {
"type": "block"
},
"condition": {
"urlFilter": "*://discord.com/assets/*.js",
"resourceTypes": ["script"]
}
}
]
48 changes: 48 additions & 0 deletions packages/browser/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"manifest_version": 3,
"name": "moonlight",
"description": "Yet another Discord mod",
"version": "1.1.0",
"permissions": [
"declarativeNetRequestWithHostAccess",
"webRequest",
"scripting",
"webNavigation"
],
"host_permissions": [
"https://moonlight-mod.github.io/*",
"https://*.discord.com/*"
],
"content_scripts": [
{
"js": ["index.js"],
"matches": ["https://*.discord.com/*"],
"run_at": "document_start",
"world": "MAIN"
}
],
"declarative_net_request": {
"rule_resources": [
{
"id": "modifyResponseHeaders",
"enabled": true,
"path": "modifyResponseHeaders.json"
},
{
"id": "blockLoading",
"enabled": true,
"path": "blockLoading.json"
}
]
},
"background": {
"service_worker": "background.js",
"type": "module"
},
"web_accessible_resources": [
{
"resources": ["index.js"],
"matches": ["https://*.discord.com/*"]
}
]
}
25 changes: 25 additions & 0 deletions packages/browser/manifestv2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"manifest_version": 2,
"name": "moonlight",
"description": "Yet another Discord mod",
"version": "1.1.0",
"permissions": [
"webRequest",
"webRequestBlocking",
"scripting",
"webNavigation",
"https://*.discord.com/assets/*.js",
"https://*.discord.com/*"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"js": ["index.js"],
"matches": ["https://*.discord.com/*"],
"run_at": "document_start",
"world": "MAIN"
}
]
}
19 changes: 19 additions & 0 deletions packages/browser/modifyResponseHeaders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"id": 1,
"priority": 2,
"action": {
"type": "modifyHeaders",
"responseHeaders": [
{
"header": "Content-Security-Policy",
"operation": "remove"
}
]
},
"condition": {
"resourceTypes": ["main_frame"],
"initiatorDomains": ["discord.com"]
}
}
]
11 changes: 11 additions & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@moonlight-mod/browser",
"private": true,
"dependencies": {
"@moonlight-mod/core": "workspace:*",
"@moonlight-mod/types": "workspace:*",
"@moonlight-mod/web-preload": "workspace:*",
"@zenfs/core": "^1.0.2",
"@zenfs/dom": "^0.2.16"
}
}
Loading

0 comments on commit ca8adee

Please sign in to comment.