Encode strings as URI before passing to method

This should decrease code size
pull/1/head
Nikita Karamov 2020-03-25 22:36:13 +01:00
parent 135e8750d4
commit b3746de13a
No known key found for this signature in database
GPG Key ID: E40DFE6E993540FF
1 changed files with 11 additions and 11 deletions

View File

@ -3,18 +3,18 @@ import './style.scss';
interface PublishPreset { interface PublishPreset {
url: string, url: string,
title: string, title: string,
extra?: { extra: {
media?: null|string, media: string,
text?: null|string, text: string,
via?: null|string, via: string,
} }
} }
type UrlBuilder = (data: PublishPreset) => string; type UrlBuilder = (data: PublishPreset) => string;
const NETWORKS: { [name: string]: UrlBuilder } = { const NETWORKS: { [name: string]: UrlBuilder } = {
telegram: (d) => `https://telegram.me/share/url?url=${encodeURIComponent(d.url)}${(d.extra && d.extra.text) ? `&text=${encodeURIComponent(d.extra.text)}` : ''}`, 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=${encodeURIComponent(d.url)}&text=${encodeURIComponent(d.title)}${(d.extra && d.extra.via) ? `&via=${encodeURIComponent(d.extra.via)}` : ''}`, twitter: (d) => `https://twitter.com/intent/tweet?url=${d.url}&text=${d.title}${d.extra.via ? `&via=${d.extra.via}` : ''}`,
}; };
function initShareonChild(child: HTMLElement, preset: PublishPreset) { function initShareonChild(child: HTMLElement, preset: PublishPreset) {
@ -44,12 +44,12 @@ window.onload = () => {
const child = container.children[j] as HTMLElement; const child = container.children[j] as HTMLElement;
const preset: PublishPreset = { const preset: PublishPreset = {
url: child.dataset.url || container.dataset.url || window.location.href, url: encodeURIComponent(child.dataset.url || container.dataset.url || window.location.href),
title: child.dataset.title || container.dataset.title || document.title || '', title: encodeURIComponent(child.dataset.title || container.dataset.title || document.title || ''),
extra: { extra: {
media: child.dataset.media || container.dataset.media || null, media: encodeURIComponent(child.dataset.media || container.dataset.media || ''),
text: child.dataset.text || container.dataset.text || null, text: encodeURIComponent(child.dataset.text || container.dataset.text || ''),
via: child.dataset.via || container.dataset.via || null, via: encodeURIComponent(child.dataset.via || container.dataset.via || ''),
}, },
}; };