Skip to content

Commit 9b52765

Browse files
authored
fix: Add sandbox attrs to live samples that call open() (#38271)
* feat(api): Fix window.open examples * feat(api): Fix window.open example
1 parent fa5f220 commit 9b52765

File tree

3 files changed

+34
-47
lines changed

3 files changed

+34
-47
lines changed

files/en-us/web/api/blob/index.md

+5-12
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,19 @@ const blob = new Blob([JSON.stringify(obj, null, 2)], {
5757

5858
### Creating a URL representing the contents of a typed array
5959

60-
The following code creates a JavaScript [typed array](/en-US/docs/Web/JavaScript/Guide/Typed_arrays) and creates a new `Blob` containing the typed array's data. It then calls {{DOMxRef("URL/createObjectURL_static", "URL.createObjectURL()")}} to convert the blob into a {{glossary("URL")}}.
60+
The following example creates a JavaScript [typed array](/en-US/docs/Web/JavaScript/Guide/Typed_arrays) and creates a new `Blob` containing the typed array's data. It then calls {{DOMxRef("URL/createObjectURL_static", "URL.createObjectURL()")}} to convert the blob into a {{glossary("URL")}}.
6161

62-
#### HTML
63-
64-
```html
62+
```html live-sample___url-from-array
6563
<p>
6664
This example creates a typed array containing the ASCII codes for the space
6765
character through the letter Z, then converts it to an object URL. A link to
6866
open that object URL is created. Click the link to see the decoded object URL.
6967
</p>
7068
```
7169

72-
#### JavaScript
73-
7470
The main piece of this code for example purposes is the `typedArrayToURL()` function, which creates a `Blob` from the given typed array and returns an object URL for it. Having converted the data into an object URL, it can be used in a number of ways, including as the value of the {{HTMLElement("img")}} element's [`src`](/en-US/docs/Web/HTML/Element/img#src) attribute (assuming the data contains an image, of course).
7571

76-
```js
72+
```js live-sample___url-from-array
7773
function showViewLiveResultButton() {
7874
if (window.self !== window.top) {
7975
// Ensure that if our document is in a frame, we get the user
@@ -103,18 +99,15 @@ if (!showViewLiveResultButton()) {
10399
}
104100

105101
const url = typedArrayToURL(bytes, "text/plain");
106-
107102
const link = document.createElement("a");
103+
108104
link.href = url;
109105
link.innerText = "Open the array URL";
110-
111106
document.body.appendChild(link);
112107
}
113108
```
114109

115-
#### Result
116-
117-
{{EmbedLiveSample("Creating_a_URL_representing_the_contents_of_a_typed_array", 600, 200)}}
110+
{{EmbedLiveSample('url-from-array', , , , , , , 'allow-popups')}}
118111

119112
### Extracting data from a blob
120113

files/en-us/web/api/document/hasfocus/index.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,27 @@ None.
3232

3333
## Examples
3434

35+
### Checking if the document has focus
36+
3537
The following example checks whether the document has focus or not.
3638
A function called `checkPageFocus()` updates a paragraph element depending on the result of `document.hasFocus()`.
3739
Opening a new window will cause the document to lose focus and switching back to the original window will cause the document to regain focus.
3840

39-
### HTML
40-
41-
```html
41+
```html live-sample___has-focus
4242
<p id="log">Focus check results are shown here.</p>
4343
<button id="newWindow">Open new window</button>
4444
```
4545

46-
```css hidden
46+
```css hidden live-sample___has-focus
4747
body {
4848
padding: 1rem;
4949
background: gray;
5050
text-align: center;
51-
font-size: 1.5rem;
51+
font: 1.5rem sans-serif;
5252
}
5353
```
5454

55-
### JavaScript
56-
57-
```js
55+
```js live-sample___has-focus
5856
const body = document.querySelector("body");
5957
const log = document.getElementById("log");
6058

@@ -80,9 +78,7 @@ document.getElementById("newWindow").addEventListener("click", openWindow);
8078
setInterval(checkDocumentFocus, 300);
8179
```
8280

83-
### Result
84-
85-
{{EmbedLiveSample("Examples")}}
81+
{{EmbedLiveSample('has-focus', , , , , , , 'allow-popups')}}
8682

8783
## Specifications
8884

files/en-us/web/api/media_capture_and_streams_api/taking_still_photos/index.md

+22-24
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
228228
229229
### HTML
230230
231-
```html
231+
```html live-sample___photo-capture
232232
<div class="content-area">
233233
<h1>MDN - navigator.mediaDevices.getUserMedia(): Still photo capture demo</h1>
234234
<p>
@@ -247,16 +247,14 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
247247
Visit our article
248248
<a
249249
href="https://developer.mozilla.org/en-US/docs/Web/API/Media_Capture_and_Streams_API/Taking_still_photos">
250-
Taking still photos with WebRTC</a
251-
>
250+
Taking still photos with WebRTC
251+
</a>
252252
to learn more about the technologies used here.
253253
</p>
254254
</div>
255255
```
256256
257-
### CSS
258-
259-
```css
257+
```css hidden live-sample___photo-capture
260258
#video {
261259
border: 1px solid black;
262260
box-shadow: 2px 2px 3px black;
@@ -301,31 +299,31 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
301299
}
302300

303301
.content-area {
304-
font-size: 16px;
305-
font-family: "Lucida Grande", "Arial", sans-serif;
302+
font:
303+
1.2rem "Lucida Grande",
304+
"Arial",
305+
sans-serif;
306306
width: 760px;
307+
padding: 2rem;
307308
}
308309
```
309310
310311
### JavaScript
311312
312-
```js
313+
```js live-sample___photo-capture
313314
(() => {
314315
// The width and height of the captured photo. We will set the
315316
// width to the value defined here, but the height will be
316317
// calculated based on the aspect ratio of the input stream.
317-
318318
const width = 320; // We will scale the photo width to this
319319
let height = 0; // This will be computed based on the input stream
320320

321321
// |streaming| indicates whether or not we're currently streaming
322322
// video from the camera. Obviously, we start at false.
323-
324323
let streaming = false;
325324

326325
// The various HTML elements we need to configure or control. These
327326
// will be set by the startup() function.
328-
329327
let video = null;
330328
let canvas = null;
331329
let photo = null;
@@ -338,9 +336,15 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
338336
// won't be able to request permission for camera access.
339337
document.querySelector(".content-area").remove();
340338
const button = document.createElement("button");
341-
button.textContent = "View live result of the example code above";
339+
button.textContent = "Open example in new window";
342340
document.body.append(button);
343-
button.addEventListener("click", () => window.open(location.href));
341+
button.addEventListener("click", () =>
342+
window.open(
343+
location.href,
344+
"MDN",
345+
"width=850,height=700,left=150,top=150",
346+
),
347+
);
344348
return true;
345349
}
346350
return false;
@@ -373,7 +377,6 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
373377

374378
// Firefox currently has a bug where the height can't be read from
375379
// the video, so we will make assumptions if this happens.
376-
377380
if (isNaN(height)) {
378381
height = width / (4 / 3);
379382
}
@@ -400,9 +403,7 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
400403
clearPhoto();
401404
}
402405

403-
// Fill the photo with an indication that none has been
404-
// captured.
405-
406+
// Fill the photo with an indication that none has been captured.
406407
function clearPhoto() {
407408
const context = canvas.getContext("2d");
408409
context.fillStyle = "#AAA";
@@ -417,7 +418,6 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
417418
// format data URL. By drawing it on an offscreen canvas and then
418419
// drawing that to the screen, we can change its size and/or apply
419420
// other changes before drawing it.
420-
421421
function takePicture() {
422422
const context = canvas.getContext("2d");
423423
if (width && height) {
@@ -438,9 +438,7 @@ If there isn't a valid image available (that is, the `width` and `height` are bo
438438
})();
439439
```
440440
441-
### Result
442-
443-
{{EmbedLiveSample('Demo', '100%', 30)}}
441+
{{EmbedLiveSample('photo-capture', '100%', '30', , , , , 'allow-popups')}}
444442
445443
## Fun with filters
446444
@@ -454,7 +452,7 @@ You can, if needed, restrict the set of permitted video sources to a specific de
454452
455453
## See also
456454
457-
- [Sample code on GitHub](https://github.com/mdn/samples-server/tree/master/s/webrtc-capturestill)
458455
- {{domxref("MediaDevices.getUserMedia")}}
459-
- [Using frames from a video](/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images#using_frames_from_a_video) in the Canvas tutorial
460456
- {{domxref("CanvasRenderingContext2D.drawImage()")}}
457+
- [Using frames from a video](/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images#using_frames_from_a_video) in the Canvas tutorial
458+
- [Sample code on GitHub](https://github.com/mdn/samples-server/tree/master/s/webrtc-capturestill)

0 commit comments

Comments
 (0)