Implementing Clipboard Functionality in WeChat Mini Programs

This article guides developers through implementing clipboard detection and paste functionality in WeChat mini programs, covering the use of wx.getClipboardData, regex URL extraction, conditional modal prompts, and provides complete sample code for integration.

php Courses
php Courses
php Courses
Implementing Clipboard Functionality in WeChat Mini Programs

The tutorial explains how to detect content copied from WeChat, extract the desired URL using regular expressions, and paste it into a mini‑program.

Key steps include:

Detecting the clipboard content copied from WeChat.

Using a regular expression to match the required URL.

The WeChat Mini Program provides two interfaces for clipboard operations; the article focuses on the retrieval interface wx.getClipboardData. The usage rules of this API are shown in the documentation screenshot.

Basic usage example:

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

For URL extraction, a utility module is created (e.g., utils/clipboard.js) with the following regex handling code:

var t = {};

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

The utility is imported where needed, and the extracted URL is passed to a modal dialog. If the detected URL matches the previously stored one, the dialog is suppressed; otherwise, wx.showModal displays a prompt asking the user to paste the link.

Full integration code (excerpt):

onShow: function (res) {
  let that = this;
  wx.getClipboardData({
    success: function (res) {
      // Match address
      let result = util.handleUrl(res.data);
      // If address is the same, do not show modal
      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) {
            // user cancelled
          } else {
            that.setData({
              prase_address: result,
            })
          }
        },
        fail: function (res) {},
        complete: function (res) {}
      })
    },
    fail: function (res) {},
    complete: function (res) {}
  })
},

By following these steps, developers can seamlessly detect and paste URLs copied from WeChat into their mini‑programs, improving user experience.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Mobile DevelopmentJavaScriptWeChat Mini Programclipboardwx.getClipboardData
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.