2

我正在尝试使用文件系统访问 API 附带的函数。

例如,当网站使用文件系统访问 API 时,如下所示。


// store a reference to our file handle
let fileHandle;

async function getFile() {
  // open file picker
  [fileHandle] = await window.showOpenFilePicker();

  if (fileHandle.type === 'file') {
    // run file code
  } else if (fileHandle.type === 'directory')
    // run directory code
  }

}

可以用 JavaScript 挂钩 Web API 吗?我的意思是当网站使用这个 API 时如何停止它的进程?

4

1 回答 1

1

如果“钩子”是指在调用函数时拦截,那么您可以像这样覆盖内置函数:

const originalShowOpenFilePicker = window.showOpenFilePicker;
window.showOpenFilePicker = (...args) => {
  console.log('Modified `showOpenFilePicker` called with these arguments:', args);
  // Block the page by calling `while (true) {}`,
  // but nor sure if this is what you mean. The
  // `return` statement below would be never reached
  // in this case. 
  return originalShowOpenFilePicker(...args);
};
于 2021-02-23T14:51:28.837 回答