From c00e449749839a80b230d7dacbc3bb33385a8a23 Mon Sep 17 00:00:00 2001 From: Edward Cui Date: Tue, 4 Jul 2023 21:52:20 +0800 Subject: [PATCH] Add hashtag to facebook and hastags to twitter Hashtag and hastags are the most commonly used features on facebook and twitter respectively. Adding support to those two will make the library even more powerful. This PR add two optional attribute `data-hashtag` for facebook and `data-hashtags` for twitter. `data-hashtag` is a single key word without `#` while `data-hashtags` are comma seperated strings without `#`. --- src/shareon.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/shareon.js b/src/shareon.js index aede88e..e514870 100644 --- a/src/shareon.js +++ b/src/shareon.js @@ -17,7 +17,7 @@ import "./shareon.css"; * }) => string}} */ const urlBuilderMap = { - facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}`, + facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}${d.hashtag? `&hashtag=#${d.hashtag}` : ''}`, linkedin: (d) => `https://www.linkedin.com/sharing/share-offsite/?url=${d.url}`, mastodon: (d) => `https://toot.kytta.dev/?text=${d.title}%0D%0A${d.url}${d.text ? `%0D%0A%0D%0A${d.text}` : ''}${d.via ? `%0D%0A%0D%0A${d.via}` : ''}`, messenger: (d) => `https://www.facebook.com/dialog/send?app_id=${d.fbAppId}&link=${d.url}&redirect_uri=${d.url}`, @@ -26,7 +26,7 @@ const urlBuilderMap = { pocket: (d) => `https://getpocket.com/edit.php?url=${d.url}`, reddit: (d) => `https://www.reddit.com/submit?title=${d.title}&url=${d.url}`, telegram: (d) => `https://telegram.me/share/url?url=${d.url}${d.text ? `&text=${d.text}` : ''}`, - twitter: (d) => `https://twitter.com/intent/tweet?url=${d.url}&text=${d.title}${d.via ? `&via=${d.via}` : ''}`, + twitter: (d) => `https://twitter.com/intent/tweet?url=${d.url}&text=${d.title}${d.via ? `&via=${d.via}` : ''}${d.hashtags? `&hashtags=${d.hashtags}` : ''}`, viber: (d) => `viber://forward?text=${d.title}%0D%0A${d.url}${d.text ? `%0D%0A%0D%0A${d.text}` : ''}`, vkontakte: (d) => `https://vk.com/share.php?url=${d.url}&title=${d.title}${d.media ? `&image=${d.media}` : ''}`, whatsapp: (d) => `https://wa.me/?text=${d.title}%0D%0A${d.url}${d.text ? `%0D%0A%0D%0A${d.text}` : ''}`, @@ -85,6 +85,12 @@ const init = () => { via: encodeURIComponent( child.dataset.via || container.dataset.via || "" ), + hashtag: encodeURIComponent( + child.dataset.hashtag || container.dataset.hashtag || "" + ), + hashtags: encodeURIComponent( + child.dataset.hashtags?.replaceAll(', ', ',') || container.dataset.hashtags?.replaceAll(', ', ',') || "" + ), fbAppId: encodeURIComponent( child.dataset.fbAppId || container.dataset.fbAppId || "" ),