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');
}
})();
@ictus4u
Copy link
Author

ictus4u commented May 12, 2021

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:

  1. Change the defaultCountryCode value to the one that suits you.
  2. Create a bookmark in your browser (tested so far with Google Chrome) and copy the content of this gist to the URL field.

Usage:

  1. While browsing, select a relevant phone number from the web
  2. Click on the bookmarklet you created
  3. It will open a new tab/window with a chat for that number in web.whatsapp.com
  4. Or, if the selection doesn't contain a phone number, it will give you a prompt to write it

@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