2023-01-25 18:27:00 +01:00
|
|
|
import "./shareon.css";
|
|
|
|
|
2022-02-10 21:16:53 +01: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,
|
2023-07-27 15:16:24 +02:00
|
|
|
* fbAppId?: string,
|
|
|
|
* posttype?: string,
|
|
|
|
* tags?:[string],
|
|
|
|
* content?: string || [string]
|
2022-02-10 21:16:53 +01:00
|
|
|
* }) => string}}
|
|
|
|
*/
|
|
|
|
const urlBuilderMap = {
|
2023-07-11 16:24:02 +02:00
|
|
|
facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}${d.hashtags? `&hashtag=%23${d.hashtags.split('%2C')[0]}` : ''}`,
|
2022-02-10 21:16:53 +01: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}`,
|
2023-07-12 15:42:54 +02:00
|
|
|
teams: (d) => `https://teams.microsoft.com/share?href=${d.url}${d.text ? `&msgText=${d.text}` : ''}`,
|
2022-02-10 21:16:53 +01:00
|
|
|
telegram: (d) => `https://telegram.me/share/url?url=${d.url}${d.text ? `&text=${d.text}` : ''}`,
|
2023-07-04 15:52:20 +02:00
|
|
|
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 21:16:53 +01: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}` : ''}`,
|
2023-07-27 15:16:24 +02:00
|
|
|
tumblr: (d) => `https://www.tumblr.com/widgets/share/tool?posttype=${d.posttype}&tags=${d.tags}${d.title? `&title=${d.title}`:''}&content=${d.content}${d.caption? `&caption=${d.caption}`:''}${d.via? `&show-via=${d.via}`:''}`,
|
2022-02-10 21:16:53 +01:00
|
|
|
};
|
2020-03-25 21:47:00 +01:00
|
|
|
|
2022-02-10 22:19:03 +01:00
|
|
|
const openUrl = (buttonUrl) => () => {
|
|
|
|
window.open(buttonUrl, "_blank", "noopener,noreferrer");
|
|
|
|
};
|
|
|
|
|
2022-02-10 23:02:50 +01:00
|
|
|
const init = () => {
|
2022-02-10 22:19:54 +01:00
|
|
|
const shareonContainers = document.querySelectorAll(".shareon");
|
2020-03-25 21:47:00 +01:00
|
|
|
|
2020-07-30 12:40:00 +02:00
|
|
|
// iterate over <div class="shareon">
|
2022-02-10 22:34:16 +01:00
|
|
|
for (const container of shareonContainers) {
|
2020-07-30 12:40:00 +02:00
|
|
|
// iterate over children of <div class="shareon">
|
2022-02-10 22:34:16 +01:00
|
|
|
for (const child of container.children) {
|
2020-07-30 12:40:00 +02:00
|
|
|
if (child) {
|
|
|
|
const classListLength = child.classList.length;
|
2020-03-25 21:47:00 +01:00
|
|
|
|
2020-07-30 12:40:00 +02:00
|
|
|
// iterate over classes of the child element
|
|
|
|
for (let k = 0; k < classListLength; k += 1) {
|
|
|
|
const cls = child.classList.item(k);
|
|
|
|
|
2023-01-23 05:49:37 +01: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 19:04:27 +01:00
|
|
|
child.classList.add("done");
|
|
|
|
setTimeout(() => {
|
|
|
|
child.classList.remove("done");
|
|
|
|
}, 1000);
|
2023-01-23 05:49:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:40:00 +02: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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:40:00 +02:00
|
|
|
// if it's one of the networks
|
2020-07-31 18:16:59 +02:00
|
|
|
if (Object.prototype.hasOwnProperty.call(urlBuilderMap, cls)) {
|
2020-07-30 12:40:00 +02:00
|
|
|
const preset = {
|
|
|
|
url: encodeURIComponent(
|
|
|
|
child.dataset.url ||
|
2020-07-31 18:18:17 +02:00
|
|
|
container.dataset.url ||
|
2023-07-14 22:27:58 +02:00
|
|
|
window.location.href,
|
2020-07-30 12:40:00 +02:00
|
|
|
),
|
|
|
|
title: encodeURIComponent(
|
2023-07-14 22:27:58 +02:00
|
|
|
child.dataset.title ||
|
|
|
|
container.dataset.title ||
|
|
|
|
document.title,
|
2020-07-30 12:40:00 +02:00
|
|
|
),
|
|
|
|
media: encodeURIComponent(
|
2023-07-14 22:27:58 +02:00
|
|
|
child.dataset.media || container.dataset.media || "",
|
2020-07-30 12:40:00 +02:00
|
|
|
),
|
|
|
|
text: encodeURIComponent(
|
2023-07-14 22:27:58 +02:00
|
|
|
child.dataset.text || container.dataset.text || "",
|
2020-07-30 12:40:00 +02:00
|
|
|
),
|
|
|
|
via: encodeURIComponent(
|
2023-07-14 22:27:58 +02:00
|
|
|
child.dataset.via || container.dataset.via || "",
|
2020-07-30 12:40:00 +02:00
|
|
|
),
|
2023-07-04 15:52:20 +02:00
|
|
|
hashtags: encodeURIComponent(
|
2023-07-14 22:27:58 +02:00
|
|
|
child.dataset.hashtags || container.dataset.hashtags || "",
|
2023-07-04 15:52:20 +02:00
|
|
|
),
|
2020-09-28 13:33:36 +02:00
|
|
|
fbAppId: encodeURIComponent(
|
2023-07-14 22:27:58 +02:00
|
|
|
child.dataset.fbAppId || container.dataset.fbAppId || "",
|
2020-09-28 13:33:36 +02:00
|
|
|
),
|
2020-07-30 12:40:00 +02:00
|
|
|
};
|
2020-07-31 18:16:59 +02:00
|
|
|
const url = urlBuilderMap[cls](preset);
|
2020-07-30 12:40:00 +02:00
|
|
|
|
|
|
|
if (child.tagName.toLowerCase() === "a") {
|
|
|
|
child.setAttribute("href", url);
|
|
|
|
child.setAttribute("rel", "noopener noreferrer");
|
|
|
|
child.setAttribute("target", "_blank");
|
|
|
|
} else {
|
2022-02-10 22:19:03 +01:00
|
|
|
child.addEventListener("click", openUrl(url));
|
2020-07-30 12:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break; // once a network is detected we don't want to check further
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 21:47:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-14 17:47:27 +02:00
|
|
|
|
2022-02-10 23:02:50 +01:00
|
|
|
export { init };
|