2020-03-25 13:53:00 -04:00
|
|
|
import './style.scss';
|
2020-03-25 16:47:00 -04:00
|
|
|
|
|
|
|
interface PublishPreset {
|
|
|
|
url: string,
|
|
|
|
title: string,
|
2020-03-25 17:36:13 -04:00
|
|
|
extra: {
|
|
|
|
media: string,
|
|
|
|
text: string,
|
|
|
|
via: string,
|
2020-03-25 16:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UrlBuilder = (data: PublishPreset) => string;
|
|
|
|
|
|
|
|
const NETWORKS: { [name: string]: UrlBuilder } = {
|
2020-03-25 18:06:55 -04:00
|
|
|
facebook: (d) => `https://www.facebook.com/sharer/sharer.php?u=${d.url}`,
|
2020-03-25 19:02:41 -04:00
|
|
|
messenger: (d) => `https://www.facebook.com/dialog/send?app_id=3619024578167617&link=${d.url}&redirect_uri=${d.url}`,
|
2020-03-26 17:45:42 -04:00
|
|
|
odnoklassniki: (d) => `https://connect.ok.ru/offer?url=${d.url}&title=${d.title}${d.extra.media ? `&imageUrl=${d.extra.media}` : ''}`,
|
2020-03-25 18:39:04 -04:00
|
|
|
pinterest: (d) => `https://pinterest.com/pin/create/button/?url=${d.url}&description=${d.title}${d.extra.media ? `&media=${d.extra.media}` : ''}`,
|
2020-03-25 17:36:13 -04:00
|
|
|
telegram: (d) => `https://telegram.me/share/url?url=${d.url}${d.extra.text ? `&text=${d.extra.text}` : ''}`,
|
|
|
|
twitter: (d) => `https://twitter.com/intent/tweet?url=${d.url}&text=${d.title}${d.extra.via ? `&via=${d.extra.via}` : ''}`,
|
2020-03-26 17:00:19 -04:00
|
|
|
vkontakte: (d) => `https://vk.com/share.php?url=${d.url}&title=${d.title}${d.extra.media ? `&image=${d.extra.media}` : ''}`,
|
2020-03-25 17:45:47 -04:00
|
|
|
whatsapp: (d) => `whatsapp://send?text=${d.title}%0D%0A${d.url}${d.extra.text ? `%0D%0A%0D%0A${d.extra.text}` : ''}`,
|
2020-03-25 16:47:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function initShareonChild(child: HTMLElement, preset: PublishPreset) {
|
|
|
|
if (child) {
|
|
|
|
child.classList.forEach((cls) => {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(NETWORKS, cls)) {
|
|
|
|
const url = NETWORKS[cls](preset);
|
|
|
|
if (child.tagName.toLowerCase() === 'a') {
|
|
|
|
child.setAttribute('href', url);
|
|
|
|
child.setAttribute('rel', 'noopener noreferrer');
|
|
|
|
child.setAttribute('target', '_blank');
|
|
|
|
} else {
|
|
|
|
child.addEventListener('click', () => { window.open(url, '_blank', 'noopener,noreferrer').opener = null; });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onload = () => {
|
|
|
|
const shareonContainers = document.getElementsByClassName('shareon');
|
|
|
|
|
|
|
|
for (let i = 0; i < shareonContainers.length; i += 1) {
|
|
|
|
const container = shareonContainers[i] as HTMLElement;
|
|
|
|
|
|
|
|
for (let j = 0; j < container.children.length; j += 1) {
|
|
|
|
const child = container.children[j] as HTMLElement;
|
|
|
|
|
|
|
|
const preset: PublishPreset = {
|
2020-03-25 17:36:13 -04:00
|
|
|
url: encodeURIComponent(child.dataset.url || container.dataset.url || window.location.href),
|
|
|
|
title: encodeURIComponent(child.dataset.title || container.dataset.title || document.title || ''),
|
2020-03-25 16:47:00 -04:00
|
|
|
extra: {
|
2020-03-25 17:36:13 -04:00
|
|
|
media: encodeURIComponent(child.dataset.media || container.dataset.media || ''),
|
|
|
|
text: encodeURIComponent(child.dataset.text || container.dataset.text || ''),
|
|
|
|
via: encodeURIComponent(child.dataset.via || container.dataset.via || ''),
|
2020-03-25 16:47:00 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
initShareonChild(
|
|
|
|
child as HTMLElement,
|
|
|
|
preset,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|