Google Chrome Extension: captureVisibleTab problem

女生的网名这么多〃 提交于 2020-01-02 06:30:30

问题


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 to get the URL of the captured image? Can someone please help me out with this.

Thanks!


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/5453459/google-chrome-extension-capturevisibletab-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!