Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MV3 sample for topsites API #840

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
10 changes: 10 additions & 0 deletions api-samples/topsites/basic/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Top Sites",
"version": "1.3",
"description": "Shows the top sites in a browser action",
"permissions": ["topSites"],
"action": {
"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>Most Visited:</h2>
<div id='mostVisited_div'></div>
<script src='popup.js'></script>
</body>
</html>
50 changes: 50 additions & 0 deletions api-samples/topsites/basic/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Event listener for clicks on links in a browser action popup.
// Open the link in a new tab of the current window.
function onAnchorClick(event) {
chrome.tabs.create({ url: event.srcElement.href });
return false;
}

// Use the get method to get a list of most visited sites.
chrome.topSites.get()
.then((mostVisitedURLs) => {
if(mostVisitedURLs){
var popupDiv = document.getElementById('mostVisited_div');
var ol = popupDiv.appendChild(document.createElement('ol'));

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

})
.catch((err) => {
console.log(err)
})

//--------------------------------------------------------
// Alternatively you can use a callback as well
//--------------------------------------------------------

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

for (var i = 0; i < mostVisitedURLs.length; i++) {
var li = ol.appendChild(document.createElement('li'));
var a = li.appendChild(document.createElement('a'));
a.href = mostVisitedURLs[i].url;
a.appendChild(document.createTextNode(mostVisitedURLs[i].title));
a.addEventListener('click', onAnchorClick);
}
}
*/
// pass in a callback to the get method.
// chrome.topSites.get(buildPopupDom);