最近更新了chrome一个旧应用程序 - 书签管理器 - 我使用,停止工作。更具体地说,获取缩略图屏幕截图的部分无效。 错误消息是
“tabs.captureVisibleTab期间出错:无法访问网址内容 “铬:// 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
答案 0 :(得分:1)
chrome.tabs.getSelected
使用chrome.tabs.query。chrome.tabs.captureVisibleTab(null
中无需显式空声明,默认为current window
。