Skip to content

Commit

Permalink
Add topsites/basic sample (GoogleChrome#967)
Browse files Browse the repository at this point in the history
* Update sample file structure.

* Update description

* Fix wrong favicon path

* Add basic sample

* Update README.md

* Fix comments
  • Loading branch information
daidr authored Jul 6, 2023
1 parent 2852c64 commit 0af7b51
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 12 deletions.
13 changes: 13 additions & 0 deletions api-samples/topSites/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# chrome.topSites - Basic

This sample demonstrates using the `chrome.topSites` API to get the user's most visited sites.

## Overview

The extension uses `chrome.topSites.get` to get the user's most visited sites, and then renders them in the popup.

## Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
3. Click the extension's action icon.
Binary file added api-samples/topSites/basic/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions api-samples/topSites/basic/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Top Sites",
"version": "1.2",
"description": "Shows the top sites in the popup.",
"permissions": ["topSites"],
"action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"manifest_version": 3
}
8 changes: 8 additions & 0 deletions api-samples/topSites/basic/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<body>
<h2 style="width: 300px">Most Visited:</h2>
<div id="mostVisited_div"></div>
<script src="popup.js"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions api-samples/topSites/basic/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Event listener for clicks on links in an action popup.
// Open the link in a new tab of the current window.
function onAnchorClick(event) {
chrome.tabs.create({ url: event.target.href });
return false;
}

// Given an array of URLs, build a DOM list of these URLs in the action popup.
function buildPopupDom(mostVisitedURLs) {
const popupDiv = document.getElementById('mostVisited_div');
const ol = popupDiv.appendChild(document.createElement('ol'));

for (let i = 0; i < mostVisitedURLs.length; i++) {
const li = ol.appendChild(document.createElement('li'));
const a = li.appendChild(document.createElement('a'));
a.href = mostVisitedURLs[i].url;
a.appendChild(document.createTextNode(mostVisitedURLs[i].title));
a.addEventListener('click', onAnchorClick);
}
}

window.onload = async () => {
const mostVisitedURLs = await chrome.topSites.get();
buildPopupDom(mostVisitedURLs);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# chrome.topSites
# chrome.topSites - New Tab Page

This sample demonstrates using the `chrome.topSites` API to suggest which sites a user should visit.

Expand Down
10 changes: 10 additions & 0 deletions api-samples/topSites/magic8ball/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "topSites API sample",
"version": "2",
"description": "An extension that demonstrates the chrome.topSites API by registering a custom new tab page.",
"chrome_url_overrides": {
"newtab": "newTab.html"
},
"permissions": ["topSites", "favicon"],
"manifest_version": 3
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

function faviconURL(u) {
const url = new URL(chrome.runtime.getURL('/_favicon/'));
url.searchParams.set('pageUrl', u); // this encodes the URL as well
url.searchParams.set('size', '16');
return url.toString();
}

function thumbnailsGotten(data) {
let eightBallWindow = document.getElementById('mostVisitedThumb');
let rand = Math.floor(Math.random() * data.length);
eightBallWindow.href = data[rand].url;
eightBallWindow.textContent = data[rand].title;
eightBallWindow.style.backgroundImage =
'url(chrome://favicon/' + data[rand].url + ')';
'url(' + faviconURL(data[rand].url) + ')';
}

window.onload = function () {
Expand Down
10 changes: 0 additions & 10 deletions api-samples/topSites/manifest.json

This file was deleted.

0 comments on commit 0af7b51

Please sign in to comment.