|
| 1 | +<!-- This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + - License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> |
| 4 | + |
| 5 | +The `places/favicon` module provides simple helper functions for working with favicons. |
| 6 | + |
| 7 | +<api name="getFavicon"> |
| 8 | +@function |
| 9 | +Takes an `object` that represents a page's URL and returns a promise |
| 10 | +that resolves with the favicon URL for that page. The `object` can |
| 11 | +be a URL `String` or a [`Tab`](modules/sdk/tabs.html#Tab). The platform service |
| 12 | +([mozIAsyncFavicons](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/mozIAsyncFavicons)) retrieves favicon data stored from previously visited sites, and as |
| 13 | +such, will only return favicon URLs for visited sites. |
| 14 | + |
| 15 | + let { getFavicon } = require("sdk/places/favicon"); |
| 16 | + |
| 17 | + // String example |
| 18 | + getFavicon("http://mozilla.org").then(function (url) { |
| 19 | + console.log(url); // http://mozorg.cdn.mozilla.net/media/img/favicon.ico |
| 20 | + }); |
| 21 | + |
| 22 | + // Tab example |
| 23 | + require("sdk/tabs").open({ |
| 24 | + url: "http://mozilla.org", |
| 25 | + onReady: function (tab) { |
| 26 | + getFavicon(tab).then(function (url) { |
| 27 | + console.log(url); // http://mozorg.cdn.mozilla.net/media/img/favicon.ico |
| 28 | + }); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + // An optional callback can be provided to handle |
| 33 | + // the promise's resolve and reject states |
| 34 | + getFavicon("http://mozilla.org", function (url) { |
| 35 | + console.log(url); // http://mozorg.cdn.mozilla.net/media/img/favicon.ico |
| 36 | + }); |
| 37 | + |
| 38 | +@param object {string, tab} |
| 39 | + A value that represents the URL of the page to get the favicon URL from. |
| 40 | + Can be a URL `String` or a [`Tab`](modules/sdk/tabs.html#Tab). |
| 41 | + |
| 42 | +@param callback {function} |
| 43 | + An optional callback function that will be used in both resolve and |
| 44 | + reject cases. |
| 45 | + |
| 46 | +@returns {promise} |
| 47 | + A promise that resolves with the favicon URL. |
| 48 | +</api> |
0 commit comments