【问题标题】:Get chrome.topSites and recently closed tab获取 chrome.topSites 和最近关闭的标签
【发布时间】:2012-07-05 17:53:51
【问题描述】:

我正在开发 chrome 扩展,它将替换新的标签 UI 我能够检索已安装的应用程序信息。 但是我无法检索访问次数最多和最近关闭的标签信息..

我的清单.json

{
 "name": "Cloud Tab",
"version": "1.0",
"description": "New Tab with cloud UI.",
"background_page": "background.html",
"permissions": [ "tabs","management","topSites",
    "chrome://favicon/"],

"chrome_url_overrides": {
"newtab": "CloudTab.html"
}
}

我的 CloudTab.html 页面的脚本标签包含

 chrome.topSites.get(function(info){
   for(var i=0;i<info.length;i++) {alert(info[i].url);}
  });

但我得到错误>未捕获的类型错误:无法调用未定义的方法'get' 我已经为此参考了谷歌的 api,但没有运气 我正在运行 chrome 的 13.0.782 版本 有什么建议吗?

【问题讨论】:

  • AFAIK alert 不起作用,而是使用 console.log

标签: api google-chrome-extension browser-history


【解决方案1】:

您可以在后台页面中使用onRemoved 事件的侦听器跟踪关闭的选项卡,并使用一些messages 向它请求信息。

【讨论】:

  • tabs.onRemoved 不可靠,或者至少它的用法不够,因为即使对于一些不可见的选项卡也会调用它,这与普通选项卡无法区分。此外,为了正确跟踪最近关闭的标签,您需要处理tabs.onUpdated,这也不可靠:(code.google.com/p/chromium/issues/detail?id=109557
【解决方案2】:

大多数 Chrome API 方法都是异步的。所以你需要做的是传入一个回调函数。

getTopSites: function(callbackfunc) {
    chrome.topSites.get (function(url_list) {
        for(var i=0;i<url_list.length;i++) {callbackfunc(url_list[i]);}
    });
}

然后您可以使用如下回调函数调用它:

getTopSites(function(url){alert(url);});

请原谅我在上面犯的任何语法错误..

【讨论】:

    【解决方案3】:

    要使用此 API,您的 Chrome 版本必须大于或等于 19。 请参阅 Google Chrome 扩展的“扩展中的新增功能”页面:http://code.google.com/chrome/extensions/whats_new.html#19

    【讨论】:

      【解决方案4】:

      我试过这种方式,它在 chrome 37

      中工作

      popup.js

      function onAnchorClick(event) {
        chrome.tabs.create({ url: event.srcElement.href });
        return false;
      }
      
      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);
        }
      }
      chrome.topSites.get(buildPopupDom);
      

      popup.html

      <!DOCTYPE html>
      <html>
        <body>
      
          <h2>Most visited links</h2>
          <div id="mostVisited_div"></div>
          <script src="popup.js"></script>
        </body>
      </html>
      

      manifest.json

      "chrome_url_overrides": { "newtab": "CloudTab.html" }

      "permissions": [
              "tabs",
              "topSites",
              "http://*/*",
              "https://*/*"
        ],            
      "browser_action": {
              "default_icon": "icon_32.png",
              "default_popup": "popup.html"
        },
       "content_scripts": [{
          "matches": ["<all_urls>"],
          "js": ["popup.js"]
       }]
      

      【讨论】:

        猜你喜欢
        • 2018-08-28
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 2017-12-17
        • 2020-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多