Skip to content

Instantly share code, notes, and snippets.

@ictus4u
Last active May 12, 2021 16:45
Show Gist options
  • Save ictus4u/05a45b0b9dec0eac531813c97f618c5f to your computer and use it in GitHub Desktop.
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.
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');
}
})();
@jsilvao
Copy link

jsilvao commented May 12, 2021

Very handy Ictus 😄 👍 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment