Skip to content

Commit c99081a

Browse files
Hans5958WorldLanguages
authored andcommitted
Page title and heading changes
1 parent 72248cf commit c99081a

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

content/docs/develop/userscripts/best-practices.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
title: Best practices for userscripts
2+
title: Best Practices
33
description: Follow these best practices when writing or reviewing userscript code.
44
---
55

66
Follow these best practices when writing or reviewing userscript code.
77

88

9-
### DOM manipulation
9+
## DOM manipulation
1010

1111

12-
#### Use addEventListener instead of "onevent"
12+
### Use addEventListener instead of "onevent"
1313

1414
Avoid setting "onevent" values on HTML elements, such as `onclick`. Instead, use `addEventListener`. This allows multiple addons to register the same event on the same element, without conflicting.
1515
It is still valid to use "onevent", but only for elements that were created by the same addon that is registering the event.
@@ -33,7 +33,7 @@ document.querySelector(".remix-button").addEventListener("click", () => {
3333
```
3434
{{< /admonition >}}
3535

36-
#### Avoid using innerHTML
36+
### Avoid using innerHTML
3737

3838
Avoid using `innerHTML`. Use `innerText` or `textContent` instead.
3939
Other APIs that can potentially lead to XSS vulnerabilities should be avoided too, such as `insertAdjacentHTML`, `outerHTML`, `document.write()`, etc.
@@ -54,7 +54,7 @@ document.querySelector(".sa-remix-button").append(span);
5454
```
5555
{{< /admonition >}}
5656

57-
#### Hide elements instead of removing them
57+
### Hide elements instead of removing them
5858

5959
Avoid calling `.remove()` on HTML elements, which in extreme cases, can cause the project page to crash.
6060
Addons may only use it for elements they themselves created, in specific situations.
@@ -76,16 +76,16 @@ document.querySelector(".remix-button").classList.add("sa-remix-button-hidden");
7676
```
7777
{{< /admonition >}}
7878

79-
#### Only use waitForElement when necessary
79+
### Only use waitForElement when necessary
8080

8181
Avoid using the `addon.tab.waitForElement` API if the element is guaranteed to exist. It will still work, and performance will not be heavily impacted, but it might confuse other developers that are reading the code. The usage of waitForElement should usually mean that there is at least 1 scenario where the element doesn't exist at that execution point.
8282
For example, it's not necessary to use waitForElement when searching for forum posts, unless the userscript was declared with `"runAtComplete": false`. In those cases, simply use `document.querySelectorAll()` normally.
8383

8484

85-
### JavaScript best practices
85+
## JavaScript best practices
8686

8787

88-
#### Use modern JavaScript
88+
### Use modern JavaScript
8989

9090
- Prefer newer APIs, such as `fetch()` over `XMLHttpRequest`.
9191
- Never use `==` for comparisons. Use `===` instead.
@@ -94,7 +94,7 @@ For example, `document.querySelector(".remix-button")?.textContent`.
9494
- Use `for ... of` loops or `.forEach()`.
9595
Avoid C style loops like `for (let i = 0; i < arr.length; i++)`.
9696

97-
#### Only use "let" over "const" if the variable may be reassigned
97+
### Only use "let" over "const" if the variable may be reassigned
9898

9999
{{< admonition info >}}
100100
We usually use `camelCase` to name variables, no matter if they're declared with "let" or "const".
@@ -114,7 +114,7 @@ const DEFAULT_ZOOM = 1.20;
114114
People reading your code may assume that a variable that was declared through the "let" keyword might be reassigned at some other point of the script. If that's not the case, use the "const" keyword instead.
115115
Remember that in JavaScript, declaring an object or an array as a "const", does not mean its values are frozen. Values in the object can still be changed, even if the variable itself cannot be reassigned.
116116

117-
#### Do not set global variables
117+
### Do not set global variables
118118

119119
Avoid setting properties on the global `window` object, unless you are polluting a global function such as `fetch()`.
120120
If multiple addons need to share information or functions between each other, create a JS module file and import it from both userscripts.
@@ -126,13 +126,13 @@ window.isDarkMode = true;
126126
```
127127
{{< /admonition >}}
128128

129-
#### Do not declare functions outside of the default export
129+
### Do not declare functions outside of the default export
130130

131131
There's no reason to declare functions outside the `export default async function(){}` function. JavaScript allows functions to be declared inside other functions.
132132

133133
You may move functions to separate JS module files (which aren't declared as userscripts in the addon manifest) if appropriate, but keep in mind that those imported files won't have access to the `addon` object, unless you expose a setup function that accepts it as an argument, and call the function in the userscript entry point.
134134

135-
#### Do not unpollute functions
135+
### Do not unpollute functions
136136

137137
Multiple addons might want to pollute the same function, such as Scratch VM methods, `XMLHttpRequest`, `fetch()` or `FileReader()`.
138138
In those cases, one of the userscripts will be polluting the real function, while the others will be polluting functions which were already polluted themselves. If, for example, the first userscript that polluted decides to unpollute (for example, by doing `window.fetch = realFetch`), then all other functions in the "pollution chain" are also lost, which is unexpected.
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
2-
title: Debugging userscripts
2+
title: Debugging Tips
33
description: Tips to easily debug userscripts, and edge cases to consider.
44
---
55

66
Tips to easily debug userscripts, and edge cases to consider.
77

8-
#### Tips
8+
## Tips
99

10-
#### It's not always necessary to reload the extension
10+
### It's not always necessary to reload the extension
1111

1212
It's not necessary to reload the extension by going to `chrome://extensions` when changing the source of an already existing JavaScript or CSS files. In those cases, reloading the page is enough.
1313

14-
#### Use the addon.* API from the console
14+
### Use the addon.* API from the console
1515

1616
For development, you may choose to expose the `addon` object as a global variable, so that it can be accessed within the browser console.
1717

@@ -22,47 +22,47 @@ export default async function ({ addon, console }) {
2222
}
2323
```
2424

25-
#### Set breakpoints with the "debugger" keyword
25+
### Set breakpoints with the "debugger" keyword
2626

2727
The `debugger;` keyword in JavaScript will freeze the page when ran, if the developer tools are open. Setting breakpoints is useful to inspect the value of local variables during execution.
2828

29-
#### Filter console messages by addon ID
29+
### Filter console messages by addon ID
3030

3131
Enter the addon ID on the "filter" console search bar to only view logs and warnings, as well errors logged with `console.error()`. Keep in mind that this will hide all exceptions, unless you're explicitly logging them in your code.
3232

3333

34-
### Edge cases
34+
## Edge cases
3535

3636

37-
#### Scratch project page and editor
37+
### Scratch project page and editor
3838

3939

40-
##### The DOM is destroyed after going inside or outside the editor
40+
#### The DOM is destroyed after going inside or outside the editor
4141

4242
Scratch creates all HTML elements each time the user clicks "see inside" or "see project page", and destroys the old ones.
4343
This can usually be fixed by using `addon.tab.waitForElement` or the `urlChange` event.
4444

45-
##### The Scratch editor language can be changed without a reload
45+
#### The Scratch editor language can be changed without a reload
4646

4747
Unlike the Scratch website, the Scratch editor will not reload when changing the language. When selecting a different language, Scratch might destroy and re-create some HTML elements.
4848

49-
##### Other situations to consider
49+
#### Other situations to consider
5050

5151
- The project editor may be used without a defined project ID (for example, when logged out).
5252
- The editor might switch from LTR to RTL (or viceversa) without requiring a page reload.
5353

5454

55-
#### Scratch website
55+
### Scratch website
5656

57-
##### scratch-www pages don't reload after logging in
57+
#### scratch-www pages don't reload after logging in
5858

5959
Unlike scratchr2 pages, scratch-www pages do not force a page reload after logging in. For example, if you go to a project page while being logged out, then log in, the page will not reload. This also affects studios, the messages page, etc.
6060
In contrast, all Scratch pages reload after logging out.
6161

62-
##### Project pages never return 404s
62+
#### Project pages never return 404s
6363

6464
Even if the project is unshared or doesn't exist, Scratch returns a 200 HTTP status code. The "our server is Scratch'ing its head" message is added dynamically to the page by Scratch.
6565

66-
##### Other situations to consider
66+
#### Other situations to consider
6767

6868
- Each of the 4 tabs inside studios have different URLs, but do not trigger a browser navigation. Addons that affect any of the 4 pages should run, no matter the initial URL.

0 commit comments

Comments
 (0)