npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

share-text-to-whatsapp

v1.0.2

Published

A small JS utility library for sharing text content via WhatsApp or via the native sharing widget of your device.

Downloads

361

Readme

Share Text to WhatsApp

A small JS utility library for sharing text content via WhatsApp or via the native sharing widget of your device.

Install

$ npm install --save share-text-to-whatsapp

// or

$ yarn add share-text-to-whatsapp

API

shareTextToWhatsApp(text)

This function uses WhatsApp's Click to Chat feature which allows you to begin a chat with someone without having their phone number saved in your phone's address book.

Click to Chat works on both phone and WhatsApp Web.

import { shareTextToWhatsApp } from 'share-text-to-whatsapp';

const message = 'Hello world';
shareTextToWhatsApp(message); // This will open up WhatsApp and you will be shown a list of contacts you can send your message to.

How to format WhatsApp messages?

WhatsApp allows you to format selected text inside your messages.

  • To italicize your message, place an underscore on both sides of the text, like so: _text_
  • To bold your message, place an asterisk on both sides of the text, like so: *text*

For more information on formatting, see this guide.

How to share multi-line content?

Use template literals to create multi-line messages like the following below.

import { shareTextToWhatsApp } from 'share-text-to-whatsapp';

console.log(
  getWhatsAppClickToChatLink(`
  This is a multi-line text
  how will it be handled
  _this is italic_
  *this is bold*
  `)
);

shareTextViaNativeSharing(data, options)

Depending on the device, it invokes the native sharing mechanism of the device as part of the Web Share API.

import { shareTextViaNativeSharing } from 'share-text-to-whatsapp';

const data = {
  message: 'Check out this website', // required
  title: 'Awesome Website', // optional parameter
  url: 'https://www.awesomeexample.com', // optional parameter
};

shareTextViaNativeSharing(data); // This will open up WhatsApp and you will be shown a list of contacts you can send your message to.

If the Web Share API is not supported by the users's browser, you can specify a fallback function as the second function argument.

import { shareTextViaNativeSharing } from 'share-text-to-whatsapp';

const data = {
  message: 'Check out this website', // required
  title: 'Awesome Website', // optional parameter
  url: 'https://www.awesomeexample.com', // optional parameter
};

shareTextViaNativeSharing(data, () => {
  // fallback function if native sharing is not supported
  // for example, send the message via the Click to Chat feature instead
  shareTextToWhatsApp(data.message);
});

hasNativeSharingSupport()

Returns true if the user's browser supports Web Share API for invoking the device native sharing mechanism.

import { hasNativeSharingSupport } from 'share-text-to-whatsapp';

hasNativeSharingSupport(); // returns true or false

As of August 2019, the Web Share API is supported by the following browsers. See caniuse.com for a more updated information.

Web Share API Browser Support

getWhatsAppClickToChatLink(text)

Accepts a string message and returns a link with a pre-filled message that will automatically appear in the text field of a chat.

This returned link will have either of the following format:

  1. https://wa.me?text=urlencodedtext
  2. whatsapp://send?text=urlencodedtext

urlencodedtext is the URL-encoded pre-filled message.

When clicking on the link, the user will be shown a list of contacts to send the message to.

import { shareTextViaNativeSharing } from 'share-text-to-whatsapp';

const message = 'Hello world';
getWhatsAppClickToChatLink(message); // https://wa.me?text=hello%20there || whatsapp://send?text=hello%20there 

Library Limitations

Does it detect if WhatsApp is installed on the user's device?

No. If the user does not have WhatsApp installed and native sharing is used, the user will have the other apps to choose from when sending the message either the device messaging app, via email, etc.

If using the Click to Chat feature, the user will be redirected to the WhatsApp download page in order to use it.

License

MIT © Ana Liza Pandac