微信小程序 获取剪切板上的内容

前言:

为了开发一个去水印的小程序,用到了读取剪切板复制粘贴的功能

开发思路:

1、获取剪切板
2、正则判断并截取视频URL
3、弹出对话框是否粘贴

用到的技术:

1、微信官方文档(剪切板)

wx.setClipboardData({   //设置剪切板
  data: 'data',
  success (res) {
    wx.getClipboardData({ //获取剪切板
      success (res) {
        console.log(res.data) // data
      }
    })
  }
})

2、正则

handleUrl(t) {var e = /(http://|https://)((w|=|?|.|/|&|-)+)/g;return !!(t = t.match(e)) && t[0];)

3、全部代码

 data:{
   value:null
 }
handleUrl(t) {var e = /(http://|https://)((w|=|?|.|/|&|-)+)/g;return !!(t = t.match(e)) && t[0];
onShow: function () {
 let that = this
 wx.getClipboardData({
   success: function (res) { // 匹配地址
     let result = handleUrl(res.data);
     // 如果地址相同则不在显示
     if (result == that.data.value) {
       return;
     }
     wx.showModal({
       title: '检测到视频链接,是否粘贴?',
       content: result,   //这个地方会提示报错改成string格式就行
       showCancel: true, //是否显示取消按钮cancelText: "取消",//默认是“取消”
       cancelColor: '#8799a3', //取消文字的颜色
       confirmText: "粘贴", //默认是“确定”
       confirmColor: '#3385FF', //确定文字的颜色
       success: function (res) {
         if (res.cancel) {

         } else {
           that.setData({
             value: result,  //赋值到输入框
           })
         }
       },
     })
   },
   fail: function (res) {},
   complete: function (res) {},
 })
}

原文地址:https://www.cnblogs.com/yx-liu/p/15206472.html