-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·53 lines (43 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
*
* @param {string} type 'facebook' || 'twitter' || 'linkedin'
* @param {string} title
* @param {string} text
* @param {string} source
*/
const share = (type, title = "", text = "", source = null, url = null) =>
{
const typeExists = ['facebook', 'twitter', 'linkedin'].indexOf(type);
if(typeExists < 0) throw new Error('This type does not exist.');
if(!url) url = window.location.href;
url = encodeURIComponent(url);
text = encodeURIComponent(text);
title = encodeURIComponent(title);
if(!source) source = window.location.origin;
source = encodeURIComponent(source);
let finalURI;
let width, height;
switch(type)
{
case 'facebook':
width = 650;
height = 450;
finalURI = `http://www.facebook.com/sharer.php?u=${url}&t=${text}`;
break;
case 'twitter':
width = 650;
height = 256;
finalURI = `http://twitter.com/share?url=${url}&text=${text}`;
break;
case 'linkedin':
width = 650;
height = 450;
finalURI = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=${title}&summary=${text}&source=${source}`;
break;
}
const top = (window.screen.height * 0.5) - ((height * 0.5) + 50);
const left = (window.screen.width * 0.5) - ((width * 0.5) + 10);
const windowFeatures = `status=no,height=${height},width=${width},resizable=yes,left=${left},top=${top},screenX=${left},screenY=${top},toolbar=no,menubar=no,scrollbars=no,location=no,directories=no`;
window.open(finalURI, 'Sharer', windowFeatures);
}
export default share;