Skip to content

Commit 24bfc5b

Browse files
committed
refactoring & get description
1 parent 24aae25 commit 24bfc5b

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

example/index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
<html>
33
<head>
44
<title>Test page for sharing</title>
5+
<meta name="description" content="Page description" />
56
<style>
67
/* Few styles :) */
78
.share-btn > a {
89
border: 1px solid #ccc;
910
padding: 5px;
11+
font-size: 12px;
12+
font-family: Verdana, Arial;
1013
}
1114
.share-btn > a:hover {
1215
cursor: pointer;
@@ -18,16 +21,20 @@
1821
<p>Share this page with:</p>
1922

2023
<!-- data-url="" data-title="" data-desc="" -->
21-
<div class="share-btn" >
24+
25+
26+
<div class="share-btn" data-url="https://github.com/wcoder/share-buttons">
2227
<a class="btn-vk" data-id="vk">VK</a>
2328
<a class="btn-facebook" data-id="fb">Facebook</a>
2429
<a class="btn-twitter" data-id="tw">Twitter</a>
25-
<a class="btn-google-plus" data-id="gp">Google+</a>
26-
<a class="btn-linkedin" data-id="in">LinkedIn</a>
2730
<a class="btn-mail" data-id="mail">EMail</a>
2831
</div>
2932

3033

34+
<!-- <a class="btn-google-plus" data-id="gp">Google+</a>
35+
<a class="btn-linkedin" data-id="in">LinkedIn</a> -->
36+
37+
3138
<script src="../src/share-buttons.js"></script>
3239
</body>
3340
</html>

src/share-buttons.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
var FB_LINK = 'https://www.facebook.com/sharer/sharer.php?u=';
55
var VK_LINK = 'https://vk.com/share.php?url={0}&description={1}. {2}';
66
var TW_LINK = 'https://twitter.com/intent/tweet?url=';
7-
var GP_LINK = 'https://plus.google.com/share?url=';
8-
var IN_LINK = 'https://www.linkedin.com/shareArticle?mini=true&url=';
7+
//var GP_LINK = 'https://plus.google.com/share?url=';
8+
//var IN_LINK = 'https://www.linkedin.com/shareArticle?mini=true&url=';
99

1010
// from http://evgeniy.pakalo.name/post/49
1111
var _F = function(str, args){
@@ -21,10 +21,11 @@
2121
this.init = function() {
2222
var _this = this,
2323
share = document.querySelectorAll('.share-btn');
24+
2425
for (var i = 0, l = share.length; i < l; i++) {
25-
var url = share[i].getAttribute('data-url') || location.href,
26-
title = share[i].getAttribute('data-title') || document.title,
27-
desc = share[i].getAttribute('data-desc') || ' ',
26+
var url = this.getUrl(share[i]),
27+
title = this.getTitle(share[i]),
28+
desc = this.getDesc(share[i]),
2829
el = share[i].querySelectorAll('a');
2930

3031
for (var a = 0, al = el.length; a < al; a++) {
@@ -41,6 +42,19 @@
4142
}
4243
};
4344

45+
this.getUrl = function (share) {
46+
return share.getAttribute('data-url') || location.href || ' ';
47+
};
48+
49+
this.getTitle = function (share) {
50+
return share.getAttribute('data-title') || document.title || ' ';
51+
};
52+
53+
this.getDesc = function (share) {
54+
var metaDesc = document.querySelector('meta[name=description]');
55+
return share.getAttribute('data-desc') || (metaDesc && metaDesc.getAttribute('content')) || ' ';
56+
};
57+
4458
this.addEventListener = function(el, eventName, opt) {
4559
var _this = this,
4660
handler = function () {
@@ -84,13 +98,13 @@
8498
this.popupCenter(TW_LINK + url + text, this.title, this.width, this.height);
8599
break;
86100

87-
case 'gp':
88-
this.popupCenter(GP_LINK + url, this.title, this.width, this.height);
89-
break;
101+
// case 'gp':
102+
// this.popupCenter(GP_LINK + url, this.title, this.width, this.height);
103+
// break;
90104

91-
case 'in':
92-
this.popupCenter(IN_LINK + url, this.title, this.width, this.height);
93-
break;
105+
// case 'in':
106+
// this.popupCenter(IN_LINK + url, this.title, this.width, this.height);
107+
// break;
94108

95109
case 'mail':
96110
var text = title || desc || '';
@@ -149,11 +163,8 @@
149163
};
150164

151165

152-
153-
// start
154-
var sb = new ShareButtons();
155-
sb.init();
156-
166+
// start
167+
new ShareButtons().init();
157168
}());
158169

159170

0 commit comments

Comments
 (0)