Skip to content

Commit

Permalink
Navigator.plugins, mimeTypes, pdfViewerEnabled updates (#13605)
Browse files Browse the repository at this point in the history
* Navigator.plugins, mimeTypes, pdfViewerEnabled updates

* Apply suggestions from code review

Co-authored-by: Joe Medley <jmedley@google.com>

* Update files/en-us/web/api/navigator/pdfViewerEnabled/index.md

Co-authored-by: Joe Medley <jmedley@google.com>

* lower case the file name

* revert slug

Co-authored-by: Joe Medley <jmedley@google.com>
  • Loading branch information
hamishwillee and jpmedley authored Mar 15, 2022
1 parent 97891a8 commit c1d3f3d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 82 deletions.
2 changes: 2 additions & 0 deletions files/en-us/web/api/navigator/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ _Doesn't inherit any properties._
- : Returns {{domxref("MediaSession")}} object which can be used to provide metadata that can be used by the browser to present information about the currently-playing media to the user, such as in a global media controls UI.
- {{domxref("Navigator.onLine")}} {{readonlyInline}}
- : Returns a boolean value indicating whether the browser is working online.
- {{domxref("Navigator.pdfViewerEnabled")}} {{readonlyinline}}
- : Returns `true` if the browser can display PDF files inline when navigating to them, and `false` otherwise.
- {{domxref("Navigator.permissions")}} {{readonlyinline}} {{experimental_inline}}
- : Returns a {{domxref("Permissions")}} object that can be used to query and update permission status of APIs covered by the [Permissions API](/en-US/docs/Web/API/Permissions_API).
- {{domxref("Navigator.presentation")}} {{readonlyInline}} {{experimental_inline}}
Expand Down
48 changes: 27 additions & 21 deletions files/en-us/web/api/navigator/mimetypes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,49 @@ tags:
- Property
- Navigator
- Reference
- Deprecated
browser-compat: api.Navigator.mimeTypes
---
{{ ApiRef("HTML DOM") }}{{deprecated_header}}

Returns a {{domxref("MimeTypeArray")}} object, which contains a list of
{{domxref("MimeType")}} objects representing the MIME types recognized by the browser.
Returns a {{domxref("MimeTypeArray")}} object, which contains a list of {{domxref("MimeType")}} objects representing the MIME types recognized and supported by the browser.
The array can be queried for information about the enabled plugin that is used to handle a file of the specified type.
Named properties of the returned object are not enumerable (except in very old browser versions).

> **Note:** Named properties of {{domxref("MimeTypeArray")}} objects are
> no longer enumerable in the latest browser versions.
Recent versions of the specification hard-code the returned set of MIME types.
If PDF files can be displayed inline then `application/pdf` and `text/pdf` are listed.
Otherwise an empty list is returned.

## Syntax
> **Note:** Use {{domxref("Navigator.pdfViewerEnabled")}} to determine if inline viewing of PDF files is supported. Do not infer it from this property.
```js
var mimeTypes[] = navigator.mimeTypes;
```
Legacy browser versions do not hard-code the list returned by the property, and might return other MIME types.


## Value

`mimeTypes` is a `MimeTypeArray` object which has a
`length` property as well as `item(index)` and
`namedItem(name)` methods.
A `MimeTypeArray` object which has a `length` property as well as `item(index)` and `namedItem(name)` methods.

If PDF inline viewing is supported this has entries for MIME types `application/pdf` and `text/pdf`.
Otherwise an empty `MimeTypeArray` is returned.
The description and file suffixes supported by enabled plugins are hard coded to `'pdf'` and `'Portable Document Format'`, respectively.

## Example

The code below tests whether PDF files can be viewed inline, and then prints the description of the plugin and the file suffixes it supports.

```js
function isJavaPresent() {
return 'application/x-java-applet' in navigator.mimeTypes;
}
if ('application/pdf' in navigator.mimeTypes) {
// browser supports inline viewing of PDF files.

function getJavaPluginDescription() {
var mimetype = navigator.mimeTypes['application/x-java-applet'];
if (mimetype === undefined) {
// no Java plugin present
return undefined;
}
return mimetype.enabledPlugin.description;
const { description, suffixes } = navigator.mimeTypes['application/pdf'];
console.log(`Description: ${ description }, Suffix: ${ suffixes }`);
// expected output: Description: Portable Document Format, Suffix: pdf
}
```

Note that while the above code tests for `application/pdf` you could equally well check `text/pdf`. (Either both or neither MIME type will be true.)
In addition, on current browsers you don't actually need to get the plugin description and suffixes, because this information is also hard-coded.

## Specifications

{{Specifications}}
Expand Down
40 changes: 40 additions & 0 deletions files/en-us/web/api/navigator/pdfviewerenabled/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Navigator.pdfViewerEnabled
slug: Web/API/Navigator/pdfViewerEnabled
tags:
- pdfViewerEnabled
- API
- DOM
- Property
- Reference
browser-compat: api.Navigator.pdfViewerEnabled
---
{{APIRef("HTML DOM")}}

The **`pdfViewerEnabled`** property of the {{domxref("Navigator")}} interface indicates whether the browser supports inline display of PDF files when navigating to them.

If inline viewing is not supported the PDF is downloaded and may then be handled by some external application.

> **Note:** This method replaces a number of legacy methods of inferring support for inline viewing of PDF files.
## Value

`true` if the browser can display PDF files inline when navigating to the file (using either an internal viewer or a PDF viewer extension); otherwise `false`.

## Examples

To check PDF inline viewing support:

```js
if (!navigator.pdfViewerEnabled) {
// The browser does not support inline viewing of PDF files.
}
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}
88 changes: 27 additions & 61 deletions files/en-us/web/api/navigator/plugins/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,55 @@ tags:
- Plugins
- Property
- Reference
- Deprecated
browser-compat: api.Navigator.plugins
---
{{APIRef("HTML DOM")}}{{deprecated_header}}

Returns a {{DOMxRef("PluginArray")}} object, listing the {{DOMxRef("Plugin")}} objects
describing the plugins installed in
the application.
Returns a {{DOMxRef("PluginArray")}} object, listing the {{DOMxRef("Plugin")}} objects describing the plugins installed in the application.
Named properties of returned object are not enumerable (except in very old browser versions).

> **Note:** In Firefox 29 and later, enumeration of the `navigator.plugins` array may
> be restricted as a privacy measure. Applications that must check for the presence of a
> browser plugin should query `navigator.plugins` or
> {{DOMxRef("Navigator.mimeTypes", "navigator.mimeTypes")}} by exact name instead of enumerating the
> `navigator.plugins` array and comparing every plugin's name. This privacy
> change does not disable any plugins; it just hides some plugin names from enumeration.
Recent versions of the specification hard-code the returned list.
If inline viewing of PDF files is supported the property lists five standard plugins
If inline PDF viewing is not supported then an empty list is returned.

## Syntax
> **Note:** Use {{domxref("Navigator.pdfViewerEnabled")}} to determine if inline viewing of PDF files is supported. Do not infer it from this property.
>
> The "five standard plugins" are those that developers have most commonly used to feature detect inline PDF viewing.
> Returning these ensures that legacy code can more reliably determine whether inline viewing is supported.
> However this approach is not recommended for new code because this property may eventually be removed.
```js
var plugins = navigator.plugins;
```
Legacy browser versions also list plugins for Adobe Flash and PDF viewer extensions.

`plugins` is {{DOMxRef("PluginArray")}} object used to access
{{DOMxRef("Plugin")}} objects either by name or as a list of items.
## Value

The returned value is not a JavaScript array, but has the `length` property
and supports accessing individual items using bracket notation
(`plugins[2]`), as well as via `item(index)` and
`namedItem("name")` methods.

## Examples
`plugins` is a {{DOMxRef("PluginArray")}} object used to access {{DOMxRef("Plugin")}} objects either by name or as a list of items.

The following example function returns the version of the Shockwave Flash plugin.

```js
function getFlashVersion() {
var flash = navigator.plugins.namedItem('Shockwave Flash');
if (typeof flash != 'object') {
// flash is not present
return undefined;
}
if(flash.version){
return flash.version;
} else {
//No version property (e.g. in Chrome)
return flash.description.replace(/Shockwave Flash /,"");
}
}
```
The returned value is not a JavaScript array, but has the `length` property and supports accessing individual items using bracket notation (`plugins[2]`), as well as via `item(index)` and `namedItem("name")` methods.

The following example displays information about the installed plugin(s).
If PDF inline viewing is supported this will contain entries for the following plugins:
- "PDF Viewer"
- "Chrome PDF Viewer"
- "Chromium PDF Viewer"
- "Microsoft Edge PDF Viewer"
- "WebKit built-in PDF"
If inline viewing of PDFs is not supported then an empty object is returned.

```js
var pluginsLength = navigator.plugins.length;

document.body.innerHTML = pluginsLength + " Plugin(s)<br>"
+ '<table id="pluginTable"><thead>'
+'<tr><th>Name</th><th>Filename</th><th>description</th><th>version</th></tr>'
+'</thead><tbody></tbody></table>';
## Examples

var table = document.getElementById('pluginTable');
This code shows how to check if PDF files can be displayed inline:

for(var i = 0; i < pluginsLength; i++) {
let newRow = table.insertRow();
newRow.insertCell().textContent = navigator.plugins[i].name;
newRow.insertCell().textContent = navigator.plugins[i].filename;
newRow.insertCell().textContent = navigator.plugins[i].description;
newRow.insertCell().textContent = navigator.plugins[i].version?navigator.plugins[i].version:"";
```js
if ('PDF Viewer' in navigator.plugins) {
// browser supports inline viewing of PDF files.
}
```

## Notes

The {{DOMxRef("Plugin")}} object exposes a small interface for getting information
about the various plugins installed in your browser. A list of plugins is also available
by entering `about:plugins` in the browser's Location bar.

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

In addition to listing each plugin as a pseudo-array by zero-indexed numeric
properties, Firefox provides properties that are the plugin name directly on the [PluginArray](/en-US/docs/Web/API/PluginArray) object.

0 comments on commit c1d3f3d

Please sign in to comment.