ImageClipboard js粘贴剪切板图片,已测试,可用,可获得base64
ImageClipboard js粘贴剪切板图片,已测试,可用,可获得base64
具体用到自己项目的时候,拿源码改成自己的库,从写一遍
3个小问题
- onpaste 执行了两遍,一次是图片加载完成,一次是加载图片之前。按说 应该搞两个事件来分别调用
- pasteCatcher 应该是作为一个保底实现,我也没看明白是怎么获取剪切板的图片,怎么就能拿到base64了,反正通过e.clipboardData.items是拿到图片了
- 大哥遍历了整个剪切板的图片,全部走了一遍函数,这性能会不会有点问题,也不知道。
结论
不管怎么说,这个代码能用,别的库,好多都不能用。
index.html
<!DOCTYPE html>
<html>
<head>
<!-- https://github.com/jorgenbs/ImageClipboard/tree/master -->
<script src="ImageClipboard.js"></script>
</head>
<body>
<div id="box">
</div>
<script type="text/javascript">
var clipboard = new ImageClipboard('#box');
//onpaste-callback can also be passed as second argument
//in the constructor above.
clipboard.onpaste = function (base64) {
//do stuff with the pasted image
console.info('clipboard.onpaste')
};
//you can also pass in single DOM-element instead of
//query as the first parameter.
</script>
</body>
</html>
ImageClipboard.js
/*jshint boss:true, laxcomma: true, expr: true*/
!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition;
else if (typeof define == 'function' && define.amd) define(name, definition);
else this[name] = definition;
}('ImageClipboard', function (selector, callback) {
'use strict';
var self = typeof this === 'object' ? this : {};
self.el = null;
self.pasteCatcher = null;
self.clipImage = null;
self.onpaste = null;
self.browserSupport = true;
self.init = function (selector, callback) {
if (typeof selector === "string") {
self.el = document.querySelector(selector);
}
else if (_isElement(selector)) self.el = selector;
else return false;
self.pasteCatcher = null;
self.clipImage = null;
self.onpaste = typeof callback === 'function' ? callback : function () { };
//pasting not supported, make workaround
if (!window.Clipboard) {
self.pasteCatcher = _makePasteCatcher();
}
window.addEventListener('paste', self.pasteHandler);
return self;
};
self.pasteHandler = function (e) {
var items;
if (e.clipboardData && e.clipboardData.items) {
items = e.clipboardData.items;
if (items) {
items = Array.prototype.filter.call(items, function (element) {
return element.type.indexOf("image") >= 0;
});
console.info('items.length', items.length)
Array.prototype.forEach.call(items, function (item) {
var blob = item.getAsFile();
var rdr = new FileReader();
rdr.onloadend = function () {
_loadImage(rdr.result);
};
rdr.readAsDataURL(blob);
});
}
}
else if (self.pasteCatcher) {
//no direct access to clipboardData (firefox)
//use the pastecatcher
setTimeout(function () {
var child = self.pasteCatcher.firstElementChild;
if (child && child.tagName == "IMG") {
_loadImage(child.src);
}
}, 5);
}
};
function _makePasteCatcher() {
var pasteBox = document.createElement("div");
pasteBox.setAttribute("id", "paste_catcher");
pasteBox.setAttribute("contenteditable", "");
pasteBox.style.opacity = 0;
document.body.insertBefore(pasteBox, document.body.firstChild);
pasteBox.focus();
self.el.addEventListener("click", function () { pasteBox.focus(); });
return pasteBox;
}
function _loadImage(source) {
var img = new Image();
self.el.innerHTML = "";
img.onload = function () {
//got picture, display it
var imgContainer = document.createElement("img");
imgContainer.src = img.src;
imgContainer.style.maxHeight = "100%";
imgContainer.style.maxHeight = "100%";
self.el.appendChild(imgContainer);
//empty out the ol' pastecatcher
if (self.pasteCatcher) self.pasteCatcher.innerHTML = "";
self.clipImage = img;
if (typeof self.onpaste === 'function')
self.onpaste(img.src);
};
img.src = source;
self.onpaste.call(self, source.split(",")[1]); //callback(base64, file-type)
}
function _isElement(obj) {
return typeof HTMLElement === "object" ? obj instanceof HTMLElement :
obj && typeof obj === "object" && obj.nodeType === 1 && typeof obj.nodeName === "string";
}
return self.init(selector, callback);
});
ImageClipboard js粘贴剪切板图片,已测试,可用,可获得base64的更多相关文章
- JS访问剪切板中的图片
google出来一个html2canvas,它利用canvas来渲染读取的DOM树,也就是说它只能截取document里的内容,如果要像qq截图那样,应该怎么做?用过百度的Ueditor编辑器的朋友都 ...
- 经验分享:计算机 web 浏览器——访问剪切板图片
有时候,我们希望能访问用户的剪切板,来实现一些方便用户的功能:但是另一方面,剪切板里的数据对用户来说又是非常隐私的,所以浏览器在获取信息方面有安全限制,同时也提供访问接口. 当我们需要实现在富文本 ...
- JS从剪切板里粘贴图片
功能需求:在网页中,Ctrl+V,把系统剪切板的图片(比如QQ截图)进行粘贴.显示.上传...,提高用户体验. 参考链接:https://ruby-china.org/topics/17266 git ...
- js修改剪切板内容的方法
代码如下: //绑定在了body上,也可以绑定在其他可用元素行,但是不是所有元素都支持copy事件. $(document.body).bind({ copy: function(e) {//copy ...
- js 操作剪切板
1.IE浏览器 window.clipboardData: setData() //设置值 getData()//获取值 clearData()//删除值 /******* ** IE 浏览器下支持w ...
- JS实现剪切板添加网站版权、来源
公司官网有这样需求,写好后,备份以后留用. 只兼容chrome.firefox.IE9+等主流浏览器. // https://developer.mozilla.org/en-US/docs/Web/ ...
- js操作cookie(转载:经测试可用)
/***js操作cookie,star***/ function addCookie(objName,objValue,objsec){//添加cookie var str = objName + ...
- win7下virtualbox装linux共享win7文件问题(已测试可用)
virtualbox这个比较强大,在win7上跑redhat5u4很流畅.os之间共享文件是个大家都很关心的问题,这会直接关系到虚拟机用的爽不爽. 在win7和其上的虚拟机linux之间共享文件也很容 ...
- 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件
[源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...
- Javascript操作剪切板数据(支持IE、Chrome、360、搜狗),亲测!
clipboarddata只能在IE浏览器中使用,在chrome下会提示对象未定义!以下的方法支持IE.Chrome.360.搜狗等浏览器,其它浏览器还未验证. <!DOCTYPE html&g ...
随机推荐
- 8.1 C++ STL 变易拷贝算法
C++ STL中的变易算法(Modifying Algorithms)是指那些能够修改容器内容的算法,主要用于修改容器中的数据,例如插入.删除.替换等操作.这些算法同样定义在头文件 <algor ...
- Python Selenium 库使用技巧
Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE,Mozilla Firefox,Safari,Google ...
- 驱动开发:Win10枚举完整SSDT地址表
在前面的博文<驱动开发:Win10内核枚举SSDT表基址>中已经教大家如何寻找SSDT表基地址了,找到后我们可根据序号获取到指定SSDT函数的原始地址,而如果需要输出所有SSDT表信息,则 ...
- html 图片地图
<html> <head> <title></title> </head> <body> <img src="8 ...
- 音乐播放器 — 用 vant4 中的滑块自定义播放器进度条
一.运行效果 二.代码实现 2.1.HTML: <!-- 音频播放器 --> <audio ref="audio" src="音乐名称.mp3" ...
- 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】
今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现!话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://bl ...
- 使用DoraCloud构建远程办公桌面云
公司总部在上海.员工分布在各地.部分员工需要远程办公.为了实现远程办公,有几种备选方案. 方案1.在员工的PC上安装向日葵.ToDesk之类的远程工具. 方案2.公司总部提供VPN,员工通过VPN拨号 ...
- 洛谷P2415 集合求和(数学问题,使用集合子集求和公式)
可以知道对于一个有n个数据的集合,其子集个数有2^n个 至于证明可以这样理解,对于n个数据,其子集就是对数据进行组和,而对于每个位置上的数据,组合时仅有两种状态即有此数据或无此数据,也就是有两种可能, ...
- Linux-nmon系统监控工具
一.Nmon介绍 Nmon得名于 Nigel 的监控器,是IBM的员工 Nigel Griffiths 为 AIX 和 Linux 系统开发的,使用 Nmon 可以很轻松的监控系统的 CPU.内存.网 ...
- 使用python 打包成exe文件
python 打包exe 起因:闲的蛋疼 过程:扯的蛋疼 结果:不疼了 1.起因(闲的蛋疼) 突然的emo,不想干活,于是乎找遍微信好友,群发了十年八辈子不联系的一群人(此办法学习 ...