|
|
Available in userscripts |
✔️ |
Available in popup scripts |
❌ |
Required manifest permissions |
None |
Description
Allows addon userscripts to get information about the tab they’re currently running on.
Examples
Using addon.tab.waitForElement
We can use addon.tab.waitForElement
with {"markAsSeen": true}
inside a while(true)
loop to store the IDs of all comments in the page.
Using addon.tab.waitForElement
, we don’t need to have code that waits until comments have loaded. This way, we also store the IDs of newly posted comments and new loaded comments when the user clicks “load more comments”.
const commentIds = [];
while (true) {
const comment = await addon.tab.waitForElement("div.comment", {
markAsSeen: true,
});
commentIds.push(comment.id);
}
Using addon.tab.displayNoneWhileDisabled
(dynamicDisable
)
We use addon.tab.displayNoneWhileDisabled
to hide an image when the addon gets disabled.
We create a button to hide the image when clicked, and the image succesfully gets hidden, even if the addon is enabled.
We also set the display
CSS property of the image to flex
when visible, even though that is not the default value for images.
/* userscript.js */
const img = document.createElement("img");
img.classList.add("sa-example-img");
addon.tab.displayNoneWhileDisabled(img, { display: "flex" });
const btn = document.createElement("btn");
btn.onclick = () => {
// We want to hide the image
// We cannot do `img.style.display = "none"` because we
// used displayNoneWhileDisabled with the same element
img.classList.add("sa-example-img-hide");
};
/* userstyle.css */
.sa-example-img {
display: flex;
}
.sa-example-img-hide {
/* We want to hide the image if the button was clicked,
even if the addon is enabled */
display: none !important;
}
Reacting to URL dynamically changed
addon.tab.addEventListener("urlChange", function(event) {
console.log(`URL changed! It was previously ${event.detail.oldUrl}, it's now ${event.detail.newUrl}`);
});
Sub-APIs
Allows addons to get direct references to objects, which are particularly useful for enhancing the editor, like the Scratch VM or the Blockly instance.
Allows addons to get and modify Redux state, which is useful for addons that modify scratch-www pages and the editor.
Properties
addon.tab.clientVersion
Value |
"scratch-www" | "scratchr2" |
Nullable |
Yes |
The Scratch client for the current tab (scratch-www
or scratchr2
).
The Scratch community website has 2 working clients used throughout the site.
scratch-www
is React/Redux based and client side rendered. This client is the one used in the homepage.
scratchr2
is Django/jQuery/Backbone.js based and mostly server side rendered. This client is the one used in profile pages.
addon.tab.editorMode
Value |
"projectpage" | "editor" | "fullscreen" | "embed" |
Nullable |
Yes |
The current viewing mode for the project (projectpage
, editor
, fullscreen
or embed
).
Will be null
if the current tab is not a project.
addon.tab.direction
Value |
"ltr" | "rtl" |
Nullable |
No |
The writing direction for the language of the Scratch website.
Methods
addon.tab.waitForElement
Parameter |
Type |
Required |
Description |
selector |
String |
Yes |
One or more selectors to match. Same parameter as document.querySelector . |
options |
Object |
No |
Property |
Type |
Required |
Default |
Description |
markAsSeen |
Boolean |
No |
false |
Whether it should mark resolved elements to be skipped next time or not. |
condition |
Function |
No |
() => true |
A function that returns whether to resolve the selector or not. No arguments are passed. |
reduxCondition |
Function |
No |
() => true |
A function that returns whether to resolve the selector or not. addon.tab.redux is passed as an argument. |
reduxEvents |
String[] |
No |
[] |
An array of Redux events that must be dispatched before resolving the selector. |
|
Return value |
Promise<HTMLElement> |
Waits until an element exists, then returns the element.
Internally, a MutationObserver
that reacts to any DOM tree change is used. This observer does not react to attribute-only DOM updates.
Option markAsSeen
should be set to true if you’re using this method inside a while(true)
loop.
Options condition
, reduxCondition
and reduxEvents
should be used as optimizations, in order to avoid multiple calls to document.querySelector
when it’s guaranteed the element will not exist yet.
addon.tab.displayNoneWhileDisabled
Parameter |
Type |
Required |
Description |
element |
HTMLElement |
Yes |
Element to hide |
options |
Object |
No |
Property |
Type |
Required |
Default |
Description |
display |
String |
No |
"" |
The display CSS value to use when the addon is enabled. |
|
Hides the given element with display: none
when the addon is disabled, until it is reenabled.
If the intended display
CSS property value for the provided element when visible is not the default value for the type of provided element (for example, block
for div
s and inline
for span
s), you should provide that value inside the options parameter.
If you want to manually hide the element in situations where the addon is enabled, you should use a dedicated class name for that, instead of manually setting el.style.display = "none";
. Use a class name selector in a userstyle to set display: none !important;
on the element.
addon.tab.copyImage
Required manifest permissions |
clipboardWrite |
Parameter |
Type |
Required |
Description |
dataURL |
String |
Yes |
Data URL of a PNG image. |
Return value |
Promise<void> |
Promise rejects if |
Image could not be copied. |
Copies a PNG image to the clipboard.
Only run this in response of the user explicitly pressing Ctrl+C.
Internally uses browser.clipboard.setImageData
in Firefox and navigator.clipboard.write
in Chrome and Edge.
addon.tab.scratchClass
Parameter |
Type |
Required |
Description |
unhashedClassName |
String |
Min 1 |
One or many unhashed Scratch stylesheet class names, as one argument each. |
opts |
Object |
No |
Property |
Type |
Required |
Description |
others |
String | String[] |
Yes |
Non-Scratch class(es) to merge |
|
Gets the hashed class name for a Scratch stylesheet class name.
addon.tab.scratchMessage
Parameter |
Type |
Required |
Description |
key |
String |
Yes |
The Scratch translation key. |
Gets Scratch translation from the current Scratch tab.
Note that these are Scratch locales, not Scratch Addons locales.
If the message isn’t found, ""
is returned and a warning is logged in the console.
Internally uses window.django.gettext
or window._messages
.
See addon.tab.appendToSharedSpace.
addon.tab.createModal
Parameter |
Type |
Required |
Description |
title |
string |
Yes |
The title of the modal. |
options |
object |
No |
Parameter |
Type |
Default |
Description |
isOpen |
Boolean |
false |
Whether to open the modal by default. |
useEditorClasses |
Boolean |
false |
If in the editor, whether to apply the editor styles instead of the scratch-www ones. |
useSizesClass |
Boolean |
false |
If on scratch-www , whether to add the modal-sizes class. |
|
Returns a blank modal using Scratch’s styles. The modal’s properties are listed below.
Property |
Type |
Description |
container |
HTMLElement |
The container element. |
content |
HTMLElement |
Where the content should be appended. |
backdrop |
HTMLElement |
The modal overlay. |
closeButton |
HTMLElement |
The close (X) button on the header. |
open |
Function |
Opens the modal. |
close |
Function |
Closes the modal. |
remove |
Function |
Removes the modal, making it no longer usable. |
addon.tab.confirm
Parameter |
Type |
Required |
Description |
title |
string |
Yes |
The title of the modal. |
message |
string |
Yes |
The message displayed in the modal. |
options |
object |
No |
Parameter |
Type |
Default |
Description |
useEditorClasses |
Boolean |
false |
If in the editor, whether to apply the editor styles instead of the scratch-www ones. |
okButtonLabel |
string |
"OK" |
The label of the button for approving the confirmation. |
cancelButtonLabel |
string |
"Cancel" |
The label of the button for rejecting the confirmation. |
|
Return value |
Promise<Boolean> |
Similar to window.confirm
, except it’s asynchronous and uses Scratch’s styles.
addon.tab.prompt
Parameter |
Type |
Required |
Description |
title |
string |
Yes |
The title of the modal. |
message |
string |
Yes |
The message displayed in the modal. |
defaultValue |
string |
No |
The initial value of the text box. |
options |
object |
No |
Parameter |
Type |
Default |
Description |
useEditorClasses |
Boolean |
false |
If in the editor, whether to apply the editor styles instead of the scratch-www ones. |
|
Return value |
Promise<string | null> |
Similar to window.prompt
, except it’s asynchronous and uses Scratch’s styles.
Parameter |
Type |
Required |
Description |
blockContextMenuCallback |
Function |
Yes |
Returns new menu items.
Parameter |
Type |
Description |
items |
Object[] |
The items added by vanilla code or other addons.
Property |
Type |
Description |
enabled |
Boolean |
Whether the item is enabled or not. |
text |
String |
The context menu item label. |
callback |
Function |
The function that is called when the item is clicked. |
separator |
Boolean |
Whether to add a separator above the item or not. |
|
block |
Object |
The targeted block, if any. |
|
conditions |
Object |
No |
Show the context menu item in various locations.
Property |
Type |
Required |
Default |
Description |
workspace |
Boolean |
No |
false |
The workspace context menu. |
blocks |
Boolean |
No |
false |
The block context menu outside the flyout. |
flyout |
Boolean |
No |
false |
The block context menu inside the flyout. |
comments |
Boolean |
No |
false |
The context menu on comments. |
|
Adds a context menu item for any of the context menus in the code editor.
Documentation for this is a WIP. Not all possible types are listed for some settings.
Parameter |
Type |
Required |
Description |
callback |
Function |
Yes |
The function that is executed when the item is clicked.
Parameter |
Type |
Description |
context |
Object |
The context for the action.
Property |
Type |
Description |
type |
String |
The type of the context menu. Possible values: "sprite", "costume", "sound" |
menuItem |
HTMLElement |
The item element. |
target |
HTMLElement |
The element that this context menu is selecting. |
index |
Number |
The index, if applicable. |
|
|
options |
Object |
No (but then it must be returned in `callback`) |
Property |
Type |
Required |
Default |
Description |
className |
String |
Yes |
|
The class name to add to the item. |
types |
String[] |
No |
All possible values |
Which types of context menu the item should be added to. Possible values: "sprite", "costume", "sound" |
position |
String |
Yes |
|
The shared space inside of the context menu that the item should be added to. Uses `addon.tab.appendToSharedSpace` internally. Possible values: "assetContextMenuAfterExport", "assetContextMenuAfterDelete", "monitor" |
order |
Number |
Yes |
|
The order of the item within its shared space in relation to other items. |
label |
String |
Yes |
|
The label for the item. |
border |
Boolean |
No |
false |
Whether to add a border to the top of the item or not. |
dangerous |
Boolean |
No |
false |
Whether to display the item as "dangerous" (orange-colored) or not. |
condition |
Function |
No |
true |
A function that determines if the item should be shown.
Parameter |
Type |
Description |
context |
Object |
The context for the action.
Property |
Type |
Description |
type |
String |
The type of the context menu. Possible values: "sprite", "costume", "sound" |
menuItem |
HTMLElement |
The item element. |
target |
HTMLElement |
The element that this context menu is selecting. |
index |
Number |
The index, if applicable. |
|
|
|
Adds a context menu item for any of the non-Blockly context menus, such as the context menu for the sprites list.
addon.tab.addBlock
Do not use this unless you are adding blocks to the debugger addon.
Parameter |
Type |
Required |
Description |
proccode |
String |
Yes |
The name that will be used to refer to the block internally. Must specify inputs on the block (if any) using %s (string), %n (number-only), and %b (boolean). |
blockData |
Object |
Yes |
Property |
Type |
Required |
Default |
Description |
args |
String[] |
Yes |
|
A list of names that will be used to refer to the block's inputs in the callback. If there are no inputs, use an empty array. |
displayName |
String |
no |
The value of `proccode` |
The name that will be displayed on the block to the user. Must also include block input syntax. |
callback |
Function |
Yes |
|
The function that will execute when the block runs.
Parameter |
Description |
args |
An object containing the values that are entered into the block inputs. The keys correpond to the `args` property specified earlier.
|
thread |
A reference to the thread that this block is running in. |
|
hidden |
Boolean |
No |
false |
Whether the block should appear in the flyout or not. This does not affect the workspace. |
|
Adds a new block to the Debugger category in the block palette.
addon.tab.removeBlock
Parameter |
Type |
Required |
Description |
proccode |
String |
Yes |
The `proccode` value of the block to remove |
Removes a block that was previously added to the Debugger category in the block palette.
addon.tab.loadScript
In most cases, you should use the userscripts
property of the addon manifest instead.
Parameter |
Type |
Required |
Description |
path |
String |
Yes |
The path pointing to the script. |
Runs the specified script file relative to the extension’s root (e.g. chrome-extension://aeepldbjfoihffgcaejikpoeppffnlbd/
) in a <script>
tag.
addon.tab.loadWorker
Parameter |
Type |
Required |
Description |
path |
String |
Yes |
The path pointing to the worker. |
Return value |
Promise<Worker> |
Loads the specified Web Worker.
Events
urlChange
Event detail property |
Type |
Description |
oldUrl |
String |
The URL before it was dynamically changed |
newUrl |
String |
The new current URL value |
Fires when Scratch dynamically changes the URL of the page.
This happens when going inside/outside the editor, or switching tabs in scratch-www studio pages.
This will not fire if location.hash
changed.
Section Pages
Comments
Make sure to follow the code of conduct. You can see this comment section on GitHub Discussions, as well as editing and removing your comment.