开发者

Google Chrome Extension: captureVisibleTab problem

开发者 https://www.devze.com 2023-02-20 08:38 出处:网络
I\'m trying to capture the current visible tab but I\'m receiving undefined. The following code is executing when the extension\'s icon is pressed. When the alert is called I see undefined instead of

I'm trying to capture the current visible tab but I'm receiving undefined. The following code is executing when the extension's icon is pressed. When the alert is called I see undefined instead of an URL.

chrome.browserAction.onClicked.addListener(function(tab) {            
  chrome.windows.getCurrent(function (win) {    
      chrome.tabs.captureVisibleTab(win.id,{"format":"png"}, function(imgUrl) {
            alert(imgUrl);                                            
      });    
  });    
});

What should I do t开发者_如何学编程o get the URL of the captured image? Can someone please help me out with this.

Thanks!


I guess your code is taken from the example given on the Chrome Extension Website and yes, it's buggy.

Change the permission attribute inside the manifest.json to this:

"permissions": [
    "tabs"
    ,"<all_urls>"
]

Cheers, David


I tried your code and it did not return undefined for me. The following is the code.
Manifest.json

{
  "name": "Test",
  "version": "1.0",
  "background_page": "background.html",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
    "tabs"
  ]
}

Background.html

<html>
<head>
<script>
 chrome.browserAction.onClicked.addListener(function(tab) {            
  chrome.windows.getCurrent(function (win) {    
    chrome.tabs.captureVisibleTab(win.id,{"format":"png"}, function(imgUrl) {
        alert(imgUrl);                                            
    });    
  });    
 });
</script>
</head>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消