-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare.js
72 lines (61 loc) · 1.91 KB
/
share.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
var AddThisJSLoaded = false; //AddThis not loaded yet
var clickedOnShare = false; //share button is not clicked
//replace with your publisher ID
//it's in your profile setting
var AddThisPubID = "ra-5f22e9f6180d8ff4";
var AddThisJS =
"//s7.addthis.com/js/300/addthis_widget.js#pubid=" + AddThisPubID;
function share(button) {
if (navigator.share) {
//check if Web Share API is supported by the browser
var url = document.location.href;
var title = document.getElementsByTagName("title")[0].innerHTML;
var description = document
.querySelector("meta[name=description]")
.getAttribute("content");
navigator.share({
title: title,
text: description,
url: url
});
} else {
if (!AddThisJSLoaded && !clickedOnShare) {
//when AddThis not loaded and share button isn't clicked
clickedOnShare = true; //share button is clicked
showLoading(button);
shareByAddThis(button);
} else {
toggleAddThisButtons(button);
}
}
}
function shareByAddThis(button) {
var script = document.createElement("script");
script.async = true;
script.src = AddThisJS;
script.onload = function() {
clickedOnShare = false; //AddThis JS is loaded
addthis.user.ready(function(data) {
AddThisJSLoaded = true; //AddThis loaded and ready to use
hideLoading(button);
showAddThisButtons(button);
});
};
script.onerror = function() {
clickedOnShare = false; //AddThis JS failed to load
hideLoading(button);
};
document.body.appendChild(script);
}
function showLoading(button) {
button.classList.add("loading");
}
function hideLoading(button) {
button.classList.remove("loading");
}
function showAddThisButtons(button) {
button.classList.add("showAddThisButtons");
}
function toggleAddThisButtons(button) {
button.classList.toggle("showAddThisButtons");
}