0

在最近更新 chrome 后,我使用的一个旧应用程序 - 书签管理器停止了工作。更具体地说,获取缩略图的部分不起作用。错误信息是

“tabs.captureVisibleTab 期间出错:无法访问 url “chrome://newtab/#20”的内容。扩展清单必须请求访问此主机的权限。“

据我所知, tabs.captureVisibleTab 不应该工作

铬合金://

标签等

这是 manifest.json 权限:

"permissions": [ "storage","bookmarks", "tabs", "history", "management", "unlimitedStorage", "chrome://favicon/", "http://*/*", "https://*/*","<all_urls>", "contextMenus", "notifications" ],

以下是在页面加载时触发缩略图的功能

    function getThumbnail(url, showInfoWarning) {
    chrome.tabs.getSelected( null,function(tab) { 
      speeddial.storage.removeThumbnail(url);
        localStorage['requestThumbnail'] = tab.id+'|||'+url; 
        openInCurrent(url); 
    }); 
}

function makeThumbnail(url,captureDelay) {
setTimeout(function() 
{
    chrome.tabs.captureVisibleTab(null,{format:'png'},function(dataurl)
    {
        var canvas = document.createElementNS( "http://www.w3.org/1999/xhtml", "html:canvas" );
        var ctx = canvas.getContext('2d');      
        var img = document.createElement('img');
        img.onload = function()
        {
            try
            {
                resized_width = 480; 
                quality = 0.72;

                if          (localStorage['options.thumbnailQuality']=='low')       { resized_width = 360; quality = 0.75;  }
                if          (localStorage['options.thumbnailQuality']=='high')      { resized_width = 720; quality = 0.65;  }

                resized_height =  Math.ceil((resized_width/img.width)*img.height);
                canvas.width=resized_width
                canvas.height=resized_height
                ctx.drawImage(img,0,0,resized_width,resized_height);

                localStorage.setItem(url, dataurl);
      // SPEED DIAL DB 
      // var speeddialdb = {};
      // speeddialdb.storage = {};
      // speeddialdb.storage.db = null;
      // var dbSize = 1 * 1024 * 1024; // 2MB
      // speeddialdb.storage.db = null;
      // speeddialdb.storage.db = openDatabase('bookmarks', '1.0', 'Speeddial2', dbSize);

      // speeddialdb.storage.db.transaction(function(tx) {
      //   tx.executeSql('DELETE FROM thumbnails WHERE url = ?', [url],function(){
      //     tx.executeSql('INSERT INTO thumbnails (url, thumbnail) values (?, ?)', [url, canvas.toDataURL("image/jpeg",quality)], null ,function(tx, e){alert('Something unexpected happened: ' + e.message ) });  
      //   });
      // });
            }
            catch(e){console.log(e)}            
        }
        img.src=dataurl;
    });
}, captureDelay);
}

chrome.tabs.onUpdated.addListener(function(id,object,tab) {
    if (tab.selected && tab.url) {
        if (localStorage['requestThumbnail']!='' && localStorage['requestThumbnail']!="undefined" && typeof localStorage["requestThumbnail"]!='undefined') {
      var requestThumbnail = localStorage['requestThumbnail'].split('|||');

      if (requestThumbnail[0] == tab.id) { 
        if ( tab.status=="complete" ) { 
            if (tab.url.indexOf('mail.google.com')>-1 || tab.url.indexOf('twitter.com')>-1) 
            {
                makeThumbnail(requestThumbnail[1],1000); 
            } else {
                makeThumbnail(requestThumbnail[1],500); 
            }
            localStorage['requestThumbnail']='';
        }
        requestThumbnail = null;
      }

    } 
    }
});

问题 - 大多数时候控制台会触发此错误消息。一旦进入蓝月亮,代码实际上将获得缩略图。据我所知,tabs.captureVisibleTab 在它打算之前触发。

我将考虑直接修复代码(更好)或如何使其更可靠的一般方向的有效答案。

我的系统 - Ubuntu 12.04、Chrome 24.0.1312.70

4

1 回答 1

1
  • chrome.tabs.getSelected不推荐使用 chrome.tabs.query 代替。
  • 不需要显式的 null 声明,chrome.tabs.captureVisibleTab(null默认为current window.

参考

于 2013-02-21T10:23:19.427 回答