Skip to content

Commit bab8e3c

Browse files
Merge pull request #3472 from MicrosoftDocs/main
main to live - 6/11/2025
2 parents 172e68f + 36837d6 commit bab8e3c

6 files changed

Lines changed: 206 additions & 35 deletions

File tree

46.6 KB
Loading

microsoft-edge/devtools-guide-chromium/console/console-javascript.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.service: microsoft-edge
88
ms.subservice: devtools
9-
ms.date: 07/12/2023
9+
ms.date: 06/06/2025
1010
---
1111
# Run JavaScript in the Console
1212

13-
You can enter any JavaScript expression, statement, or code snippet in the **Console**, and it runs immediately and interactively as you type. This is possible because the **Console** tool in DevTools is a [REPL](https://wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) environment. _REPL_ stands for Read, Evaluate, Print, and Loop.
13+
You can enter any JavaScript expression, statement, or code snippet in the **Console**, and it runs immediately and interactively as you type. This is possible because the **Console** tool in DevTools is a [Read–eval–print loop (REPL)](https://wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) environment.
1414

1515
The **Console**:
1616
1. Reads the JavaScript that you type into it.
@@ -65,15 +65,17 @@ As with many other command-line environments, a history of the commands that you
6565

6666
Similarly, autocompletion keeps a history of the commands you previously typed. You can type the first few letters of earlier commands, and your previous choices appear in a text box.
6767

68-
Also, the **Console** also offers quite a few [utility methods](utilities.md) that make your life easier. For example, `$_` always contains the result of the last expression you ran in the **Console**.
68+
Also, the **Console** also offers quite a few utility methods that make your life easier. For example, `$_` always contains the result of the last expression you ran in the **Console**. See [Console tool utility functions and selectors](./utilities.md).
6969

7070
![The $_ expression in the Console always contains the last result](./console-javascript-images/console-history.png)
7171

7272

7373
<!-- ====================================================================== -->
7474
## Multiline edits
7575

76-
By default, the **Console** only gives you one line to write your JavaScript expression. You code runs when you press **Enter**. The one line limitation may frustrate you. To work around the 1-line limitation, press **Shift+Enter** instead of **Enter**. In the following example, the value displayed is the result of all the lines (statements) run in order:
76+
By default, the **Console** only gives you one line to write your JavaScript expression. You code runs when you press **Enter**. To work around the 1-line limitation, press **Shift+Enter** instead of **Enter**.
77+
78+
In the following example, the value displayed is the result of all the lines (statements) run in order:
7779

7880
![Press Shift+Enter to write several lines of JavaScript. The resulting value is output](./console-javascript-images/multiline.png)
7981

@@ -82,6 +84,50 @@ If you start a multi-line statement in the **Console**, the code block is automa
8284
![The Console recognizes multiline expressions using curly braces and indents](./console-javascript-images/automatic-lineindent.png)
8385

8486

87+
<!-- ====================================================================== -->
88+
## Allow pasting into the Console
89+
90+
When you first try to paste content into the **Console** tool, instead of pasting, a message is displayed: "Warning: Don't paste code into the DevTools Console that you don't understand or haven't reviewed yourself. This could allow attackers to steal your identity or take control of your computer. Please type 'allow pasting' below and press Enter to allow pasting."
91+
92+
![Console displaying the self-XSS warning](./console-javascript-images/console-self-xss-warning.png)
93+
94+
This warning helps prevent self cross-site scripting attacks (self-XSS) on end-users. To paste code, first type **allow pasting** in the **Console**, and then press **Enter**. Then paste the content. Or, start Edge with the flag below.
95+
96+
Pasting into the **Sources** tool's snippet editor is similar; see [Allow pasting into the Snippet editor](../javascript/snippets.md#allow-pasting-into-the-snippet-editor) in _Run snippets of JavaScript on any webpage_.
97+
98+
99+
<!-- ------------------------------ -->
100+
#### Disable self-XSS warnings by starting Edge with a command-line flag
101+
102+
To prevent the above warnings and immediately allow pasting into the **Console** tool and the **Sources** tool's snippet editor, such as for automated testing, start Microsoft Edge from the command line, using the following flag: `--unsafely-disable-devtools-self-xss-warnings`. The flag applies to a single session of Microsoft Edge.
103+
104+
For example, on Windows:
105+
106+
Edge Stable:
107+
108+
```shell
109+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
110+
```
111+
112+
Edge Beta:
113+
114+
```shell
115+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge Beta\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
116+
```
117+
118+
Edge Dev:
119+
120+
```shell
121+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge Dev\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
122+
```
123+
124+
Edge Canary:
125+
126+
```shell
127+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge SxS\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
128+
```
129+
130+
85131
<!-- ====================================================================== -->
86132
## Network requests using top-level await()
87133

@@ -120,3 +166,21 @@ To get the last 50 issues that were filed on the [Microsoft Edge Developer Tools
120166
```
121167

122168
The **Console** is a great way to practice JavaScript and to do some quick calculations. The real power is the fact that you have access to the [window](https://developer.mozilla.org/docs/Web/API/Window) object. See [Interact with the DOM using the Console](console-dom-interaction.md).
169+
170+
171+
<!-- ====================================================================== -->
172+
## See also
173+
<!-- all links in article -->
174+
175+
* [Interact with the DOM using the Console](console-dom-interaction.md)
176+
* [Console tool utility functions and selectors](./utilities.md)
177+
178+
GitHub:
179+
* [ECMAScript proposal: Top-level `await`](https://github.com/tc39/proposal-top-level-await)
180+
* [Microsoft Edge Developer Tools for Visual Studio Code](https://github.com/microsoft/vscode-edge-devtools)
181+
182+
MDN:
183+
* [Window](https://developer.mozilla.org/docs/Web/API/Window) object.
184+
185+
Wikipedia:
186+
* [Read–eval–print loop (REPL)](https://wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)
52.6 KB
Loading

microsoft-edge/devtools-guide-chromium/javascript/snippets.md

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ms.date: 10/17/2023
2222
See the License for the specific language governing permissions and
2323
limitations under the License. -->
2424
# Run snippets of JavaScript on any webpage
25+
<!-- https://developer.chrome.com/docs/devtools/javascript/snippets/ -->
2526

2627
If you're entering the same code into the **Console** tool repeatedly, consider saving the code as a snippet instead, and then running the snippet. Snippets are scripts that you author in the **Sources** tool. Snippets have access to the JavaScript context of the webpage, and you can run snippets on any webpage. Snippets can be used to alter a webpage, such as to change its content or appearance, or to extract data.
2728

@@ -50,13 +51,17 @@ The code changes the background color of the webpage to dimgrey, adds a new line
5051

5152
When you run a snippet on a webpage, the snippet's source code is added to the current webpage. For more information about changing the existing code of a webpage instead of adding new code, see [Override webpage resources with local copies (Overrides tab)](./overrides.md).
5253

54+
55+
<!-- ------------------------------ -->
5356
#### Include all your code in one file
57+
<!-- not upstream -->
5458

5559
The security settings of most webpages block from loading other scripts in snippets. For this reason, you must include all your code in one file.
5660

5761

5862
<!-- ====================================================================== -->
5963
## Open the Snippets tab
64+
<!-- Open the Snippets pane https://developer.chrome.com/docs/devtools/javascript/snippets/#open -->
6065

6166
The **Snippets** tab is grouped with the **Page** tab in the **Navigator** pane, on the left of the **Sources** tool.
6267

@@ -71,7 +76,9 @@ To open the **Snippets** tab:
7176
1. In the **Navigator** pane (on the left), select the **Snippets** tab. To access the **Snippets** option, you might need to click the **More tabs** (![More tabs](./snippets-images/more-tabs-icon.png)) button.
7277

7378

79+
<!-- ------------------------------ -->
7480
#### Open the Snippets tab from the Command Menu
81+
<!-- no separate heading upstream -->
7582

7683
You can also open the **Snippets** tab by using the **Command Menu**:
7784

@@ -85,7 +92,17 @@ You can also open the **Snippets** tab by using the **Command Menu**:
8592

8693

8794
<!-- ====================================================================== -->
88-
## Create a new snippet
95+
## Create a snippet
96+
<!-- Create snippets https://developer.chrome.com/docs/devtools/javascript/snippets/#create -->
97+
98+
You can create a snippet from within the **Snippets** tool, or by running the **Create new snippet** command from the **Command Menu** anywhere in DevTools.
99+
100+
The **Snippets** pane sorts your snippets in alphabetical order.
101+
102+
103+
<!-- ------------------------------ -->
104+
#### Create a snippet in the Sources tool
105+
<!-- Create a snippet in the Sources panel https://developer.chrome.com/docs/devtools/javascript/snippets/#create-sources -->
89106

90107
To create a new snippet from the **Snippets** tab:
91108

@@ -97,7 +114,10 @@ To create a new snippet from the **Snippets** tab:
97114

98115
![A new, empty, snippet in the Sources tool](./snippets-images/new-snippet.png)
99116

100-
#### Create a new snippet from the Command Menu
117+
118+
<!-- ------------------------------ -->
119+
#### Create a snippet from the Command Menu
120+
<!-- Create a snippet from the Command Menu https://developer.chrome.com/docs/devtools/javascript/snippets/#create-command-menu -->
101121

102122
1. Focus your cursor somewhere in DevTools.
103123

@@ -112,6 +132,7 @@ To rename your new snippet, see [Rename a snippet](#rename-a-snippet), below.
112132

113133
<!-- ====================================================================== -->
114134
## Edit a snippet
135+
<!-- Edit snippets https://developer.chrome.com/docs/devtools/javascript/snippets/#edit -->
115136

116137
To edit the source code of a snippet:
117138

@@ -128,10 +149,60 @@ To edit the source code of a snippet:
128149
![An asterisk next to the snippet name indicates unsaved code](./snippets-images/unsaved-changes.png)
129150

130151

152+
<!-- ====================================================================== -->
153+
## Allow pasting into the Snippet editor
154+
<!-- not upstream -->
155+
156+
When you first try to paste content into the **Sources** tool > **Snippets** tab > snippet editor, instead of pasting, a **Do you trust this code?** dialog is displayed, with the message: "Don't paste code you do not understand or have not reviewed yourself into DevTools. This could allow attackers to steal your identity or take control of your computer. Please type 'allow pasting' below to allow pasting."
157+
158+
![Sources tool's Snippets tab editor displaying the self-XSS warning](./snippets-images/sources-self-xss-warning.png)
159+
160+
This warning helps prevent self cross-site scripting attacks (self-XSS) on end-users. To paste code, first type **allow pasting** in the dialog's text box, and then click the **Allow** button. Then paste the content. Or, start Edge with the flag below.
161+
162+
Pasting into the **Console** tool is similar; see [Allow pasting into the Console](../console/console-javascript.md#allow-pasting-into-the-console) in _Run JavaScript in the Console_.
163+
164+
165+
<!-- ------------------------------ -->
166+
#### Disable self-XSS warnings by starting Edge with a command-line flag
167+
<!-- not upstream -->
168+
169+
To prevent the above warnings and immediately allow pasting into the **Console** tool and the **Sources** tool's snippet editor, such as for automated testing, start Microsoft Edge from the command line, using the following flag: `--unsafely-disable-devtools-self-xss-warnings`. The flag applies to a single session of Microsoft Edge.
170+
171+
For example, on Windows:
172+
173+
Edge Stable:
174+
175+
```shell
176+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
177+
```
178+
179+
Edge Beta:
180+
181+
```shell
182+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge Beta\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
183+
```
184+
185+
Edge Dev:
186+
187+
```shell
188+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge Dev\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
189+
```
190+
191+
Edge Canary:
192+
193+
```shell
194+
"C:\Users\localAccount\AppData\Local\Microsoft\Edge SxS\Application\msedge.exe" --unsafely-disable-devtools-self-xss-warnings
195+
```
196+
197+
131198
<!-- ====================================================================== -->
132199
## Run a snippet
200+
<!-- Run snippets https://developer.chrome.com/docs/devtools/javascript/snippets/#run -->
201+
133202

203+
<!-- ------------------------------ -->
134204
#### Run a snippet from the Sources tool
205+
<!-- Run a snippet in the Sources panel https://developer.chrome.com/docs/devtools/javascript/snippets/#run-sources -->
135206

136207
1. [Open the Snippets tab](#open-the-snippets-tab).
137208

@@ -141,7 +212,10 @@ To edit the source code of a snippet:
141212

142213
![The run snippet button at the bottom of the code editor](./snippets-images/run-snippet-from-sources-tool.png)
143214

215+
216+
<!-- ------------------------------ -->
144217
#### Run a snippet from the Command Menu
218+
<!-- https://developer.chrome.com/docs/devtools/javascript/snippets/#run-command-menu -->
145219

146220
1. Focus your cursor somewhere in DevTools.
147221

@@ -156,6 +230,7 @@ To edit the source code of a snippet:
156230

157231
<!-- ====================================================================== -->
158232
## Rename a snippet
233+
<!-- Rename snippets https://developer.chrome.com/docs/devtools/javascript/snippets/#rename -->
159234

160235
1. [Open the Snippets tab](#open-the-snippets-tab).
161236

@@ -164,22 +239,26 @@ To edit the source code of a snippet:
164239

165240
<!-- ====================================================================== -->
166241
## Delete a snippet
242+
<!-- Delete snippets https://developer.chrome.com/docs/devtools/javascript/snippets/#delete -->
167243

168244
1. [Open the Snippets tab](#open-the-snippets-tab).
169245

170246
1. Right-click the snippet name, and then select **Remove**.
171247

172248

173249
<!-- ====================================================================== -->
174-
## Save a snippet
250+
## Save a snippet to disk as a file
251+
<!-- not upstream -->
175252

176-
By default, snippets are only available within DevTools, but you can also save them to disk.
253+
By default, snippets are only available within DevTools, but you can also save snippets to disk.
177254

178255
1. [Open the Snippets tab](#open-the-snippets-tab).
179256

180257
1. Right-click the snippet name, and then select **Save as**.
181258

182-
1. When prompted, enter a file name and location.
259+
The **Save As** dialog opens.
260+
261+
1. Select a folder, enter a file name (such as `Script snippet #1.js`), and then click the **Save** button.
183262

184263

185264
<!-- ====================================================================== -->

0 commit comments

Comments
 (0)