Skip to content

Commit

Permalink
📝
Browse files Browse the repository at this point in the history
- mark non-public API as internal in docs
- add typedoc.json for configuration
- update README
  • Loading branch information
pixeldesu committed Jul 25, 2021
1 parent b58d224 commit 1316c25
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
54 changes: 16 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<img width=200px src='.github/logo.png?raw=true'>

<h1>moduleRaid</h1>
<p>moduleRaid is a utility to get modules and module constructors from <code>webpackJsonp</code> functions embedded on websites by Webpack. It also provides functions to search through returned modules.</p>
<p>moduleRaid is a utility to get modules and module constructors from <code>webpackJsonp</code> functions (or similar endpoints) embedded on websites by Webpack. It also provides functions to search through returned modules.</p>
</div>

<h2 align='center'>Installation</h2>

You can get moduleRaid over npm

```
```shell
$ npm install moduleraid
```

Expand All @@ -19,8 +19,7 @@ Or if you directly want to use it in the browser
<script src="https://unpkg.com/moduleraid/dist/moduleraid.iife.js"></script>
```

Alternatively, just copy the script from `dist/moduleraid.min.js` and run it in a devtool console
on the site of your choosing.
Or alternatively, just copy the contents from above unpkg link into the devtools console on a website!

<h2 align='center'>Usage</h2>

Expand All @@ -32,47 +31,26 @@ includes a Webpack build!
```js
import ModuleRaid from 'moduleRaid'

window.mR = new ModuleRaid()
const mR = new ModuleRaid()
```

### The `moduleraid` object

Once `moduleRaid` is run or included on a page that includes a Webpack build (usually noted by a `webpackJsonp` function), it
will return a object, containing:

* `modules`: An object containing all modules we could get from Webpack
* `constructors`: An array containing all module constructor functions
* `get(id)`: Get the module from the specified `id`
* `findModule(query)`: Return the modules that have `query` as a key in its exports (`query` can be either a string or a function)
* `findConstructor(query)`: Return constructor functions and modules that include `query` (`query` can be either a string or a function). Returns an array of tuples, where the first element is the constructor, and the second is the module.

If you run the code in devtools or load it as external script from unpkg/etc. the `moduleRaid` object can be found in `window.mR` by default.

**Note:** If moduleRaid had to get modules through iteration, `constructors` will be empty and so `findFunction` will not work.

#### Debug Mode
### Examples

If you call moduleRaid with an optional argument `true`, you will enable debug output. Debug output will show errors that are normally supressed.
Now, with the `mR` instance available and modules being fetched, you can use the two available `find*()` methods to search for modules!

In the version that is minified and you can't just add another argument easily, simply run `window.mRdebug = true` before adding the script and you should
be fine!

<h2 align='center'>How it works</h2>

There already was a script basically doing the same as `moduleRaid` some months earlier, called [`webcrack`](https://gist.github.com/twilight-sparkle-irl/cb63762000e606e50690911cac1bcead) (made by [twilight-sparkle-irl](https://github.com/twilight-sparkle-irl)), which was rendered obsolute due to structural changes in how you can access Webpack modules from the outside.

This library is an effort to bring back the ability to inspect all available modules, for debugging or userscript purposes.

As noted above, Webpack exposes a function `webpackJsonp` containing all the code that has been bundled with Webpack. The function takes three
parameters, all of them being an array. The first two don't seem to really matter, the last one is interesting, it seems to directly return
a module given an index.
```js
let results = mR.findModule('coolFeature')
// => Array of fitting modules for the search query

So, in a brute-forcy manner we simply run a `while` over `webpackJsonp([], [], [i])` until we get an exception from Webpack (trying to run `call`
on `undefined`), and now we have all modules (_or most of them_)!
let constRes = mR.findConstructor('_internal')
// => Array of fitting constructor/module tuples for the search query
```

<h2 align='center'>Known Issues</h2>
For more in-depth documentation around what you can use of moduleRaid, you can visit the **[API Documentation](https://moduleraid.netlify.app/)**!
<h2 align='center'>Special Thanks</h2>

* There seem to be a lot of empty modules, I'm not sure if these are _padding_ or something is missing here
* [twilight-sparkle-irl](https://github.com/twilight-sparkle-irl) for [`webcrack`](https://gist.github.com/twilight-sparkle-irl/cb63762000e606e50690911cac1bcead), of which the initial module extraction was based on
* [pedroslopez](https://github.com/pedroslopez) for Webpack 5 support, which I backported from their fork

<h2 align='center'>License</h2>

Expand Down
10 changes: 6 additions & 4 deletions src/moduleraid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export class ModuleRaid {
/**
* The Webpack entrypoint present on the global window object
*
* @defaultValue `webpackJsonp`
* @default `webpackJsonp`
*/
private entrypoint: string

/**
* Option enabling or disabling debug output
*
* @defaultValue `false`
* @default `false`
*/
private debug: boolean

Expand Down Expand Up @@ -146,10 +146,10 @@ export class ModuleRaid {
}

/**
* Debug logging method, works when true is passed as an argument to the main
* moduleRaid function, or when window.mRdebug is set to true
* Debug logging method, outputs to the console when {@link ModuleRaid.debug} is true
*
* @param {*} message The message to be logged
* @internal
*/
private log(message: string): void {
if (this.debug) {
Expand All @@ -160,6 +160,7 @@ export class ModuleRaid {
/**
* Method to set an alternative getter if we weren't able to extract __webpack_require__
* from Webpack
* @internal
*/
private replaceGet(): void {
if (this.get === null) {
Expand All @@ -170,6 +171,7 @@ export class ModuleRaid {
/**
* Method that will try to inject a module into Webpack or get modules
* depending on it's success it might be more or less brute about it
* @internal
*/
private fillModules(): void {
if (typeof webpackJsonp === 'function') {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type ModuleLike =

/**
* Type describing generic contents of default modules
* @internal
*/
export type DefaultModuleLike = {
default: Record<string, unknown>
Expand All @@ -43,6 +44,7 @@ export type ConstructorModuleTuple = [AnyFunction, ModuleLike]

/**
* Type describing possible arguments to `webpackJsonp`
* @internal
*/
export type WebpackArgument = [
[number] | [string] | [],
Expand All @@ -57,13 +59,15 @@ export type WebpackArgument = [

/**
* Type describing the contents of the Webpack module cache
* @internal
*/
export type WebpackModuleCache = {
[key: string]: { exports: ModuleLike }
}

/**
* Type describing the prototype contents of the __webpack_require__
* @internal
*/
export interface WebpackRequire {
c: WebpackModuleCache
Expand All @@ -77,6 +81,7 @@ export type WebpackRequireFunction = (key: string | number) => ModuleLike

/**
* Type describing Webpack module constructors
* @internal
*/
export type WebpackModuleFunction =
| ((e: unknown, t: unknown, i: WebpackRequire) => void)
Expand Down
6 changes: 6 additions & 0 deletions typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"entryPoints": ["src/"],
"exclude": ["src/index.ts"],
"excludeInternal": true,
"readme": "none"
}

0 comments on commit 1316c25

Please sign in to comment.