Type of an eval result event.
Code mix-in which when executed immediately removes the current script element from the DOM.
Code mix-in to evaluate some code and expose the return value in the DOM. It relies on PUSH_RESULT.
Requires code
, and uid
to be set using formatCode.
Works by wrapping code
in an async function and
pushing the result after finishing.
Code mix-in which exposes the pushResult(value: any, key: string)
function
to "expose" a value in an element so that it can be accessed from the extension context.
Requires the variable uid
to be set using formatCode.
Observable for all eval results
Evaluate code
in the context of the page window and return its result.
This function can be used to get access to Javascript variables
because extensions don't have access to the page's Window object.
The return value needs to be JSON serializable!
Supports both sync and async code. If you don't use an explicit return statement your
code will be prefixed with return
. This can be used to create simple eval statements:
// title holds the document title (note: there's no need to use evaluateCode for this)
const title = await evaluateCode(`document.title`);
A simple formatter that can be used to format template code.
The code uses {{name}}
syntax to indicate templates which can be replaced by providing {name: "value"}
to the function.
This function is very basic and shouldn't be used for more complex templates!
Generate a random id. This function is used to communicate between the windows by giving each evaluation context its own id.
Inject javascript code to be run in the context of the page's Window.
script tag that the code was injected to.
Generated using TypeDoc
Because extension content scripts run in their own sand-boxed environment they can't easily access the page's Javascript namespace. But this module helps you do exactly that.
You can use injectCode to simply run code in the page's context and evaluateCode if you need to pass a value back.
Using real functions in your code
The code needs to be written in Javascript that can be executed by the browser. It isn't processed in any way. If possible, write an actual function which you then convert to a string to leverage the power of TypeScript.
// write the function like you would for any other function, this way it'll // be compiled to the targeted javascript version function test(value: any): string { return value.toString(); } const code = ` // test now holds the compiled test function code const test = ${test.toString()}; return test(5); `; // returns "5" await evaluateCode(code);
Getting something from the page
Let's say you want to access the variable
USER_NAME
which the page conveniently exposes to the global scope, you can simply grab it using evaluateCode.const username = await evaluateCode(`USER_NAME`);
Running code
If you have to call a function in the page to perform some action, say, changing the colour of the background (called "changeBackgroundColour"), you can use injectCode to simply run it.
injectCode(`changeBackgroundColour("green");`);