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 = {
|
|
|
|
facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}`,
|
|
|
|
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}` : ''}`,
|
|
|
|
twitter: (d) => `https://twitter.com/intent/tweet?url=${d.url}&text=${d.title}${d.via ? `&via=${d.via}` : ''}`,
|
|
|
|
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");
|
|
|
|
};
|
|
|
|
|
2020-07-31 12:18:17 -04:00
|
|
|
const initializeShareon = () => {
|
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:20:06 -05:00
|
|
|
for (
|
|
|
|
let containerIndex = 0;
|
|
|
|
containerIndex < shareonContainers.length;
|
|
|
|
containerIndex += 1
|
|
|
|
) {
|
2020-07-31 12:18:17 -04:00
|
|
|
/** @type Element */
|
2022-02-10 16:20:06 -05:00
|
|
|
const container = shareonContainers[containerIndex];
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
// iterate over children of <div class="shareon">
|
2022-02-10 16:20:06 -05:00
|
|
|
for (
|
|
|
|
let childIndex = 0;
|
|
|
|
childIndex < container.children.length;
|
|
|
|
childIndex += 1
|
|
|
|
) {
|
2020-07-31 12:18:17 -04:00
|
|
|
/** @type Element */
|
2022-02-10 16:20:06 -05:00
|
|
|
const child = container.children[childIndex];
|
2020-03-25 16:47:00 -04:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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
|
|
|
),
|
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
|
|
|
|
|
|
|
export default initializeShareon;
|