shareon/src/shareon.js

141 lines
5.6 KiB
JavaScript
Raw Normal View History

2023-01-25 12:27:00 -05:00
import "./shareon.css";
2022-02-10 15:16:53 -05:00
// prettier-ignore
/**
* Map of social networks to their respective URL builders.
*
* The `d` argument of each builder is the object with the page metadata, such
* as page title, URL, author name, etc.
*
* @type {{ [network: string]: (d: {
* url: string,
* title?: string,
* media?: string,
* text?: string,
* via?: string,
* fbAppId?: string
2022-02-10 15:16:53 -05:00
* }) => string}}
*/
const urlBuilderMap = {
facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}${d.hashtags? `&hashtag=%23${d.hashtags.split('%2C')[0]}` : ''}`,
2022-02-10 15:16:53 -05:00
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}`,
odnoklassniki: (d) => `https://connect.ok.ru/offer?url=${d.url}&title=${d.title}${d.media ? `&imageUrl=${d.media}` : ''}`,
pinterest: (d) => `https://pinterest.com/pin/create/button/?url=${d.url}&description=${d.title}${d.media ? `&media=${d.media}` : ''}`,
pocket: (d) => `https://getpocket.com/edit.php?url=${d.url}`,
reddit: (d) => `https://www.reddit.com/submit?title=${d.title}&url=${d.url}`,
teams: (d) => `https://teams.microsoft.com/share?href=${d.url}${d.text ? `&msgText=${d.text}` : ''}`,
2022-02-10 15:16:53 -05:00
telegram: (d) => `https://telegram.me/share/url?url=${d.url}${d.text ? `&text=${d.text}` : ''}`,
tumblr: (d) => `https://www.tumblr.com/widgets/share/tool?posttype=${d.url}&tags=${d.hashtags}${d.title? `&title=${d.title}`:''}&content=${d.content}${d.text? `&caption=${d.text}`:''}${d.via? `&show-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}` : ''}`,
2022-02-10 15:16:53 -05:00
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}` : ''}`
2022-02-10 15:16:53 -05:00
};
2020-03-25 16:47:00 -04:00
2022-02-10 16:19:03 -05:00
const openUrl = (buttonUrl) => () => {
window.open(buttonUrl, "_blank", "noopener,noreferrer");
};
2022-02-10 17:02:50 -05:00
const init = () => {
const shareonContainers = document.querySelectorAll(".shareon");
2020-03-25 16:47:00 -04:00
// iterate over <div class="shareon">
2022-02-10 16:34:16 -05:00
for (const container of shareonContainers) {
// iterate over children of <div class="shareon">
2022-02-10 16:34:16 -05:00
for (const child of container.children) {
if (child) {
const classListLength = child.classList.length;
2020-03-25 16:47:00 -04:00
// iterate over classes of the child element
for (let k = 0; k < classListLength; k += 1) {
const cls = child.classList.item(k);
2023-01-22 23:49:37 -05:00
// if it's "Copy URL"
if (cls === "copy-url") {
child.addEventListener("click", () => {
const url =
child.dataset.url ||
container.dataset.url ||
window.location.href;
navigator.clipboard.writeText(url);
2023-03-20 14:04:27 -04:00
child.classList.add("done");
setTimeout(() => {
child.classList.remove("done");
}, 1000);
2023-01-22 23:49:37 -05:00
});
}
2023-07-14 17:40:00 -04:00
// if it's "Web Share"
if (cls === "web-share") {
const data = {
title:
child.dataset.title ||
container.dataset.title ||
document.title,
text: child.dataset.text || container.dataset.text || "",
url:
child.dataset.url ||
container.dataset.url ||
window.location.href,
};
if (navigator.canShare && navigator.canShare(data)) {
child.addEventListener("click", () => {
navigator.share(data);
});
} else {
child.style.display = "none";
}
}
// if it's one of the networks
2020-07-31 12:16:59 -04:00
if (Object.prototype.hasOwnProperty.call(urlBuilderMap, cls)) {
const preset = {
url: encodeURIComponent(
2022-02-08 12:56:39 -05:00
child.dataset.url ||
container.dataset.url ||
2023-07-14 16:27:58 -04:00
window.location.href,
),
title: encodeURIComponent(
2023-07-14 16:27:58 -04:00
child.dataset.title ||
container.dataset.title ||
document.title,
),
media: encodeURIComponent(
2023-07-14 16:27:58 -04:00
child.dataset.media || container.dataset.media || "",
),
text: encodeURIComponent(
2023-07-14 16:27:58 -04:00
child.dataset.text || container.dataset.text || "",
),
via: encodeURIComponent(
2023-07-14 16:27:58 -04:00
child.dataset.via || container.dataset.via || "",
),
hashtags: encodeURIComponent(
2023-07-14 16:27:58 -04:00
child.dataset.hashtags || container.dataset.hashtags || "",
),
fbAppId: encodeURIComponent(
2023-07-14 16:27:58 -04:00
child.dataset.fbAppId || container.dataset.fbAppId || "",
),
};
2020-07-31 12:16:59 -04:00
const url = urlBuilderMap[cls](preset);
2022-02-08 12:56:39 -05:00
if (child.tagName.toLowerCase() === "a") {
child.setAttribute("href", url);
child.setAttribute("rel", "noopener noreferrer");
child.setAttribute("target", "_blank");
} else {
2022-02-10 16:19:03 -05:00
child.addEventListener("click", openUrl(url));
}
break; // once a network is detected we don't want to check further
}
}
}
2020-03-25 16:47:00 -04:00
}
}
};
2022-02-10 17:02:50 -05:00
export { init };