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
|
|
|
|
* }) => string}}
|
|
|
|
*/
|
|
|
|
const urlBuilderMap = {
|
2023-07-11 10:24:02 -04: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 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}`,
|
|
|
|
telegram: (d) => `https://telegram.me/share/url?url=${d.url}${d.text ? `&text=${d.text}` : ''}`,
|
2023-07-04 09:52:20 -04: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 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}` : ''}`,
|
|
|
|
};
|
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 = () => {
|
2022-02-10 16:19:54 -05:00
|
|
|
const shareonContainers = document.querySelectorAll(".shareon");
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
// iterate over <div class="shareon">
|
2022-02-10 16:34:16 -05:00
|
|
|
for (const container of shareonContainers) {
|
2020-07-30 06:40:00 -04:00
|
|
|
// iterate over children of <div class="shareon">
|
2022-02-10 16:34:16 -05:00
|
|
|
for (const child of container.children) {
|
2020-07-30 06:40:00 -04:00
|
|
|
if (child) {
|
|
|
|
const classListLength = child.classList.length;
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40: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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
// if it's one of the networks
|
2020-07-31 12:16:59 -04:00
|
|
|
if (Object.prototype.hasOwnProperty.call(urlBuilderMap, cls)) {
|
2020-07-30 06:40:00 -04:00
|
|
|
const preset = {
|
|
|
|
url: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.url ||
|
|
|
|
container.dataset.url ||
|
|
|
|
window.location.href
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
title: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.title || container.dataset.title || document.title
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
media: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.media || container.dataset.media || ""
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
text: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.text || container.dataset.text || ""
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
via: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.via || container.dataset.via || ""
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
2023-07-04 09:52:20 -04:00
|
|
|
hashtags: encodeURIComponent(
|
2023-07-06 08:50:57 -04:00
|
|
|
child.dataset.hashtags || container.dataset.hashtags || ""
|
2023-07-04 09:52:20 -04:00
|
|
|
),
|
2020-09-28 07:33:36 -04:00
|
|
|
fbAppId: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.fbAppId || container.dataset.fbAppId || ""
|
2020-09-28 07:33:36 -04:00
|
|
|
),
|
2020-07-30 06:40:00 -04:00
|
|
|
};
|
2020-07-31 12:16:59 -04:00
|
|
|
const url = urlBuilderMap[cls](preset);
|
2020-07-30 06:40:00 -04:00
|
|
|
|
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");
|
2020-07-30 06:40:00 -04:00
|
|
|
} else {
|
2022-02-10 16:19:03 -05:00
|
|
|
child.addEventListener("click", openUrl(url));
|
2020-07-30 06:40:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break; // once a network is detected we don't want to check further
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 16:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-14 11:47:27 -04:00
|
|
|
|
2022-02-10 17:02:50 -05:00
|
|
|
export { init };
|