Skip to content

Commit

Permalink
okay actually test everything
Browse files Browse the repository at this point in the history
  • Loading branch information
adryd325 committed Nov 22, 2023
1 parent 5d5dba1 commit c895fe9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 77 deletions.
14 changes: 6 additions & 8 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -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',
],
})
outfile: "dist/webpackToolsRuntime.js",
logLevel: "info",
target: ["es2020"],
});
16 changes: 7 additions & 9 deletions build/dev.js
Original file line number Diff line number Diff line change
@@ -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()
await context.watch();
107 changes: 53 additions & 54 deletions dist/webpackTools.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
});
})();

}
if (!thisSiteConfig) {
return;
}

unsafeWindow.__webpackTools_siteConfig = thisSiteConfig;

GM_addElement("script", {
textContent: GM_getResourceText("runtimeScript"),
});
})();
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import config from "./config";
import {interceptWebpack} from "./patcher";

// todo: magicrequire everywhere impl
Expand Down
3 changes: 2 additions & 1 deletion src/patcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 12 additions & 4 deletions src/wpTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !(
Expand All @@ -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),
};
});
}

Expand Down

0 comments on commit c895fe9

Please sign in to comment.