草案
此页面不完整。

getAll() 方法在 ContentIndex interface returns a Promise that resolves with an iterable list of content index entries.

句法

var indexedContent = ContentIndex.getAll();
					

参数

This method receives no parameters.

返回值

返回 Promise that resolves with an 数组 of ContentDescription 项。

ContentDescription
Each item returned is an 对象 containing the following data:
  • id : A unique 字符串 标识符。
  • title : A 字符串 title for the item. Used in user-visible lists of content.
  • title : A 字符串 title of the item. Used in user-visible lists of content.
  • description : A 字符串 description of the item. Used in user-visible lists of content.
  • url : A 字符串 containing the url of the corresponding HTML document. Needs to be under the scope of the current service worker .
  • category : 可选 A 字符串 defining the category of content. Can be:
    • '' 字符串 , this is the default.
    • homepage
    • 文章
    • 视频
    • audio
  • icons : 可选 数组 of image resources, defined as an 对象 with the following data:

异常

No exceptions are thrown. If there are no items in the Content Index, an empty 数组 被返回。

范例

The below example shows an asynchronous function that retrieves items within the content index and iterates over each entry, building a list for the interface.

async function createReadingList() {
  // access our service worker registration
  const registration = await navigator.serviceWorker.ready;
  // get our index entries
  const entries = await registration.index.getAll();
  // create a containing element
  const readingListElem = document.createElement('div');
  // test for entries
  if (!Array.length) {
    // if there are no entries, display a message
    const message = document.createElement('p');
    message.innerText = 'You currently have no articles saved for offline reading.'
    readingListElem.append(message);
  } else {
    // if entries are present, display in a list of links to the content
    const listElem = document.createElement('ul');
    for (const entry of entries) {
      const listItem = document.createElement('li');
      const anchorElem = document.createElement('a');
      anchorElem.innerText = entry.title;
      anchorElem.setAttribute('href', entry.url);
      listElem.append(listItem);
    }
    readingListElem.append(listElem);
  }
}
					

规范

规范 状态 注释
未知
The definition of 'getAll' in that specification.
未知 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

No compatibility data found. Please contribute data for "api.ContentIndex.getAll" (depth: 1) to the MDN 兼容性数据存储库 .

另请参阅:

元数据

  • 最后修改: