谷歌浏览器扩展程序:captureVisibleTab问题

4

我想获取当前可见的标签页,但是我收到了未定义的消息。 下面的代码在扩展程序图标被按下时执行。当调用警报时,我看到的是undefined而不是一个URL。

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

我该怎么做才能获取捕获图像的URL呢?有人能帮帮我吗?

谢谢!

2个回答

5

我猜你的代码是从Chrome扩展网站的示例中获取的,是有漏洞的。

将manifest.json中的permission属性更改为以下内容:

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

干杯,David。

1

我尝试了你的代码,对我来说它没有返回undefined。以下是代码。
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>

好的,谢谢!也许是我的浏览器出了问题。我会重新安装它。 - Julian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接