简体   繁体   English

下载画布作为chrome扩展程序中的图像

[英]download a canvas as an image in chrome extension

This code work for me for downloading a canvas as an image in a custom html page: 此代码对我有用,可用于在自定义html页面中将画布下载为图像:

var save = function() {
            var x= document.getElementById("output-canvas");
            var dt = x.toDataURL('image/png');
            this.href = dt;
};
downloadLink.addEventListener('click', save, false);

but it doesn't work (without any errors) for canvas in popup.html in a chrome plugin which i am developing. 但它对我正在开发的Chrome插件中的popup.html中的canvas无效(没有任何错误)。

Could anybody help me with a solution for downloading canvas items in a chrome plugin? 有人可以帮我提供在Chrome插件中下载画布项目的解决方案吗?

Edited: 编辑:

This is my manifest file: 这是我的清单文件:

{
  ...
  "permissions": [ "contextMenus", "tabs", "http://*/*", "https://*/*", "activeTab", "storage"],
  ...
}

Checklist: 清单:

  1. The image URLs you draw on canvas should be allowed in manifest.json "permissions" : 您在画布上绘制的图像URL应该在manifest.json “ permissions”中允许使用

     "permissions": [ "activeTab", "https://i.imgur.com/*" ], 
  2. The link element should have a download attribute that contains a name of the file to save: link元素应具有download属性 ,其中包含要保存的文件的名称:

     <a download="test.png" href="...">Save me</a> 

    or 要么

     var link = document.createElement('a'); link.download = 'test.png'; link.href = canvas.toDataURL('image/png'); document.body.appendChild(link); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM