Skip to content

Commit 7348ad4

Browse files
authored
Modernize remaining JS (mdn#40398)
1 parent eb0c3d0 commit 7348ad4

File tree

4 files changed

+38
-35
lines changed
  • files/en-us
    • mozilla/add-ons/webextensions
    • web/api/web_components/using_custom_elements

4 files changed

+38
-35
lines changed

files/en-us/mozilla/add-ons/webextensions/content_scripts/cloneinto/index.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sidebar: addonsidebar
99
This function provides a safe way to take an object defined in a privileged scope and create a [structured clone](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) of it in a less-privileged scope. It returns a reference to the clone:
1010

1111
```js
12-
var clonedObject = cloneInto(myObject, targetWindow);
12+
const clonedObject = cloneInto(myObject, targetWindow);
1313
```
1414

1515
You can then assign the clone to an object in the target scope as an expando property, and scripts running in that scope can access it:
@@ -53,7 +53,7 @@ This content script creates an object, clones it into the content window and mak
5353

5454
```js
5555
// content script
56-
var addonScriptObject = { greeting: "hello from your extension" };
56+
const addonScriptObject = { greeting: "hello from your extension" };
5757
window.addonScriptObject = cloneInto(addonScriptObject, window);
5858
```
5959

@@ -63,7 +63,7 @@ Scripts running in the page can access the object:
6363
// page script
6464
button.addEventListener(
6565
"click",
66-
function () {
66+
() => {
6767
console.log(window.addonScriptObject.greeting); // "hello from your extension"
6868
},
6969
false,
@@ -90,18 +90,18 @@ The content script can define an object, clone it, and pass it into this functio
9090

9191
```js
9292
// content script
93-
var addonScriptObject = { message: "hello from your extension" };
93+
const addonScriptObject = { message: "hello from your extension" };
9494
window.foo(cloneInto(addonScriptObject, window)); // "they said: hello from your extension"
9595
```
9696

9797
### Cloning objects that have functions
9898

99-
If the object to clone contains functions, you must pass the `{cloneFunctions:true}` flag, or you get an error. If you do pass this flag, then functions in the object are cloned using the same mechanism used in [`exportFunction`](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts/exportFunction):
99+
If the object to clone contains functions, you must pass the `{ cloneFunctions: true }` flag, or you get an error. If you do pass this flag, then functions in the object are cloned using the same mechanism used in [`exportFunction`](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts/exportFunction):
100100

101101
```js
102102
// content script
103-
var addonScriptObject = {
104-
greetMe: function () {
103+
const addonScriptObject = {
104+
greetMe() {
105105
alert("hello from your extension");
106106
},
107107
};
@@ -112,10 +112,10 @@ window.addonScriptObject = cloneInto(addonScriptObject, window, {
112112

113113
```js
114114
// page script
115-
var test = document.getElementById("test");
115+
const test = document.getElementById("test");
116116
test.addEventListener(
117117
"click",
118-
function () {
118+
() => {
119119
window.addonScriptObject.greetMe();
120120
},
121121
false,
@@ -124,11 +124,11 @@ test.addEventListener(
124124

125125
### Cloning objects that contain DOM elements
126126

127-
By default, if the object you clone contains objects reflected from C++, such as DOM elements, the cloning operation fails with an error. If you pass the `{wrapReflectors:true}` flag, then the object you clone contains these objects:
127+
By default, if the object you clone contains objects reflected from C++, such as DOM elements, the cloning operation fails with an error. If you pass the `{ wrapReflectors: true }` flag, then the object you clone contains these objects:
128128

129129
```js
130130
// content script
131-
var addonScriptObject = {
131+
const addonScriptObject = {
132132
body: window.document.body,
133133
};
134134
window.addonScriptObject = cloneInto(addonScriptObject, window, {
@@ -138,10 +138,10 @@ window.addonScriptObject = cloneInto(addonScriptObject, window, {
138138

139139
```js
140140
// page script
141-
var test = document.getElementById("test");
141+
const test = document.getElementById("test");
142142
test.addEventListener(
143143
"click",
144-
function () {
144+
() => {
145145
console.log(window.addonScriptObject.body.innerHTML);
146146
},
147147
false,

files/en-us/mozilla/add-ons/webextensions/content_scripts/exportfunction/index.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ exportFunction(changeMyName, window, {
6161

6262
```js
6363
// less-privileged scope: for example, a page script
64-
var user = { name: "Jim" };
65-
var test = document.getElementById("test");
64+
const user = { name: "Jim" };
65+
const test = document.getElementById("test");
6666
test.addEventListener(
6767
"click",
68-
function () {
68+
() => {
6969
console.log(user.name); // "Jim"
7070
window.changeMyName(user);
7171
console.log(user.name); // "Bill"
@@ -93,15 +93,15 @@ exportFunction(logUser, window, {
9393

9494
```js
9595
// less-privileged scope: for example, a page script
96-
var user = {
97-
getUser: function () {
96+
const user = {
97+
getUser() {
9898
return "Bill";
9999
},
100100
};
101-
var test = document.getElementById("test");
101+
const test = document.getElementById("test");
102102
test.addEventListener(
103103
"click",
104-
function () {
104+
() => {
105105
window.logUser(user);
106106
},
107107
false,
@@ -129,10 +129,10 @@ exportFunction(logUser, unsafeWindow, {
129129
function getUser() {
130130
return "Bill";
131131
}
132-
var test = document.getElementById("test");
132+
const test = document.getElementById("test");
133133
test.addEventListener(
134134
"click",
135-
function () {
135+
() => {
136136
window.logUser(getUser);
137137
},
138138
false,
@@ -151,7 +151,7 @@ This script defines a function and then exports it to a content window:
151151

152152
```js
153153
// extension-script.js
154-
var salutation = "hello ";
154+
const salutation = "hello ";
155155
function greetMe(user) {
156156
return salutation + user;
157157
}
@@ -162,7 +162,7 @@ Instead of using `defineAs`, the script can assign the result of `exportFunction
162162

163163
```js
164164
// extension-script.js
165-
var salutation = "hello ";
165+
const salutation = "hello ";
166166
function greetMe(user) {
167167
return salutation + user;
168168
}
@@ -173,7 +173,7 @@ Either way, code running in the content window's scope can call the function:
173173

174174
```js
175175
// page-script.js
176-
var greeting = foo("alice");
176+
const greeting = foo("alice");
177177
console.log(greeting);
178178
// "hello alice"
179179
```
@@ -184,7 +184,7 @@ Instead of attaching the function to the target's global `window` object, the ca
184184

185185
```js
186186
// page-script.js
187-
var bar = {};
187+
const bar = {};
188188
```
189189

190190
Now the extension script can attach the function to `bar`:
@@ -198,7 +198,7 @@ exportFunction(greetMe, window.bar, {
198198

199199
```js
200200
// page-script.js
201-
var value = bar.greetMe("bob");
201+
const value = bar.greetMe("bob");
202202
console.log(value);
203203
// "hello bob"
204204
```

files/en-us/mozilla/add-ons/webextensions/work_with_the_cookies_api/index.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,13 @@ The extension's UI uses a toolbar button ({{WebExtAPIRef("browserAction")}}) imp
128128
To handle the icon buttons the script first gathers all the class names used for the buttons in the HTML file. It then loops through all the buttons assigning them their image and creating an `onclick` listener for each button:
129129

130130
```js
131-
let bgBtns = document.querySelectorAll(".bg-container button");
131+
const bgBtns = document.querySelectorAll(".bg-container button");
132132

133-
for (let i = 0; i < bgBtns.length; i++) {
134-
let imgName = bgBtns[i].getAttribute("class");
135-
let bgImg = `url('images/${imgName}.png')`;
136-
bgBtns[i].style.backgroundImage = bgImg;
133+
for (const btn of bgBtns) {
134+
const imgName = btn.getAttribute("class");
135+
btn.style.backgroundImage = `url('images/${imgName}.png')`;
137136

138-
bgBtns[i].onclick = (e) => {
137+
btn.onclick = (e) => {
139138
// ...
140139
};
141140
}

files/en-us/web/api/web_components/using_custom_elements/index.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ If you want to preserve the element's state, you can do so by defining a `connec
110110
You could add an empty `connectedMoveCallback()` to stop the other two callbacks running, or include some custom logic to handle the move:
111111

112112
```js
113-
connectedMoveCallback() {
114-
console.log("Custom move-handling logic here.");
113+
class MyComponent {
114+
// ...
115+
connectedMoveCallback() {
116+
console.log("Custom move-handling logic here.");
117+
}
118+
// ...
115119
}
116120
```
117121

0 commit comments

Comments
 (0)