From c895fe90aaef9fb5d659f4d9ee642e137588ac63 Mon Sep 17 00:00:00 2001 From: adryd Date: Wed, 22 Nov 2023 17:42:25 -0500 Subject: [PATCH] okay actually test everything --- build/build.js | 14 +++-- build/dev.js | 16 +++--- dist/webpackTools.user.js | 107 +++++++++++++++++++------------------- src/index.js | 1 - src/patcher.js | 3 +- src/wpTools.js | 16 ++++-- 6 files changed, 80 insertions(+), 77 deletions(-) diff --git a/build/build.js b/build/build.js index 25b0e3a..9b45a61 100644 --- a/build/build.js +++ b/build/build.js @@ -1,11 +1,9 @@ -import * as esbuild from 'esbuild' +import * as esbuild from "esbuild"; await esbuild.build({ - entryPoints: ['src/index.js'], + entryPoints: ["src/index.js"], bundle: true, - outfile: 'dist/webpackToolsRuntime.js', - logLevel: 'info', - target: [ - 'es2020', - ], -}) \ No newline at end of file + outfile: "dist/webpackToolsRuntime.js", + logLevel: "info", + target: ["es2020"], +}); diff --git a/build/dev.js b/build/dev.js index 05a4f6b..b186e22 100644 --- a/build/dev.js +++ b/build/dev.js @@ -1,13 +1,11 @@ -import * as esbuild from 'esbuild' +import * as esbuild from "esbuild"; const context = await esbuild.context({ - entryPoints: ['src/index.js'], + entryPoints: ["src/index.js"], bundle: true, - outfile: 'dist/webpackToolsDev.user.js', - logLevel: 'info', - target: [ - 'es2020', - ], -}) + outfile: "dist/webpackToolsRuntime.js", + logLevel: "info", + target: ["es2020"], +}); -await context.watch() \ No newline at end of file +await context.watch(); diff --git a/dist/webpackTools.user.js b/dist/webpackTools.user.js index 026a294..3e866f2 100644 --- a/dist/webpackTools.user.js +++ b/dist/webpackTools.user.js @@ -13,60 +13,59 @@ // ==/UserScript== (() => { - const configs = [ - { - name: "discord", - matchSites: ["discord.com", "ptb.discord.com", "canary.discord.com"], - chunkObject: "webpackChunkdiscord_app", - webpackVersion: 5, - patches: [], - modules: [], - inspectAll: true - }, - { - 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}`, - }, + const configs = [ + { + name: "discord", + matchSites: ["discord.com", "ptb.discord.com", "canary.discord.com"], + chunkObject: "webpackChunkdiscord_app", + webpackVersion: 5, + patches: [], + modules: [], + inspectAll: true, + }, + { + name: "twitter", + matchSites: ["twitter.com"], + chunkObject: "webpackChunk_twitter_responsive_web", + webpackVersion: 5, + patches: [ + { + name: "patchingDemo", + find: "(window.__INITIAL_STATE__", + replace: { + match: /const .{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; - }, + }, + ], + modules: [ + { + name: "exposeWpRequire", + needs: [], + 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; + }, + ], + }, + ]; + + let thisSiteConfig; + for (let siteConfig of configs) { + if (siteConfig.matchSites?.includes(window.location.host)) { + thisSiteConfig = siteConfig; + break; } - - unsafeWindow.__webpackTools_siteConfig = thisSiteConfig; - - GM_addElement("script", { - textContent: GM_getResourceText("runtimeScript"), - }); - })(); - \ No newline at end of file + } + if (!thisSiteConfig) { + return; + } + + unsafeWindow.__webpackTools_siteConfig = thisSiteConfig; + + GM_addElement("script", { + textContent: GM_getResourceText("runtimeScript"), + }); +})(); diff --git a/src/index.js b/src/index.js index b8533f6..3fcab71 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -import config from "./config"; import {interceptWebpack} from "./patcher"; // todo: magicrequire everywhere impl diff --git a/src/patcher.js b/src/patcher.js index ccf7503..33eaca1 100644 --- a/src/patcher.js +++ b/src/patcher.js @@ -133,7 +133,8 @@ export function injectModules(chunk) { } // Patch our own modules, for fun :) - chunk[1] = Object.assign(chunk[1], patchModules(injectModules)); + patchModules(injectModules) + chunk[1] = Object.assign(chunk[1], injectModules); if (injectEntries.length > 0) { switch (config.webpackVersion) { case 5: diff --git a/src/wpTools.js b/src/wpTools.js index b262303..216fc09 100644 --- a/src/wpTools.js +++ b/src/wpTools.js @@ -8,7 +8,7 @@ export default function wpTools(module, exports, webpackRequire) { // TODO: recurse in objects function findModulesByExports(keysArg) { const keys = keysArg instanceof Array ? keysArg : [keysArg]; - Object.entries(webpackRequire.c) + return Object.entries(webpackRequire.c) .filter(([moduleId, exportCache]) => { return !keys.some((searchKey) => { return !( @@ -18,17 +18,25 @@ export default function wpTools(module, exports, webpackRequire) { ); }); }) - .map(([moduleId, exportCache]) => exportCache); + .map(([moduleId, exportCache]) => { + return { + id: moduleId, + exports: exportCache, + }; + }); } function findModulesByMatches(search) { - return Object.entires(webpackRequire.m) + return Object.entries(webpackRequire.m) .filter(([moduleId, moduleFunc]) => { const funcStr = Function.prototype.toString.apply(moduleFunc); return matchModule(funcStr, search); }) .map(([moduleId, moduleFunc]) => { - webpackRequire(moduleId); + return { + id: moduleId, + exports: webpackRequire(moduleId), + }; }); }