topSites.get() 编辑

Gets an array containing information about pages that the user has visited often and recently. 

Browsers keep a list of pages that the user visits often and recently. They use this list to help the user get back to these places easily. For example, Firefox by default provides a list of the most-visited pages in the "New Tab" page.

To determine which pages appear in the list, and the order in which they appear, the browser combines "frequency" - how often the user has visited the page - and "recency" - how recently the user visited the page.

The browser may then apply further filtering to this list before presenting it to the user. For example, in Firefox the "New Tab" page only lists one page per domain, and the user is able to block pages from appearing in the list.

The topSites.get() API enables an extension to get access to this list. Called without any options, it will provide the filtered list of pages -  that is, the one that appears in the "New Tab" page. However, by providing various options it's possible for an extension to get the unfiltered list of pages.

This is an asynchronous function that returns a Promise.

To use the topSites API you must have the "topSites" API permission.

Syntax

var gettingTopSites = browser.topSites.get(
    options  // object
)

Parameters

options
object. Options to modify the list of pages returned. This may include any of the following properties:
includeBlocked Optional
Boolean. Include pages that the user has removed from the "New Tab" page. Defaults to false.
includeFavicon Optional
Boolean. Include favicons in the results, for pages where they are available. Defaults to false.
includePinned Optional
Boolean. Includes sites that the user has pinned to the Firefox new tab.
Defaults to false.
includeSearchShortcuts Optional
Boolean. Includes search shortcuts that appear on the Firefox new tab.
Defaults to false.
limit Optional
Integer. The number of pages to return. This must be a number between 1 and 100, inclusive. Defaults to 12.
newtab Optional
Boolean. If included, the method returns the list of pages returned when the user opens a new tab. If included, and set to true, the method ignores all other parameters except limit and includeFavicon. Defaults to false.
onePerDomain Optional
Boolean. Only include one page per domain. Defaults to true.

Return value

A Promise. This will be fulfilled with an array of MostVisitedURL objects, one for each page listed in the browser's "New Tab" page. If an error occurs, the promise will be rejected with an error message.

Browser compatibility

BCD tables only load in the browser

Examples

This code logs the title and URL for all pages in the "New Tab" page:

function logTopSites(topSitesArray) {
  for (topSite of topSitesArray) {
    console.log(`Title: ${topSite.title}, URL: ${topSite.url}`);
  }
}

function onError(error) {
  console.log(error);
}

var gettingTopSites = browser.topSites.get();
gettingTopSites.then(logTopSites, onError);

This code logs the title and URL for all top pages, including ones the user has blocked, and potentially including multiple pages in the same domain:

function logTopSites(topSitesArray) {
  for (topSite of topSitesArray) {
    console.log(`Title: ${topSite.title}, URL: ${topSite.url}`);
  }
}

function onError(error) {
  console.log(error);
}

var gettingTopSites = browser.topSites.get({
  includeBlocked: true,
  onePerDomain: false
});

gettingTopSites.then(logTopSites, onError);

Example extensions

Acknowledgements

This API is based on Chromium's chrome.topSites API.

// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:71 次

字数:7203

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文