ImageClipboard js粘贴剪切板图片,已测试,可用,可获得base64

具体用到自己项目的时候,拿源码改成自己的库,从写一遍

3个小问题

  1. onpaste 执行了两遍,一次是图片加载完成,一次是加载图片之前。按说 应该搞两个事件来分别调用
  2. pasteCatcher 应该是作为一个保底实现,我也没看明白是怎么获取剪切板的图片,怎么就能拿到base64了,反正通过e.clipboardData.items是拿到图片了
  3. 大哥遍历了整个剪切板的图片,全部走了一遍函数,这性能会不会有点问题,也不知道。

结论

不管怎么说,这个代码能用,别的库,好多都不能用。

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的更多相关文章

  1. JS访问剪切板中的图片

    google出来一个html2canvas,它利用canvas来渲染读取的DOM树,也就是说它只能截取document里的内容,如果要像qq截图那样,应该怎么做?用过百度的Ueditor编辑器的朋友都 ...

  2. 经验分享:计算机 web 浏览器——访问剪切板图片

      有时候,我们希望能访问用户的剪切板,来实现一些方便用户的功能:但是另一方面,剪切板里的数据对用户来说又是非常隐私的,所以浏览器在获取信息方面有安全限制,同时也提供访问接口. 当我们需要实现在富文本 ...

  3. JS从剪切板里粘贴图片

    功能需求:在网页中,Ctrl+V,把系统剪切板的图片(比如QQ截图)进行粘贴.显示.上传...,提高用户体验. 参考链接:https://ruby-china.org/topics/17266 git ...

  4. js修改剪切板内容的方法

    代码如下: //绑定在了body上,也可以绑定在其他可用元素行,但是不是所有元素都支持copy事件. $(document.body).bind({ copy: function(e) {//copy ...

  5. js 操作剪切板

    1.IE浏览器 window.clipboardData: setData() //设置值 getData()//获取值 clearData()//删除值 /******* ** IE 浏览器下支持w ...

  6. JS实现剪切板添加网站版权、来源

    公司官网有这样需求,写好后,备份以后留用. 只兼容chrome.firefox.IE9+等主流浏览器. // https://developer.mozilla.org/en-US/docs/Web/ ...

  7. js操作cookie(转载:经测试可用)

    /***js操作cookie,star***/ function addCookie(objName,objValue,objsec){//添加cookie  var str = objName + ...

  8. win7下virtualbox装linux共享win7文件问题(已测试可用)

    virtualbox这个比较强大,在win7上跑redhat5u4很流畅.os之间共享文件是个大家都很关心的问题,这会直接关系到虚拟机用的爽不爽. 在win7和其上的虚拟机linux之间共享文件也很容 ...

  9. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  10. Javascript操作剪切板数据(支持IE、Chrome、360、搜狗),亲测!

    clipboarddata只能在IE浏览器中使用,在chrome下会提示对象未定义!以下的方法支持IE.Chrome.360.搜狗等浏览器,其它浏览器还未验证. <!DOCTYPE html&g ...

随机推荐

  1. C/C++ Qt 编译打包项目

    Qt程序编译后,需要去qt目录拷贝几个文件,与qt程序放在一起该程序才可以脱离开发环境而独立运行下去,在开发环境下编译好代码以后,还需要进行以下操作将其打包才可以在别的机器上正常运行. QT的下载地址 ...

  2. LyScript 实现应用层钩子扫描器

    Capstone 是一个轻量级的多平台.多架构的反汇编框架,该模块支持目前所有通用操作系统,反汇编架构几乎全部支持,本篇文章将运用LyScript插件结合Capstone反汇编引擎实现一个钩子扫描器. ...

  3. 【奶奶看了都会】云服务器ChatGLM模型fine-tuning微调,让你拥有自己的知识库

    1.背景 大家好啊,上次给大家写了ChatGLM-6B的部署使用教程,[奶奶看了都会]云服务器部署开源ChatGLM-6B,让你拥有自己的ChatGPT 但是因为模型比较小的问题,所以日常工作中可能用 ...

  4. SpringBoot不再需要@Autowired来注入属性

    实操部分 需要lombok依赖 在对应需要注入属性的类上添加注解 @RequiredArgsConstructor 所有需要注入的属性改为final修饰 为什么 lombok的@RequiredArg ...

  5. ListView改变行高的技巧

    改变 ListView 的行高 (Line Height) (cjc,2009.6.2) 改变 ListView 的行高 (Line Height) (cjc,2009.6.2) ListView在R ...

  6. .NET Core开发实战(第22课:异常处理中间件:区分真异常与逻辑异常)--学习笔记(下)

    接下来介绍使用代理方法的方式,也就是说把 ErrorController 整段逻辑直接定义在注册的地方,使用一个匿名委托来处理,这里的逻辑与之前的逻辑是相同的 app.UseExceptionHand ...

  7. offline 2 online | Cal-QL:校准保守 offline 训出的 Q value,让它与真实 reward 尺度相当

    论文标题:Cal-QL: Calibrated Offline RL Pre-Training for Efficient Online Fine-Tuning. NeurIPS 2023,5 5 6 ...

  8. Pandas字符串离散化处理

    字符串离散化处理 import pandas as pd import numpy as np from matplotlib import pyplot as plt # 读取csv文件 file_ ...

  9. CentOS 7.3 源码安装squid 4.12 及安装过程遇到的一些问题

    一.源码安装squid 4.12 1.下载squid-4.12源码包 wget http://www.squid-cache.org/Versions/v4/squid-4.12.tar.gz tar ...

  10. 对称加密算法汇总:AES DES 3DES SM4 java 实现入门

    密码的世界 如果你是黑帮老大,平时和手下沟通,如何保证自己的信息安全呢? 在神探夏洛克的第一季中,就讲述了一个如何侦破黑帮的加密交流的故事. 这种密码利用的是密码字典. 密码本身可以是一本书,比如常见 ...