1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
(function () { function ChangeLinkWA() { // The {wz_metric} token is used for substituting the Analytics code. // Make sure that the token and the text in which it is written, // were separated by at least one space character. this.text = 'Hello! My application number: {wz_metric}'; this.cookieSource = '_ym_uid'; this.observer = null; } ChangeLinkWA.prototype.editLink = function (url, id) { if (decodeURIComponent(url.split('text=')[1]) === this.text.replace(/{wz_metric}/gi, id)) return; var regexNumberPhone = /\d+/; if(!regexNumberPhone.test(url)) return var phone = url.match(regexNumberPhone)[0]; var host = url.split(phone)[0]; var newUrl = (host === 'https://wa.me/') ? `${host}${phone}?text=${this.text.replace(/{wz_metric}/gi, id)}` : `${host}${phone}&text=${this.text.replace(/{wz_metric}/gi, id)}` return newUrl; }; ChangeLinkWA.prototype.getCookie = function (name) { var cookie = document.cookie; var matches = cookie.match(new RegExp(`(?:^|; )${ name.replace(/([.$?*|{}()[]\/+^])/g, '\\$1') }=([^;]*)`)); return matches ? decodeURIComponent(matches[1]) : undefined; }; ChangeLinkWA.prototype.censusLinks = function () { var links = document.querySelectorAll('[href*="//wa.me"], [href*="//api.whatsapp.com/send"], [href*="//web.whatsapp.com/send"], [href^="whatsapp://send"]'); var id = this.getCookie(this.cookieSource); var that = this; links.forEach(function(link) { var newLink = that.editLink(link.href, id) if (newLink) link.href = newLink; }); }; ChangeLinkWA.prototype.initObserver = function () { var that = this; this.observer = new MutationObserver(function () { that.censusLinks(); }); }; ChangeLinkWA.prototype.startObserver = function () { this.observer.observe(document.body, { attributeFilter: ['href'], attributes: true, childList: true, attributeOldValue: true, characterDataOldValue: true, characterData: true, subtree: true, }); }; ChangeLinkWA.prototype.init = function () { this.initObserver(); this.startObserver(); }; if (!(window.__wz_scripts && window.__wz_scripts.scriptsChangeLinkWA)) { if (!window.__wz_scripts) window.__wz_scripts = {}; window.__wz_scripts.scriptsChangeLinkWA = new ChangeLinkWA(); window.__wz_scripts.scriptsChangeLinkWA.init(); } })(); |