1

I am trying to get the most visited sites in my chrome extension. I have been through Chrome Extension Developer Documentation.

When I trying to get the property topSites from the chrome object, it returning undefined. But as per documentation it available since Chrome 36.

Even I added permission to access topSites in manifest.json.

//Load top sites
function getTopSites() {
  chrome.topSites.get((top) => {
    console.log(top);
  });
}

getTopSites()

When I do print chrome object I find nothing property topSites. But documentation mention topSites available.

//Load top sites
function getTopSites() {
  console.log(chrome);
};

getTopSites()

Output in Microsoft Edge

Output chrome object

"permissions": [
        "activeTab",
        "storage",
        "topSites"
    ]

I am trying to console the output in the browser's console.

Could anyone tell me what's the problem?

Docs

Prathamesh More
  • 1,470
  • 2
  • 18
  • 32
  • 1
    The code is correct and it works in the latest Edge. Make sure you've reloaded the extension on chrome://extensions page after editing manifest.json. – wOxxOm Sep 25 '20 at 05:11

1 Answers1

1

I try to make a test with chrome.topSites in MS Edge Chromium browser Version 85.0.564.51

I found that it is working fine.

I suggest you try to display it in html file instead of console.

I made a test extension with the below files and code.

popup.html

<!DOCTYPE HTML>
<html>
  <body>
    <h2>site list</h2>
    <div id='urls'></div>
    <script src='popup.js'></script>
  </body>
</html>

popup.js

function top_site(urls)
{   
    var url= document.getElementById('urls');
    for (var i = 0; i < urls.length; i++) 
    {
        url.innerHTML += urls[i].url + "<br>";      
    }
}

chrome.topSites.get(top_site);

manifest.json

{
  "name": "sitelist",
  "version": "101",
  "description": "display site list",
  "permissions": ["topSites"],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

Output in Edge browser:

enter image description here

Reference for the extension:

Top sites sample Extensions

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
  • Yeah. The problem is when we try to console this. – Prathamesh More Sep 25 '20 at 12:08
  • 1
    @PrathameshMore it means your question doesn't present an [MCVE](/help/mcve). Every detail is important in development of extensions as they have so many moving parts. – wOxxOm Sep 25 '20 at 12:10
  • I already described all the important things in question. @wOxxOm – Prathamesh More Sep 28 '20 at 11:32
  • No, the purpose of [MCVE](/help/mcve) is to help us reproduce the problem. Your question omitted the most important step. – wOxxOm Sep 28 '20 at 11:33
  • As per the question. If I trying to console object chrome.topSites. It should be returned. But not in that case. It's returning undefined. @wOxxOm – Prathamesh More Sep 28 '20 at 11:35
  • There are [multiple different consoles](https://stackoverflow.com/a/38920982) for each context of an extension. You didn't describe where you are using that code so your question does not contain an [MCVE](/help/mcve). For example, in a content script chrome.topSites is undefined which is the correct behavior. In case it's undefined in an extension page, the cause is different: most likely you didn't reload the extension as I suggested right away. Or maybe you open devtools on edge://extensions page instead of opening devtools for the options page. – wOxxOm Sep 28 '20 at 11:38
  • @wOxxOm I updated the question and I added that I was consoling to the browser's console. Thanks. – Prathamesh More Dec 05 '20 at 04:16