This repository has been archived by the owner on May 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
needsharebutton.js
522 lines (473 loc) · 18.9 KB
/
needsharebutton.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/***********************************************
needShareButton
- Version 1.0.0
- Copyright 2015 Dzmitry Vasileuski
- Licensed under MIT (http://opensource.org/licenses/MIT)
***********************************************/
(function () {
// find closest
function closest(elem, parent) {
if (typeof (parent) == "string") {
var matchesSelector = elem.matches || elem.webkitMatchesSelector ||
elem.mozMatchesSelector || elem.msMatchesSelector;
if (!!matchesSelector) {
while (elem) {
if (matchesSelector.bind(elem)(parent)) {
return elem;
} else {
elem = elem.parentElement;
}
}
}
return false;
} else {
while (elem) {
if (elem == parent) {
return elem;
} else {
elem = elem.parentElement;
}
}
return false;
}
}
// share button class
window.needShareButton = function (elem, options) {
// create element reference
var root = this;
root.elem = elem || "need-share-button";
/* Helpers
***********************************************/
// get title from html
root.getTitle = function () {
var content;
// check querySelector existance for old browsers
if (document.querySelector) {
content = document.querySelector("title");
if (content) {
return content.innerText;
}
}
return document.title;
};
// get image from html
root.getImage = function () {
var content;
// check querySelector existance for old browsers
if (document.querySelector) {
content = document.querySelector("meta[property=\"og:image\"]") ||
document.querySelector("meta[name=\"twitter:image\"]");
if (content) {
return content.getAttribute("content");
} else {
return "";
}
} else {
return "";
}
};
// get description from html
root.getDescription = function () {
var content;
// check querySelector existance for old browsers
if (document.querySelector) {
content = document.querySelector("meta[property=\"og:description\"]") ||
document.querySelector("meta[name=\"twitter:description\"]") ||
document.querySelector("meta[name=\"description\"]");
if (content) {
return content.getAttribute("content");
} else {
return "";
}
} else {
content = document.getElementsByTagName("meta").namedItem("description");
if (content) {
return content.getAttribute("content");
} else {
return "";
}
}
};
// share urls for all networks
root.share = {
"weibo": function (el) {
var myoptions = getOptions(el);
var url = "http://v.t.sina.com.cn/share/share.php?title=" +
encodeURIComponent(myoptions.title) +
"&url=" + encodeURIComponent(myoptions.url) +
"&pic=" + encodeURIComponent(myoptions.image);
root.popup(url);
},
"wechat": function (el) {
var myoptions = getOptions(el);
var imgSrc = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" + encodeURIComponent(myoptions.url);
var dropdownEl = el.querySelector(".need-share-button_dropdown");
var img = dropdownEl.getElementsByClassName("need-share-wechat-code-image")[0];
if (img) {
img.remove();
} else {
img = document.createElement("img");
img.src = imgSrc;
img.alt = "loading wechat image...";
img.setAttribute("class", "need-share-wechat-code-image");
if (root.options.position == 'middleRight') {
dropdownEl.insertBefore(img, dropdownEl.children[0]);
}
else {
dropdownEl.appendChild(img);
}
}
},
"douban": function (el) {
var myoptions = getOptions(el);
var url = "https://www.douban.com/share/service?name=" +
encodeURIComponent(myoptions.title) +
"&href=" + encodeURIComponent(myoptions.url) +
"&image=" + encodeURIComponent(myoptions.image);
root.popup(url);
},
"qqzone": function (el) {
var myoptions = getOptions(el);
var url = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?title=" +
encodeURIComponent(myoptions.title) +
"&url=" + encodeURIComponent(myoptions.url) +
"&pics=" + encodeURIComponent(myoptions.image) +
"&desc=" + encodeURIComponent(myoptions.description);
root.popup(url);
},
"renren": function (el) {
var myoptions = getOptions(el);
var url = "http://widget.renren.com/dialog/share?title=" +
encodeURIComponent(myoptions.title) +
"&resourceUrl=" + encodeURIComponent(myoptions.url) +
"&pic=" + encodeURIComponent(myoptions.image) +
"&description=" + encodeURIComponent(myoptions.description);
root.popup(url);
},
"mailto": function (el) {
var myoptions = getOptions(el);
var url = "mailto:?subject=" + encodeURIComponent(myoptions.title) +
"&body=Thought you might enjoy reading this: " + encodeURIComponent(myoptions.url) +
" - " + encodeURIComponent(myoptions.description);
window.location.href = url;
},
"twitter": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "twitter.com/intent/tweet?text=";
url += encodeURIComponent(myoptions.title) + "&url=" + encodeURIComponent(myoptions.url);
root.popup(url);
},
"pinterest": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "pinterest.com/pin/create/bookmarklet/?is_video=false";
url += "&media=" + encodeURIComponent(myoptions.image);
url += "&url=" + encodeURIComponent(myoptions.url);
url += "&description=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"facebook": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.facebook.com/share.php?";
url += "u=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"googleplus": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "plus.google.com/share?";
url += "url=" + encodeURIComponent(myoptions.url);
root.popup(url);
},
"reddit": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.reddit.com/submit?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"delicious": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "del.icio.us/post?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
url += "¬es=" + encodeURIComponent(myoptions.description);
root.popup(url);
},
"stumbleupon": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.stumbleupon.com/submit?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"linkedin": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.linkedin.com/shareArticle?mini=true";
url += "&url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
url += "&source=" + encodeURIComponent(myoptions.source);
root.popup(url);
},
"tumblr": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.tumblr.com/share?v=3";
url += "&u=" + encodeURIComponent(myoptions.url);
url += "&t=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"googlebookmarks": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.google.com/bookmarks/mark?op=edit";
url += "&bkmk=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
url += "&annotation=" + encodeURIComponent(myoptions.description);
root.popup(url);
},
"newsvine": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.newsvine.com/_tools/seed&save?";
url += "u=" + encodeURIComponent(myoptions.url);
url += "&h=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"evernote": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.evernote.com/clip.action?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"friendfeed": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.friendfeed.com/share?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
root.popup(url);
},
"vkontakte": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "vkontakte.ru/share.php?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
url += "&description=" + encodeURIComponent(myoptions.description);
url += "&image=" + encodeURIComponent(myoptions.image);
url += "&noparse=true";
root.popup(url);
},
"odnoklassniki": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "www.odnoklassniki.ru/dk?st.cmd=addShare&st.s=1";
url += "&st.comments=" + encodeURIComponent(myoptions.description);
url += "&st._surl=" + encodeURIComponent(myoptions.url);
root.popup(url);
},
"mailru": function (el) {
var myoptions = getOptions(el);
var url = myoptions.protocol + "connect.mail.ru/share?";
url += "url=" + encodeURIComponent(myoptions.url);
url += "&title=" + encodeURIComponent(myoptions.title);
url += "&description=" + encodeURIComponent(myoptions.description);
url += "&imageurl=" + encodeURIComponent(myoptions.image);
root.popup(url);
}
};
// open share link in a popup
root.popup = function (url) {
// set left and top position
var popupWidth = 600,
popupHeight = 500,
// fix dual screen mode
dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left,
dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top,
width = window.innerWidth ?
window.innerWidth :
document.documentElement.clientWidth ?
document.documentElement.clientWidth :
screen.width,
height = window.innerHeight ?
window.innerHeight :
document.documentElement.clientHeight ?
document.documentElement.clientHeight :
screen.height,
// calculate top and left position
left = ((width / 2) - (popupWidth / 2)) + dualScreenLeft,
top = ((height / 2) - (popupHeight / 2)) + dualScreenTop,
// show popup
shareWindow = window.open(url, "targetWindow",
"toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=" + popupWidth +
", height=" + popupHeight + ", top=" + top + ", left=" + left);
// Puts focus on the newWindow
if (window.focus) {
shareWindow.focus();
}
};
/* Set options
***********************************************/
// create default options
root.options = {
iconStyle: "default", // default or box
boxForm: "horizontal", // horizontal or vertical
position: "bottomCenter", // top / middle / bottom + Left / Center / Right
protocol: ["http", "https"].indexOf(window.location.href.split(":")[0]) === -1 ? "https://" : "//",
networks: "Weibo,Wechat,Douban,QQZone,Twitter,Pinterest,Facebook,GooglePlus,Reddit,Linkedin,Tumblr,Evernote"
};
// integrate custom options
for (var i in options) {
if (options.hasOwnProperty(i)) {
root.options[i] = options[i];
}
}
// convert networks string into array
//root.options.networks = root.options.networks.toLowerCase().split(",");
root.options.networks = root.options.networks.split(",");
function getOptions(el) {
// integrate data attribute options
var ret = {};
for (var i in root.options) {
if (root.options.hasOwnProperty(i)) {
ret[i] = root.options[i];
}
}
// these attrs must get dynamically.
ret.url = window.location.href;
ret.title = root.getTitle();
ret.image = root.getImage();
ret.description = root.getDescription();
for (var option in el.dataset) {
// replace only 'share-' prefixed data-attributes
if (option.match(/share/)) {
var newOption = option.replace(/share/, "");
if (!newOption.length) {
continue;
}
newOption = newOption.charAt(0).toLowerCase() + newOption.slice(1);
var val = el.dataset[option];
if (newOption === "networks") {
//val = val.toLowerCase().split(",");
val = val.split(",");
} else if (newOption === "url" && val && val[0] === "/") {
// fix relative url problem.
val = location.origin + val;
}
ret[newOption] = val;
}
}
return ret;
}
function createDropdown(el) {
// create dropdown
var dropdownEl = document.createElement("span");
dropdownEl.className = "need-share-button_dropdown";
if (el.querySelector(".need-share-button_dropdown")) {
return;
}
var myoptions = getOptions(el);
// set dropdown row length
if (myoptions.iconStyle == "default" && myoptions.boxForm == "vertical") {
dropdownEl.className += " need-share-button_dropdown-box-vertical";
} else if (myoptions.iconStyle == "box" && myoptions.boxForm == "horizontal") {
dropdownEl.className += " need-share-button_dropdown-box-horizontal";
} else if (myoptions.iconStyle == "box" && myoptions.boxForm == "vertical") {
dropdownEl.className += " need-share-button_dropdown-box-vertical";
}
// set dropdown position
setTimeout(function () {
switch (myoptions.position) {
case "topLeft":
dropdownEl.className += " need-share-button_dropdown-top-left";
break;
case "topRight":
dropdownEl.className += " need-share-button_dropdown-top-right";
break;
case "topCenter":
dropdownEl.className += " need-share-button_dropdown-top-center";
dropdownEl.style.marginLeft = -dropdownEl.offsetWidth / 2 + "px";
break;
case "middleLeft":
dropdownEl.className += " need-share-button_dropdown-middle-left";
dropdownEl.style.marginTop = -dropdownEl.offsetHeight / 2 + "px";
break;
case "middleRight":
dropdownEl.className += " need-share-button_dropdown-middle-right";
dropdownEl.style.marginTop = -dropdownEl.offsetHeight / 2 + "px";
break;
case "bottomLeft":
dropdownEl.className += " need-share-button_dropdown-bottom-left";
break;
case "bottomRight":
dropdownEl.className += " need-share-button_dropdown-bottom-right";
break;
case "bottomCenter":
dropdownEl.className += " need-share-button_dropdown-bottom-center";
dropdownEl.style.marginLeft = -dropdownEl.offsetWidth / 2 + "px";
break;
default:
dropdownEl.className += " need-share-button_dropdown-bottom-center";
dropdownEl.style.marginLeft = -dropdownEl.offsetWidth / 2 + "px";
break;
}
}, 1);
// fill fropdown with buttons
var iconClass = myoptions.iconStyle == "default" ?
"need-share-button_link need-share-button_" :
"need-share-button_link-" + myoptions.iconStyle + " need-share-button_link need-share-button_";
for (var network in myoptions.networks) {
if (myoptions.networks.hasOwnProperty(network)) {
var link = document.createElement("span");
network = myoptions.networks[network].trim();
var networkLC = network.toLowerCase();
link.className = iconClass + networkLC;
var fontello = ["weibo", "wechat", "douban", "qqzone", "renren"];
if (fontello.indexOf(networkLC) === -1) {
link.className += " social-" + networkLC;
} else {
link.className += " icon-" + networkLC;
}
link.dataset.network = networkLC;
link.title = network;
dropdownEl.appendChild(link);
}
}
dropdownEl.addEventListener("click", function (event) {
if (closest(event.target, ".need-share-button_link")) {
event.preventDefault();
event.stopPropagation();
root.share[event.target.dataset.network](el);
return false;
}
});
el.appendChild(dropdownEl);
}
// close on click outside
document.addEventListener("click", function (event) {
var openedEl = document.querySelector(".need-share-button-opened");
if (!closest(event.target, ".need-share-button-opened")) {
if (openedEl) {
openedEl.classList.remove("need-share-button-opened");
// hide wechat code image when close the dropdown.
if (openedEl.querySelector(".need-share-wechat-code-image")) {
openedEl.querySelector(".need-share-wechat-code-image").remove();
}
} else {
var el = closest(event.target, root.elem);
if (el) {
if (!el.classList.contains("need-share-button-opened")) {
createDropdown(el);
setTimeout(function () {
el.classList.add("need-share-button-opened");
}, 1);
}
}
}
} else {
setTimeout(function () {
openedEl.classList.remove("need-share-button-opened");
// hide wechat code image when close the dropdown.
if (openedEl.querySelector(".need-share-wechat-code-image")) {
openedEl.querySelector(".need-share-wechat-code-image").remove();
}
}, 1);
}
});
};
})();