2014-04-16 96 views
1

我在这里看到很多答案,但没有人是我在找什么。 我想从Chrome扩展中截取屏幕截图,仅用于我第一次看到的屏幕,而不滚动页面。 并“提醒”创建的文件base64路径。开发屏幕截图镀铬扩展

我拥有所有正确的权限:

"permissions": [ 
    "activeTab", 
    "tabs" , 
    "storage", 
    "unlimitedStorage", 
    "browsingData", 
    "notifications", 
    "http://*/*", 
    "https://*/*", 
    "file://*/*", 
    "background" // added after i got the answer 
], 
"background": { // added after i got the answer 
    "scripts": [ 
     "js/background.js" 
    ] 
}, 
在我的manifest.json

我也有代码:

$(document).ready(function() { 
    alert("1"); 
    chrome.tabs.captureVisibleTab(null, {}, function (image) { 
     alert("2"); 
    }); 
}); 

我得到1所有的时间,但2我从来没有得到,我不知道为什么。请帮助..

感谢..

UPDATE

那是缺失的部分(background.js)

 chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) { 
    chrome.tabs.captureVisibleTab(
     null, 
     {}, 
     function(dataUrl){ 
      sendResponse({imgSrc:dataUrl}); 
     }); //remember that captureVisibleTab() is a statement 
    return true; 
} 
); 

然后:

 chrome.tabs.captureVisibleTab(null, {}, function (image) { 
     // alert("2"); 
      alert(response.imgSrc); 
    }); 

回答

3

你不能在内容脚本中执行扩展API调用。如果您真的想在c中触发此函数,请尝试使用消息传递内容脚本。

请注意,自chrome修订版本246766以来,tabs.captureVisibleTab()的权限要求已更新。

Extension need to have '< all_urls >' permission, or been granted the 'activeTab' permission to be allowed to use tabs.captureVisibleTab().

Developer doc没有提及它。

的manifest.json

"permissions": [ 
    "activeTab", 
    "tabs" , 
    "storage", 
    "unlimitedStorage", 
    "browsingData", 
    "notifications", 
    "http://*/*", 
    "https://*/*", 
    "file://*/*", 
    "<all_urls>" 
] 

尝试在后台页面下执行这个代码,并如预期截图捕获会工作。

chrome.tabs.captureVisibleTab(null,{},function(dataUri){ 
    console.log(dataUri); 
}); 

截图

enter image description here

+0

我得到的消息:遗漏的类型错误:无法读取的不确定 – avishayhajbi

+0

财产“captureVisibleTab”你调用captureVisibleTab内容脚本? – Chickenrice

+0

你对我非常感谢,我修好了! – avishayhajbi

0

它实际上并没有在谷歌网页的扩展页面上工作,是它看起来非常自然,以测试你的扩展而发展。 (应该是测试在创建快照之前的方式,我觉得...)