Encode strings as URI before passing to method
This should decrease code sizepull/1/head
parent
135e8750d4
commit
b3746de13a
22
src/index.ts
22
src/index.ts
|
@ -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 || ''),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue