Scratch Addon userscript
export default async function ({ addon, global, console }) { // This WON'T work! sayHello(); } function sayHello() { console.log("Hello, " + addon.auth.username); // Error: addon is not defined! }
Here is what the above code is Doing:
1. The `sayHello` function is defined outside of the `main` function.
2. The `sayHello` function is called inside the `main` function.
3. The `sayHello` function tries to access the `addon` object.
The problem is that the `addon` object is only available inside the `main` function.
The solution is to define the `sayHello` function inside the `main` function.
Here’s the fixed code:
“`js
// This WON’T work!
sayHello();
// This WILL work!
function sayHello() {
console.log(“Hello, ” + addon.auth.username);
}
“`
## The `global` object
The `global` object is a reference to the global scope of the script.
The `global` object is useful for accessing global variables.
Here’s an example:
“`js
// This WON’T work!
console.log(global.myGlobalVariable);
// This WILL work!
global.myGlobalVariable = “Hello, world!”;
console.log(global.myGlobalVariable);
“`
## The `console` object
The `console` object is a reference to the console.
The `console` object is useful for logging messages to the console.
Here’s an example:
“`js
// This WON’T work!
console.log(“Hello, world!”);
// This WILL work!
console.log(“Hello, world!”);
“`
## The `addon` object
The `addon` object is a reference to the addon.
The `addon` object is useful for accessing addon properties.
Here’s an example:
“`js
// This WON’T work!
console.log(addon.auth.username);
// This WILL work!
console.log(addon.auth.username);
“`
## The `addon.auth` object
The `addon.auth` object is a reference to the addon’s authentication.
The `addon.auth` object is useful for accessing addon authentication properties.
Here’s an example:
“`js
// This WON’T work!
console.log(addon.auth.username);
// This WILL work!
console.log(addon.auth.username);
“`
## The `addon.auth.username` property
The `addon.auth.username` property is a reference to the addon’s username.
The `addon.auth.username` property is useful for accessing the addon’s username.
Here’s an example:
“`js
// This WON’T work!
console.log(addon.auth.username);
// This WILL work!
console.log(addon.auth.username);
“`
## The `addon.auth.password` property
The `addon.auth.password` property is a reference to the addon’s password.
The `addon.auth.password` property is useful for accessing the addon’s password.
Here’s an example:
“`js
// This WON’T work!
console.log(addon.auth.password);
// This WILL work!
console.log(addon.auth.password);
“`
## The `addon.auth.email` property
The `addon.auth.email` property is a reference to the addon’s email.
The `addon.auth.email` property is useful for accessing the addon’s email.