Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. FileSystemObserver
  4. observe()

FileSystemObserver: observe() method

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.

Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.

Theobserve() method of theFileSystemObserver interface asks the observer to start observing changes to a given file or directory.

Syntax

js
observe(handle)observe(handle, options)

Parameters

handle

The handle of the file system entry representing the file or directory to observe.

optionsOptional

An object specifying options for theobserve() call. This can contain the following properties:

recursive

A boolean specifying whether you want to observe changes to a directory recursively. If set totrue, changes are observed in the directory itself and all contained subdirectories and files. If set tofalse, changes are only observed in the directory itself and directly contained files (that is, files in subdirectories are excluded). Defaults tofalse.

This property has no effect ifhandle represents a file.

Return value

APromise that resolves toundefined.

Exceptions

NotFoundErrorDOMException

Thrown if the file or directory represented byhandle could not be found.

Examples

Observe a file or directory

Assuming aFileSystemObserver instance is available, you can start observing changes to a file system entry by callingobserve().

You can observe a file or directory in the user-observable file system or theOrigin Private File System (OPFS) by passing aFileSystemFileHandle orFileSystemDirectoryHandle toobserve(). Instances of these objects can be returned, for example, when asking the user to select a file or directory usingWindow.showSaveFilePicker() orWindow.showDirectoryPicker():

js
// Observe a fileasync function observeFile() {  const fileHandle = await window.showSaveFilePicker();  await observer.observe(fileHandle);}// Observe a directoryasync function observeDirectory() {  const directoryHandle = await window.showDirectoryPicker();  await observer.observe(directoryHandle);}

You can also observe changes to the OPFS by passing aFileSystemSyncAccessHandle toobserve():

js
// Observe an OPFS file system entryasync function observeOPFSFile() {  const root = await navigator.storage.getDirectory();  const draftHandle = await root.getFileHandle("draft.txt", { create: true });  const syncHandle = await draftHandle.createSyncAccessHandle();  await observer.observe(syncHandle);}

Observe a directory recursively

To observe a directory recursively, callobserve() with therecursive option set totrue:

js
// Observe a directory recursivelyasync function observeDirectory() {  const directoryHandle = await window.showDirectoryPicker();  await observer.observe(directoryHandle, { recursive: true });}

Specifications

Not currently part of a specification. Seehttps://github.com/whatwg/fs/pull/165 for the relevant specification PR.

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp