Heim  >  Artikel  >  WeChat-Applet  >  Wie Miniprogramme von WeChat kopierte Inhalte erkennen

Wie Miniprogramme von WeChat kopierte Inhalte erkennen

咔咔
咔咔Original
2020-12-14 11:08:322579Durchsuche
"

Dieser Artikel führt jeden dazu, die Zwischenablagefunktion des Miniprogramms zu implementieren

"

Vorwort

In den letzten zwei Tagen habe ich eine Funktion zum Kopieren implementiert Verbindung von WeChat. Wenn Sie ein Miniprogramm verwenden, können Sie den Inhalt direkt erkennen und einfügen.

Implementieren Sie die Funktion

  • , um von WeChat kopierte Inhalte zu erkennen.
  • passt regelmäßig zum erforderlichen Inhalt.

1. Das Miniprogramm implementiert die Zwischenablagefunktion.

Der Implementierungseffekt ist wie folgt:

Wie Miniprogramme von WeChat kopierte Inhalte erkennen
Der Implementierungseffekt: Öffnen Sie das WeChat-Dokument und Sie können die beiden bereitgestellten Schnittstellen sehen. Eine besteht darin, das System einzurichten. Der Inhalt der Zwischenablage ist eine vom Dokument bereitgestellte Schnittstelle, um den Inhalt der Systemzwischenablage abzurufen Zwischenablage, die Schnittstelle ist
wx.getClipboardData

Machen Sie sich zunächst anhand der Dokumentation mit den Nutzungsregeln dieser Schnittstelle vertraut.

Wie Miniprogramme von WeChat kopierte Inhalte erkennen
Nutzungsregeln für die Schnittstelle

Die oben genannten Regeln für die Verwendung der Zwischenablage und die Verwendungsmethoden sind auch im Dokument aufgeführt. Sobald Sie diese Inhalte kennen, können Sie zur Überprüfung zu Ihrem eigenen Projekt kommen.

Der Prozess hier dient lediglich dazu, Partnern, die neu bei Miniprogrammen sind, etwas Entwicklungserfahrung zu vermitteln.

<span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">wx.getClipboardData({<br/>  success (res){<br/>    console.log(res.data)<br/>  }<br/>})<br/></code>

2. Code-Implementierung

Das Bild unten ist eine sehr einfache Implementierung. Sie können sehen, dass die kopierten Daten direkt auf der Konsole gedruckt werden.

Wie Miniprogramme von WeChat kopierte Inhalte erkennen
Code-Implementierung

Dann müssen Sie als Nächstes den kopierten Inhalt so regulieren, dass er mit dem Inhalt übereinstimmt, den wir nur benötigen.

Der folgende js-Code kann eine neue Datei im Utils-Verzeichnis erstellen.

Wie Miniprogramme von WeChat kopierte Inhalte erkennen
Regulärer Code
<span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">var</span> t = {};<br/><br/>t.handleUrl = <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span>(<span class="hljs-params" style="line-height: 26px;">t</span>) </span>{<br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">var</span> e = <span class="hljs-regexp" style="color: #98c379; line-height: 26px;">/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g</span>;<br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> !!(t = t.match(e)) && t[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>];<br/>}, <span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">module</span>.exports = t;<br/></code>

Importieren Sie dann die Datei dort, wo Sie sie importieren müssen, und Sie können sie verwenden.

Wie Miniprogramme von WeChat kopierte Inhalte erkennen
Führen Sie die Datei ein

Rufen Sie die für den regulären Abgleich erforderlichen Daten auf, und der zurückgegebene Wert ist das Ergebnis des Abgleichs.

Wie Miniprogramme von WeChat kopierte Inhalte erkennen
Das zurückgegebene Ergebnis

Der zweite Schritt besteht darin, das Popup-Fenster nicht mehr anzuzeigen, wenn festgestellt wird, dass der Inhalt der Zwischenablage mit dem eingestellten Wert übereinstimmt.

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

完整代码如下。

<span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">  onShow: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> (<span class="hljs-params" style="line-height: 26px;">res</span>) </span>{<br/>    <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">let</span> that = <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">this</span>;<br/>    wx.getClipboardData({<br/>      <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">success</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> (<span class="hljs-params" style="line-height: 26px;">res</span>) </span>{<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 匹配地址</span><br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">let</span> result = util.handleUrl(res.data);<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 如果地址相同则不在显示</span><br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span>(result == that.data.prase_address){<br/>          <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span>;<br/>        }<br/>        wx.showModal({<br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">title</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">&#39;检测到视频链接,是否粘贴?&#39;</span>,<br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">content</span>: result,<br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">showCancel</span>: <span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">true</span>,<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">//是否显示取消按钮</span><br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">cancelText</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">"取消"</span>,<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">//默认是“取消”</span><br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">cancelColor</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">&#39;#ff9900&#39;</span>,<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">//取消文字的颜色</span><br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">confirmText</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">"粘贴"</span>,<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">//默认是“确定”</span><br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">confirmColor</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">&#39;#ff9900&#39;</span>,<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">//确定文字的颜色</span><br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">success</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> (<span class="hljs-params" style="line-height: 26px;">res</span>) </span>{<br/>            <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">if</span> (res.cancel) {<br/>            } <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">else</span> {<br/>              that.setData({<br/>                <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">prase_address</span>: result,<br/>              })<br/>            }<br/>          },<br/>        })<br/>      },<br/>      <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">fail</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> (<span class="hljs-params" style="line-height: 26px;">res</span>) </span>{ },<br/>      <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">complete</span>: <span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">function</span> (<span class="hljs-params" style="line-height: 26px;">res</span>) </span>{<br/>       },<br/>    })<br/>  },<br/></code>

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

Das obige ist der detaillierte Inhalt vonWie Miniprogramme von WeChat kopierte Inhalte erkennen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn