Home  >  Article  >  WeChat Applet  >  How mini programs detect content copied from WeChat

How mini programs detect content copied from WeChat

咔咔
咔咔Original
2020-12-14 11:08:322472browse

This article will lead everyone to implement the clipboard function of the mini program

Preface

In the past two days, I have been implementing a function to copy a connection from WeChat. When the mini program is opened, the content can be directly detected and pasted.

Implement function

  • Detect content copied by WeChat.
  • Regularly matches the required content.

1. Mini program to implement clipboard function

The effect is as follows

How mini programs detect content copied from WeChat
Achievement effect

Open the WeChat document and you can see the two interfaces provided, one is to set the contents of the system clipboard, and the other is to obtain the system clipboard Content

How mini programs detect content copied from WeChat
Interface provided by the document

So what this article will introduce to you is to obtain the contents of the system clipboard, the interface iswx. getClipboardData

First familiarize yourself with the usage rules of this interface according to the documentation.

How mini programs detect content copied from WeChat
Interface usage rules

The above are the rules for using the clipboard, and the usage methods are also provided in the document. Once you know these contents You can come to your own project for verification.

The process here is just to give some development experience to partners who are new to small programs.

wx.getClipboardData({
success (res){
console.log(res.data)
}
})

2. Code implementation

The picture below is a very simple implementation, you can see the copied data is printed directly on the console.

How mini programs detect content copied from WeChat
Code implementation

Then the next thing to do is to regular match the copied content to match the content we only need.

The following js code can create a new file in the utils directory.

How mini programs detect content copied from WeChat
Regular code
var t = {};

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

Then import the file where you need to import it and you can use it.

How mini programs detect content copied from WeChat
Introduction file

Call the data required for regular matching, and the returned value is the result of the matching.

How mini programs detect content copied from WeChat
Returned results

The second step is to no longer pop up the box when the content of the clipboard is detected to be consistent with the set value.

弹框的代码就是showModel这个接口,可以自行查看文档哈!

完整代码如下。

  onShow: function (res) {
let that = this;
wx.getClipboardData({
success: function (res) {
// 匹配地址
let result = util.handleUrl(res.data);
// 如果地址相同则不在显示
if(result == that.data.prase_address){
return;
}
wx.showModal({
title: '检测到视频链接,是否粘贴?',
content: result,
showCancel: true,//是否显示取消按钮
cancelText: "取消",//默认是“取消”
cancelColor: '#ff9900',//取消文字的颜色
confirmText: "粘贴",//默认是“确定”
confirmColor: '#ff9900',//确定文字的颜色
success: function (res) {
if (res.cancel) {
} else {
that.setData({
prase_address: result,
})
}
},
})
},
fail: function (res) { },
complete: function (res) {
},
})
},

坚持学习、坚持写博、坚持分享是咔咔从业以来一直所秉持的信念。希望在偌大互联网中咔咔的文章能带给你一丝丝帮助。我是咔咔,下期见。

The above is the detailed content of How mini programs detect content copied from WeChat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn