Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展:自动下载使用“Chrome.tabs.captureVisibleTab”拍摄的屏幕截图_Javascript_Google Chrome_Google Chrome Extension_Screenshot - Fatal编程技术网

Javascript Chrome扩展:自动下载使用“Chrome.tabs.captureVisibleTab”拍摄的屏幕截图

Javascript Chrome扩展:自动下载使用“Chrome.tabs.captureVisibleTab”拍摄的屏幕截图,javascript,google-chrome,google-chrome-extension,screenshot,Javascript,Google Chrome,Google Chrome Extension,Screenshot,我是Chrome扩展/自动下载领域的新手。我有一个背景页面,它用chrome.tabs.captureVisibleTab截图显示了可见网页。在我的弹出窗口中,我有: chrome.tabs.captureVisibleTab(null, {}, function (image) { // Here I want to automatically download the image }); 我以前也用blob做过类似的事情,但我完全不知道如何下载图像以及如何自动下载 实际上,我希望我的C

我是Chrome扩展/自动下载领域的新手。我有一个背景页面,它用chrome.tabs.captureVisibleTab截图显示了可见网页。在我的弹出窗口中,我有:

chrome.tabs.captureVisibleTab(null, {}, function (image) {
  // Here I want to automatically download the image
});
我以前也用blob做过类似的事情,但我完全不知道如何下载图像以及如何自动下载


实际上,我希望我的Chrome扩展能够在加载特定页面时自动截图+下载图像。我猜这必须通过让我的内容脚本与我的背景页面对话来实现,对吗?

是的,正如你所说,你可以用它来完成。通过内容脚本检测特定页面上的切换,然后与后台页面聊天,以便捕获该页面的屏幕截图。内容脚本应使用chrome.runtime.sendMessage发送消息,后台页面应使用chrome.runtime.onMessage.addListener进行侦听:

我创建并测试的示例代码与我一起使用:

Content scriptmyscript.js:

Background.js:

还要记住在清单文件中注册内容脚本的代码和权限:


它捕获屏幕截图,并在加载特定页面时以.png格式自动下载图像。干杯

是的,正如你所说,你可以用它来完成。通过内容脚本检测特定页面上的切换,然后与后台页面聊天,以便捕获该页面的屏幕截图。内容脚本应使用chrome.runtime.sendMessage发送消息,后台页面应使用chrome.runtime.onMessage.addListener进行侦听:

我创建并测试的示例代码与我一起使用:

Content scriptmyscript.js:

Background.js:

还要记住在清单文件中注册内容脚本的代码和权限:


它捕获屏幕截图,并在加载特定页面时以.png格式自动下载图像。干杯

我用你的代码下载了网页的自动截图。但它总是提醒我保存文件。我不想有一个警报,并保存它自动下载。。。我使用的mac和chrome浏览器都是最新的。有什么想法吗?我用你的代码下载了网页的自动截图。但它总是提醒我保存文件。我不想有一个警报,并保存它自动下载。。。我使用的mac和chrome浏览器都是最新的。有什么想法吗
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {

  });
var screenshot = {
  content : document.createElement("canvas"),
  data : '',

  init : function() {
    this.initEvents();
  },
 saveScreenshot : function() {
    var image = new Image();
    image.onload = function() {
      var canvas = screenshot.content;
      canvas.width = image.width;
      canvas.height = image.height;
      var context = canvas.getContext("2d");
      context.drawImage(image, 0, 0);

      // save the image
      var link = document.createElement('a');
      link.download = "download.png";
      link.href = screenshot.content.toDataURL();
      link.click();
      screenshot.data = '';
    };
    image.src = screenshot.data; 
  },
initEvents : function() {
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.greeting == "hello") {
          chrome.tabs.captureVisibleTab(null, {format : "png"}, function(data) {
                screenshot.data = data;
                screenshot.saveScreenshot();

            }); 

        }
    });
  }
};
screenshot.init();
"permissions": ["<all_urls>","tabs"],
    "content_scripts": [
    {
      "matches": ["http://www.particularpageone.com/*", "http://www.particularpagetwo.com/*"],
      "js": ["myscript.js"]
    }
  ]