-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sharelinks.js
142 lines (124 loc) · 3.25 KB
/
Sharelinks.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Helper function to retrieve attribute value.
* @param {String} suffix
* @param {Element} element
* @param {String} default_val
*/
function dataAttr(suffix, element, default_val) {
if (typeof element.dataset[suffix] === 'undefined') {
return default_val;
}
return element.dataset[suffix];
}
function urlify(value) {
return encodeURIComponent(value).replaceAll(/%20/g, '+');
}
let platforms = [
{
name: 'whatsapp',
href: 'whatsapp://send?text=%URL%',
width: null,
height: null,
sameWindow: true
},
{
name: 'facebook',
href: 'https://www.facebook.com/sharer/sharer.php?u=%URL%',
width: 400,
height: 500
},
{
name: 'twitter',
href: 'https://twitter.com/intent/tweet?text=%TITLE%+-+%URL%',
width: 540,
height: 260
},
{
name: 'pinterest',
href:
'http://pinterest.com/pin/create/button/?url=%URL%&description=%TITLE%&media=%IMAGE%',
width: 520,
height: 570
},
{
name: 'tumblr',
href: 'http://www.tumblr.com/share/link?url=%URL%',
width: 500,
height: 500
},
{
name: 'linkedin',
href: 'https://www.linkedin.com/sharing/share-offsite/?url=%URL%',
width: 520,
height: 570
}
];
class Sharelinks {
constructor(selector, options = {}) {
platforms.push(...(options.platforms ?? []));
this.options = options;
// Listen for clicks
document.querySelectorAll(selector).forEach((link) => {
link.addEventListener('click', this.onClick.bind(this));
});
}
makeLink(platform, url, title, image) {
return platform.href
.replaceAll('%URL%', urlify(url))
.replaceAll('%TITLE%', urlify(title))
.replaceAll('%IMAGE%', urlify(image));
}
findImage(elem) {
if (dataAttr('image', elem)) {
return dataAttr('image', elem);
}
const ogImage = document.querySelector('meta[property="og:image"]');
if (ogImage) {
return ogImage.getAttribute('content');
}
}
onClick(e) {
// Left click only! Don't hijack middle click!
if (e.which !== 1) {
return;
}
e.preventDefault();
const elem = e.currentTarget;
const platform = platforms.find((platform) => {
return platform.name === dataAttr('platform', elem);
});
if (typeof platform === 'undefined') {
throw (
'Sharelinks Error: Invalid data-platform: ' + dataAttr('platform', elem)
);
}
const url = dataAttr('url', elem, window.location.href);
const width = dataAttr('width', elem, platform.width);
const height = dataAttr('height', elem, platform.height);
const href = this.makeLink(
platform,
url,
dataAttr('title', elem, document.title),
this.findImage(elem)
);
if (typeof this.options.callback === 'function') {
this.options.callback({
platform: elem.dataset.platform,
url: url
});
}
if (platform.sameWindow) {
window.location.href = href;
} else {
const new_window = window.open(
href,
'',
'status=yes, width=' + width + ', height=' + height
);
// Prevent referer bug.
// See https://www.jitbit.com/alexblog/256-targetblank---the-most-underestimated-vulnerability-ever/
new_window.opener = null;
}
}
}
export default Sharelinks;