From e914f649870fc28b7920830f6d6a78962ecdd9a3 Mon Sep 17 00:00:00 2001 From: adryd Date: Thu, 16 Nov 2023 14:28:15 -0500 Subject: [PATCH] owo --- .gitignore | 2 + build/build.js | 13 ++ build/dev.js | 13 ++ dist/webpackTools.user.js | 71 +++++++ package-lock.json | 405 ++++++++++++++++++++++++++++++++++++++ package.json | 15 ++ src/config.js | 4 + src/index.js | 32 +++ src/patcher.js | 115 +++++++++++ webpackPreload.user.js | 114 ----------- 10 files changed, 670 insertions(+), 114 deletions(-) create mode 100644 .gitignore create mode 100644 build/build.js create mode 100644 build/dev.js create mode 100644 dist/webpackTools.user.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/config.js create mode 100644 src/index.js create mode 100644 src/patcher.js delete mode 100644 webpackPreload.user.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8af3b6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dist/webpackToolsRuntime.js +node_modules \ No newline at end of file diff --git a/build/build.js b/build/build.js new file mode 100644 index 0000000..5e704e6 --- /dev/null +++ b/build/build.js @@ -0,0 +1,13 @@ +import * as esbuild from 'esbuild' + +const context = await esbuild.context({ + entryPoints: ['src/index.js'], + bundle: true, + outfile: 'dist/webpackToolsRuntime.js', + logLevel: 'info', + target: [ + 'es2020', + ], +}) + +await context.build() \ No newline at end of file diff --git a/build/dev.js b/build/dev.js new file mode 100644 index 0000000..3183fea --- /dev/null +++ b/build/dev.js @@ -0,0 +1,13 @@ +import * as esbuild from 'esbuild' + +const context = await esbuild.context({ + entryPoints: ['src/index.js'], + bundle: true, + outfile: 'dist/webpackToolsRuntime.js', + logLevel: 'info', + target: [ + 'es2020', + ], +}) + +await context.watch() \ No newline at end of file diff --git a/dist/webpackTools.user.js b/dist/webpackTools.user.js new file mode 100644 index 0000000..49fcf0b --- /dev/null +++ b/dist/webpackTools.user.js @@ -0,0 +1,71 @@ +// ==UserScript== +// @name webpackTools +// @namespace https://adryd.com +// @version 0.1 +// @description meow meow emwo +// @author adryd +// @match https://* +// @match http://* +// @grant GM_addElement +// @grant GM_getResourceText +// @run-at document-start +// @resource runtimeScript http://127.0.0.1:8080/webpackToolsRuntime.js +// ==/UserScript== + +(() => { + const configs = [ + { + name: "discord", + matchSites: ["discord.com", "ptb.discord.com", "canary.discord.com"], + chunkObject: "webpackChunkdiscord_app", + webpackVersion: 5, + patches: [], + modules: [], + }, + { + name: "twitter", + matchSites: ["twitter.com"], + chunkObject: "webpackChunk_twitter_responsive_web", + webpackVersion: 5, + patches: [ + { + name: "demo", + find: "(window.__INITIAL_STATE__", + replace: { + match: /var .{1,3}=.\..\(window\.__INITIAL_STATE__/, + replacement: (orig) => `console.log('Patches work!!!');${orig}`, + }, + }, + ], + modules: [ + { + name: "inspect", + needs: ["(window.__INITIAL_STATE__"], + entry: true, + run: function (module, exports, webpackRequire) { + console.log("meowwie"); + unsafeWindow.wpRequire = webpackRequire; + }, + }, + ], + } + ]; + + let thisSiteConfig; + for (let siteConfig of configs) { + if (siteConfig.matchSites?.includes(window.location.host)) { + thisSiteConfig = siteConfig; + break; + } + } + if (!thisSiteConfig) { + return; + } + + unsafeWindow.__webpackTools_siteConfig = thisSiteConfig; + + GM_addElement("script", { + textContent: GM_getResourceText("runtimeScript"), + }); + })(); + \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..ce6481f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,405 @@ +{ + "name": "webpacktools", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "webpacktools", + "version": "1.0.0", + "license": "ISC", + "devDependencies": { + "esbuild": "0.19.5" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.19.5.tgz", + "integrity": "sha512-bhvbzWFF3CwMs5tbjf3ObfGqbl/17ict2/uwOSfr3wmxDE6VdS2GqY/FuzIPe0q0bdhj65zQsvqfArI9MY6+AA==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.19.5.tgz", + "integrity": "sha512-5d1OkoJxnYQfmC+Zd8NBFjkhyCNYwM4n9ODrycTFY6Jk1IGiZ+tjVJDDSwDt77nK+tfpGP4T50iMtVi4dEGzhQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.19.5.tgz", + "integrity": "sha512-9t+28jHGL7uBdkBjL90QFxe7DVA+KGqWlHCF8ChTKyaKO//VLuoBricQCgwhOjA1/qOczsw843Fy4cbs4H3DVA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.19.5.tgz", + "integrity": "sha512-mvXGcKqqIqyKoxq26qEDPHJuBYUA5KizJncKOAf9eJQez+L9O+KfvNFu6nl7SCZ/gFb2QPaRqqmG0doSWlgkqw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.19.5.tgz", + "integrity": "sha512-Ly8cn6fGLNet19s0X4unjcniX24I0RqjPv+kurpXabZYSXGM4Pwpmf85WHJN3lAgB8GSth7s5A0r856S+4DyiA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.19.5.tgz", + "integrity": "sha512-GGDNnPWTmWE+DMchq1W8Sd0mUkL+APvJg3b11klSGUDvRXh70JqLAO56tubmq1s2cgpVCSKYywEiKBfju8JztQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.19.5.tgz", + "integrity": "sha512-1CCwDHnSSoA0HNwdfoNY0jLfJpd7ygaLAp5EHFos3VWJCRX9DMwWODf96s9TSse39Br7oOTLryRVmBoFwXbuuQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.19.5.tgz", + "integrity": "sha512-lrWXLY/vJBzCPC51QN0HM71uWgIEpGSjSZZADQhq7DKhPcI6NH1IdzjfHkDQws2oNpJKpR13kv7/pFHBbDQDwQ==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.19.5.tgz", + "integrity": "sha512-o3vYippBmSrjjQUCEEiTZ2l+4yC0pVJD/Dl57WfPwwlvFkrxoSO7rmBZFii6kQB3Wrn/6GwJUPLU5t52eq2meA==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.19.5.tgz", + "integrity": "sha512-MkjHXS03AXAkNp1KKkhSKPOCYztRtK+KXDNkBa6P78F8Bw0ynknCSClO/ztGszILZtyO/lVKpa7MolbBZ6oJtQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.19.5.tgz", + "integrity": "sha512-42GwZMm5oYOD/JHqHska3Jg0r+XFb/fdZRX+WjADm3nLWLcIsN27YKtqxzQmGNJgu0AyXg4HtcSK9HuOk3v1Dw==", + "cpu": [ + "loong64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.19.5.tgz", + "integrity": "sha512-kcjndCSMitUuPJobWCnwQ9lLjiLZUR3QLQmlgaBfMX23UEa7ZOrtufnRds+6WZtIS9HdTXqND4yH8NLoVVIkcg==", + "cpu": [ + "mips64el" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.19.5.tgz", + "integrity": "sha512-yJAxJfHVm0ZbsiljbtFFP1BQKLc8kUF6+17tjQ78QjqjAQDnhULWiTA6u0FCDmYT1oOKS9PzZ2z0aBI+Mcyj7Q==", + "cpu": [ + "ppc64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.19.5.tgz", + "integrity": "sha512-5u8cIR/t3gaD6ad3wNt1MNRstAZO+aNyBxu2We8X31bA8XUNyamTVQwLDA1SLoPCUehNCymhBhK3Qim1433Zag==", + "cpu": [ + "riscv64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.19.5.tgz", + "integrity": "sha512-Z6JrMyEw/EmZBD/OFEFpb+gao9xJ59ATsoTNlj39jVBbXqoZm4Xntu6wVmGPB/OATi1uk/DB+yeDPv2E8PqZGw==", + "cpu": [ + "s390x" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.19.5.tgz", + "integrity": "sha512-psagl+2RlK1z8zWZOmVdImisMtrUxvwereIdyJTmtmHahJTKb64pAcqoPlx6CewPdvGvUKe2Jw+0Z/0qhSbG1A==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.19.5.tgz", + "integrity": "sha512-kL2l+xScnAy/E/3119OggX8SrWyBEcqAh8aOY1gr4gPvw76la2GlD4Ymf832UCVbmuWeTf2adkZDK+h0Z/fB4g==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.19.5.tgz", + "integrity": "sha512-sPOfhtzFufQfTBgRnE1DIJjzsXukKSvZxloZbkJDG383q0awVAq600pc1nfqBcl0ice/WN9p4qLc39WhBShRTA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.19.5.tgz", + "integrity": "sha512-dGZkBXaafuKLpDSjKcB0ax0FL36YXCvJNnztjKV+6CO82tTYVDSH2lifitJ29jxRMoUhgkg9a+VA/B03WK5lcg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.19.5.tgz", + "integrity": "sha512-dWVjD9y03ilhdRQ6Xig1NWNgfLtf2o/STKTS+eZuF90fI2BhbwD6WlaiCGKptlqXlURVB5AUOxUj09LuwKGDTg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.19.5.tgz", + "integrity": "sha512-4liggWIA4oDgUxqpZwrDhmEfAH4d0iljanDOK7AnVU89T6CzHon/ony8C5LeOdfgx60x5cnQJFZwEydVlYx4iw==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.19.5.tgz", + "integrity": "sha512-czTrygUsB/jlM8qEW5MD8bgYU2Xg14lo6kBDXW6HdxKjh8M5PzETGiSHaz9MtbXBYDloHNUAUW2tMiKW4KM9Mw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild": { + "version": "0.19.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.19.5.tgz", + "integrity": "sha512-bUxalY7b1g8vNhQKdB24QDmHeY4V4tw/s6Ak5z+jJX9laP5MoQseTOMemAr0gxssjNcH0MCViG8ONI2kksvfFQ==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/android-arm": "0.19.5", + "@esbuild/android-arm64": "0.19.5", + "@esbuild/android-x64": "0.19.5", + "@esbuild/darwin-arm64": "0.19.5", + "@esbuild/darwin-x64": "0.19.5", + "@esbuild/freebsd-arm64": "0.19.5", + "@esbuild/freebsd-x64": "0.19.5", + "@esbuild/linux-arm": "0.19.5", + "@esbuild/linux-arm64": "0.19.5", + "@esbuild/linux-ia32": "0.19.5", + "@esbuild/linux-loong64": "0.19.5", + "@esbuild/linux-mips64el": "0.19.5", + "@esbuild/linux-ppc64": "0.19.5", + "@esbuild/linux-riscv64": "0.19.5", + "@esbuild/linux-s390x": "0.19.5", + "@esbuild/linux-x64": "0.19.5", + "@esbuild/netbsd-x64": "0.19.5", + "@esbuild/openbsd-x64": "0.19.5", + "@esbuild/sunos-x64": "0.19.5", + "@esbuild/win32-arm64": "0.19.5", + "@esbuild/win32-ia32": "0.19.5", + "@esbuild/win32-x64": "0.19.5" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..25f317b --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "webpacktools", + "version": "1.0.0", + "description": "", + "main": "webpackPreload.user.js", + "scripts": { + "build": "node build/build.js" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "esbuild": "0.19.5" + }, + "type": "module" +} diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..902bd7a --- /dev/null +++ b/src/config.js @@ -0,0 +1,4 @@ +const config = window.__webpackTools_siteConfig; +delete window.__webpackTools_siteConfig; + +export default config \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a0d5506 --- /dev/null +++ b/src/index.js @@ -0,0 +1,32 @@ +import config from "./config"; +import {patchModules, injectModules} from "./patcher"; + +const chunkObjectName = config.chunkObject; + +// This is necesary since some sites (twitter) define the chunk object earlier +let realChunkObject = window[chunkObjectName]; + +Object.defineProperty(window, chunkObjectName, { + set: function set(value) { + // Don't infinitely re-wrap .push() + if (!value.push.__wpt_injected) { + realChunkObject = value; + const webpackPush = value.push; + + value.push = function (chunk) { + if (!webpackPush.__wpt_injected) { + patchModules(chunk[1]); + injectModules(chunk); + } + return webpackPush.apply(this, arguments); + }; + + value.push.__wpt_injected = true; + console.log("injected " + chunkObjectName); + } + }, + get: function get() { + return realChunkObject; + }, + configurable: true, +}); diff --git a/src/patcher.js b/src/patcher.js new file mode 100644 index 0000000..e990888 --- /dev/null +++ b/src/patcher.js @@ -0,0 +1,115 @@ +import config from "./config"; + +const patchesToApply = new Set(); +for (const patch of config.patches) { + patchesToApply.add(patch); +} + +export function patchModules(modules) { + for (let id in modules) { + let funcStr = Function.prototype.toString.apply(modules[id]); + + const patchesToApply = []; + for (let patch of config.patches) { + // TODO: support regexp matches + if (funcStr.indexOf(patch.find) != -1) { + patchesToApply.push(patch.replace); + } + } + + for (let patchToApply of patchesToApply) { + funcStr = funcStr.replace(patchToApply.match, patchToApply.replacement); + } + + if (patchesToApply.length > 0 || config.inspectAll) { + const debugString = + "Patched by: " + patchesToApply.map((patch) => patch.name).join(", "); + + modules[id] = new Function( + "module", + "exports", + "webpackRequire", + `(${funcStr}).apply(this, arguments)\n// ${debugString}\n//# sourceURL=Webpack-Module-${id}` + ); + modules[id].__wpt_patched = true; + } + + modules[id].__wpt_funcStr = funcStr; + modules[id].__wpt_processed = true; + } +} + +const modulesToInject = new Set(); +for (const module of config.modules) { + module.needs = new Set(module.needs); + modulesToInject.add(module); +} + +//// Expose webpackRequire in global scope +// modulesToInject.add({ +// name: "webpackRequire", +// run: (module, exports, webpackRequire) => { +// window.webpackRequire = webpackRequire; +// }, +// entry: true, +// }); + +export function injectModules(chunk) { + const readyModules = new Set(); + + for (let moduleToInject of modulesToInject) { + if (moduleToInject?.needs?.size > 0) { + for (const need of moduleToInject.needs) { + for (let wpModule of Object.values(chunk[1])) { + // TODO: optimize + // TODO: support regexp and moduleId + if (wpModule.__wpt_funcStr.includes(need)) { + moduleToInject.needs.delete(need); + if (moduleToInject.needs.size == 0) { + readyModules.add(moduleToInject); + } + break; + } + } + } + } else { + readyModules.add(moduleToInject); + } + } + + if (readyModules.size > 0) { + const injectModules = {}; + const injectEntries = []; + + for (const readyModule of readyModules) { + modulesToInject.delete(readyModule); + injectModules[readyModule.name] = readyModule.run; + if (readyModule.entry) { + injectEntries.push(readyModule.name); + } + } + + chunk[1] = Object.assign(chunk[1], injectModules); + if (injectEntries.length > 0) { + switch (config.webpackVersion) { + case 5: + if (chunk[2]) { + const originalEntry = chunk[2]; + chunk[2] = function (webpackRequire) { + originalEntry.apply(this, arguments); + injectEntries.forEach(webpackRequire); + }; + } else { + chunk[2] = function (webpackRequire) { + injectEntries.forEach(webpackRequire); + }; + } + break; + case 4: + const originalEntry = chunk[2] ?? []; + chunk[2] = originalEntry.concat(injectEntries); + break; + } + } + } +} diff --git a/webpackPreload.user.js b/webpackPreload.user.js deleted file mode 100644 index 24cf4c0..0000000 --- a/webpackPreload.user.js +++ /dev/null @@ -1,114 +0,0 @@ -// ==UserScript== -// @name WebpackPreload -// @namespace Violentmonkey Scripts -// @version 0.1 -// @description meow meow emwo -// @author adryd -// @match https://twitter.com/* -// @match https://discord.com/* -// @grant none -// @run-at document-start -// ==/UserScript== - -(function () { - "use strict"; - function i() { - const siteConfigs = { - twitter: { - chunkObject: "webpackChunk_twitter_responsive_web", - patches: [ - { - find: "(window.__INITIAL_STATE__", - replace: { - match: /var .{1,3}=.\..\(window\.__INITIAL_STATE__/, - replacement: (orig) => `console.log('Patches work!!!');${orig}`, - }, - }, - ], - inspectAll: true - }, - discord: { - chunkObject: "webpackChunkdiscord_app", - patches: [], - inspectAll: false - } - }; - let thisConfig = siteConfigs["twitter.com"]; - switch (window.location.host) { - case "discord.com": - case "ptb.discord.com": - case "canary.discord.com": - thisConfig = siteConfigs.discord; - break; - case "twitter.com": - case "mobile.twitter.com": - thisConfig = siteConfigs.twitter; - break; - default: - return; - } - - let realWebpackChunk = window[thisConfig.chunkObject]; - - const hasInjected = false; - Object.defineProperty(window, thisConfig.chunkObject, { - set: function set(value) { - // Don't infinitely re-wrap .push() - if (!value.push.__injected) { - realWebpackChunk = value; - const webpackPush = value.push; - - value.push = function (chunk) { - if (!webpackPush.__injected) { - patchModules(chunk[1]); - } - return webpackPush.apply(this, arguments); - }; - - value.push.__injected = true; - console.log("injected " + thisConfig.chunkObject); - } - }, - get: function get() { - return realWebpackChunk; - }, - configurable: true, - }); - - function patchModules(modules) { - for (let id in modules) { - let funcStr = Function.prototype.toString.apply(modules[id]); - - const patchesToApply = []; - for (let patch of thisConfig.patches) { - // todo regex matches - if (funcStr.indexOf(patch.find) != -1) { - patchesToApply.push(patch.replace); - } - } - - for (let toApply of patchesToApply) { - funcStr = funcStr.replace(toApply.match, toApply.replacement); - } - - if (patchesToApply.length > 0 || thisConfig.inspectAll) { - modules[id] = new Function( - "module", - "exports", - "webpackRequire", - "(" + - funcStr + - ").apply(this, arguments)\n//# sourceURL=Webpack-Module-" + - id + - "\n" - ); - } - } - } - } - // Early loading hack - const script = document.createElement("script"); - script.text = "(" + i.toString() + ")()"; - document.documentElement.appendChild(script); - })(); - \ No newline at end of file