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
OptionalBoolean
. Include pages that the user has removed from the "New Tab" page. Defaults tofalse
.includeFavicon
OptionalBoolean
. Include favicons in the results, for pages where they are available. Defaults tofalse
.includePinned
OptionalBoolean
. Includes sites that the user has pinned to the Firefox new tab.
Defaults tofalse
.includeSearchShortcuts
OptionalBoolean
. Includes search shortcuts that appear on the Firefox new tab.
Defaults tofalse
.limit
OptionalInteger
. The number of pages to return. This must be a number between 1 and 100, inclusive. Defaults to 12.newtab
OptionalBoolean
. If included, the method returns the list of pages returned when the user opens a new tab. If included, and set totrue
, the method ignores all other parameters exceptlimit
andincludeFavicon
. Defaults tofalse
.onePerDomain
OptionalBoolean
. Only include one page per domain. Defaults totrue
.
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
AcknowledgementsThis 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论