Last active
May 12, 2021 16:45
-
-
Save ictus4u/05a45b0b9dec0eac531813c97f618c5f to your computer and use it in GitHub Desktop.
Bookmarklet for sending WS message to a selected phone number from the Web.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function () { | |
"use strict"; | |
var defaultCountryCode = '49'; | |
var phoneRegexp = /^\(?(?:00|\+?)?([0-9]{2})?\)?[-. ]?([0-9])[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; | |
var isPhone = function (text) { | |
return !!text.match(phoneRegexp); | |
}; | |
var normalizePhone = function (phoneText) { | |
var groups = phoneText.match(phoneRegexp); | |
var countryCode = groups[1] != undefined ? groups[1] : defaultCountryCode; | |
var phoneNumber = countryCode + groups[2] + groups[3] + groups[4]; | |
return phoneNumber; | |
}; | |
var phoneNumber; | |
var selectedText = window.getSelection().toString(); | |
if (isPhone(selectedText)) { | |
phoneNumber = normalizePhone(selectedText); | |
} else { | |
phoneNumber = prompt('Phone: ', defaultCountryCode); | |
} | |
if (!!phoneNumber.match(/^[0-9]{10}$/)) { | |
window.open(`https://web.whatsapp.com/send?phone=${phoneNumber}`, '_blank'); | |
} else { | |
alert('Error: Bad phone format'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is intended for desktop browsing, using https://web.whatsapp.com as a chat enabler. You are expected to have it linked to your phone.
Setup:
defaultCountryCode
value to the one that suits you.Usage: