headA event & call interception library for javascript. Censor monkeypatches your custom handles to any existing object or class' functions, attributes and events.
Here is an example of intercepting all proceeding fetch requests and load events:
censor(window).whenCall("fetch", async (ctx) => {
console.log(ctx.args) // Log the arguments passed to fetch
var result = await ctx.pass() // Run the original fetch function
console.log(result.status) // Log the result status
return result // Pass the response
}).on("load", (ctx, event) => {
console.log("loaded") // Signifiy that the event was run
return ctx.pass() // Run the original handler
})
fetch("https://echo.zuplo.io").catch((error) => {
console.error(error.message)
})
import { censor } from "https://cdn.jsdelivr.net/gh/thatrandomperson5/censorjs@master/release/censor.module.min.js"
or
<script src="https://cdn.jsdelivr.net/gh/thatrandomperson5/censorjs@master/release/censor.min.js"></script>
There are two main parts to the Censor process:
WebSocket or window)CensorObject or CensorClass instance)All censor handles applied via the censoring object will directly and permenantly affect the censored instance. You can think of the censoring object as a interface that allows for easy creation of censor handles, not directly connected to any handles themselves.
Before you continue, you should know the difference between censoring a instance vs. a class. When you censor a instance, you replace current and future calls and triggers with glorified wrappers that handle it your way before passing back to the original. Censoring a class simply designates a initilization function so that when anyone creates a new instance of that class, it automatically comes with a censor on that instance. You can think of this as a "group" of sorts so that you can apply one set of handles to any object of your choice.
The simplest, reccommended way of creating a censoring object is using the censor function.
var windowCensor = censor(window)
var webSocketCensor = censor(WebSocket)
You can also directly create the classes, but that makes inline chains more difficult (eg. censor(something).first().second())
var windowCensor = new CensorObject(window)
var webSocketCensor = new CensorClass(WebSocket)
Instance censors and class censors are implemented automatically, but class censors can also be set manually in the scope.
var webSocketCensor = censor(WebSocket, null, null) // Last null signifies that you are manually implementing it.
WebSocket = webSocketCensor.genFunc() // Implement your class censoring
| Name | Description | Pass/Next Effect | Step Graph |
|---|---|---|---|
.whenCall(name, handle) |
Register a handle that is called instead whenever name is called. |
Call the original function behind name |
Outside Call -> Handle -> Original Function -> Handle Return |
.whenAttr(name, handles) |
Sets a custom setter and getter for property with name. |
Get or set the property | Outside Set/Get -> Handle -> Internal Get/Set -> Handle Return |
.on(event, handle) |
A event intercepter that intercepts all event handles of the event event |
Call the original handle function. | Event Triggered -> Handle -> Original Handle -> Handle Return |
.whenCreate(handle) |
A instance creation interceptor (before other intercepters are added) | Call the original new ClassName(...args) function. |
Creation Called -> Handle -> Constructor -> Handle Return -> Other Interception Added -> Return Final |
Censor handles are constructed very similarly to the orignal handle/function with exception of the ctx object as the first object.
.next(...args) or .pass which passes argument automatically.The general structure goes something like this:
(ctx) => {
ctx.args.modify() // Change it to your wish
var result = ctx.pass()
result.modify() // Change the result
return result;
}
The whenCall function supports async handles. These will should be used when the original function is async and can be enabled by passing async (ctx, arg1) => {doSomething} as a handler.
Coming soon
softRefresh example (installing eruda mobile console before anything else loaded)
softRefresh(
() => {
alert("Hello world")
eruda.init()
},
{
resources: ["https://cdn.jsdelivr.net/npm/eruda"],
},
)
<script id="test1">
function log(txt) {
document.getElementById("outp").textContent += txt.toString() + "\n"
}
function test1() {
censor(window, true).whenCall("fetch", async (ctx) => {
log(ctx.args)
var result = await ctx.pass()
log(result.status)
return result
})
window.fetch("https://echo.zuplo.io").catch((error) => {
console.error(error.message)
})
document.getElementById("btn").onclick = () => {
alert("Hello")
}
censor(document.getElementById("btn")).on("click", (ctx, event) => {
log(ctx.args)
return ctx.pass()
})
document.getElementById("btn").addEventListener("click", () => {
log("EVENT CLICK")
})
}
function test2() {
var webSocketCensor = censor(WebSocket, "WebSocket")
webSocketCensor.whenCall("send", (ctx, data) => {
console.log(data)
return ctx.pass()
})
webSocketCensor.on("message", (ctx, event) => {
console.log(event.data)
return ctx.pass()
})
let ws = new WebSocket("wss://echo.websocket.org/")
ws.onopen = (event) => {
ws.send("Here's some text that the server is urgently awaiting!")
}
ws.onmessage = (event) => {
log(event.data)
}
}
function test3() {
var urlc = censor(URL)
urlc.whenCreate((ctx) => {
log(ctx.args)
return ctx.pass()
})
let url = new URL("https://google.com")
}
</script>
<pre><code id="outp"></code></pre>
<button id="btn">Button</button>
All functions, variables and classes and their types.
Other documentation versions can be found here.
The main module of the Censor library. Contains all core features.
censor(window).whenCall("fetch", async (ctx) => {
console.log(ctx.args) // Log the arguments passed to fetch
var result = await ctx.pass() // Run the original fetch function
console.log(result.status) // Log the result status
return result // Pass the response
}).on("load", (ctx, event) => {
console.log("loaded") // Signifiy that the event was run
return ctx.pass() // Run the original handler
})
fetch("https://echo.zuplo.io").catch((error) => {
console.error(error.message)
})
Type: Object
(bool?)
(bool?)
(any?)
(bool?)
(function (): void?)
(function (any): void?)
A class for providing context and interaction within the Censor handle.
(CensorObject)
The connected Censor object.
(string)
The name of the attribute, function, or event that is currently connected.
Arguments that are passed to the context when. Usually function arguments.
Type: Array<any>
The connected Censor object.
Type: CensorObject
The "original" based on situation.
Type: function (...any): any
The object that the Context is attached to.
Type: any
Run the original function. Run after modifications to input but before output.
(...any)
Modified arguments to pass to the original function.
any:
The result of the original function.
Run the original function. Run after modifications to input but before output. (Automatically passes arguments from context)
any:
The result of the original function.
Shallow copy the current object.
CensorContext:
the copied objevt
A class for providing context and interaction within the Censor handle.
Extends CensorContext
The main callback function type. Note that the effects of .pass()/.next() change based om where the context was assigned from. (See Pass/NextEffect)
Type: Function
((CensorContext | CensorCallContext))
The passed context.
(...any)
Any number of arguments passed to the context.
any:
The result you want to be passed to the original.
The main Censor class. Applies censor effects to any object, and effects are independent of the Censor class object.
(Object)
The base object.
A type assertion util based on typeof
A recursive version of getOwnPropertyDescriptor, searching the inheritance tree.
(Object)
The object to search.
(string)
The name of the property to retrive.
(number
= 10)
The maximum search depth.
PropertyDescription:
The found property descriptor.
Register a handle for when function with name is called from base object
(string)
The name of the function.
(genericHandle)
The handler function.
CensorObject:
Returns self for chaining.
Register a handle for when a attribute with name is modified or retrived from base object
(string)
The name of the function.
(Object)
The handler functions.
| Name | Description |
|---|---|
handles.get genericHandle?
|
The get handler for the attribute. |
handles.set genericHandle?
|
The set handler for the attribute. |
CensorObject:
Returns self for chaining.
Register a handle for when a event is triggered and responded to from base object
(string)
The name of the event.
(genericHandle)
The handle the will be applied to all listeners.
CensorObject:
Returns self for chaining.
Very similar to CensorObject, used to censor uninitiated classes on initiation. whenCall, whenAttr and on are all directly equivilent to their CensorObject counterparts.
(function (...any): Object)
The class descriptor function.
((string | null)
= null)
The name this class is accessed by publicly. (For example,
WebSocket
is internally named
E
, so this would be needed)
((Object | null)
= window)
The object to automatically implement the result
genFunc()
onto.
Register a handle for when a instance of the linked class is created.
(genericHandle)
The handle the will be applied to when it is created.
CensorObject:
Returns self for chaining.
Apply all censor handles to a instance of the given class.
(Object)
A instance of the given class or any other fitting object.
CensorClass:
Returns self for chaining.
Properly censor the given object if able
(any)
The object given as a base.
(...any)
Options to pass to the Censor constructor.
(CensorObject | CensorClass):
The Censor object used to apply handles.
The link to the censor CDN, can be used in options.resources
Type: string
A hash function taken from here
number:
The hash
The callback type for softRefresh
Type: Function
Refresh a webpage with a handle the executes before any original code execution.
(softRefreshCallback)
The handle to execute.
(Object
= {})
The options object.
| Name | Description |
|---|---|
options.pass Object?
|
A json serializable object to pass to the handle after load. |
options.resources Array<(URL | string)>?
|
A list of links to libraries or other resources you want avalible in your handle. |
options.processer function (Document)?
|
Any custom operations on the document before the soft refresh. |
options.useCurrentState bool?
|
Use current html instead of fetching default. |
Promise:
The promise that resolves the function.