Skip to content

Commit

Permalink
Optmized jsonp
Browse files Browse the repository at this point in the history
  • Loading branch information
pinceladasdaweb committed May 18, 2016
1 parent c2fd5fc commit 77af80d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 38 deletions.
4 changes: 2 additions & 2 deletions build/socialight.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 6 additions & 36 deletions src/socialight.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,10 @@
xhttp.send(data);
xhttp = null;
},
jsonp: function (url, overwritten, callback, context) {
jsonp: function (url, callback, context) {
var name, head, script, extScript;

if (overwritten) {
name = this.randomString(30, "A");
} else {
name = 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000);
}
name = 'jsonp_' + Date.now() + '_' + Math.ceil(Math.random() * 100000);

head = document.head || document.getElementsByTagName('head')[0];
extScript = document.createElement('script');
Expand All @@ -106,32 +102,6 @@
delete this.name;
}.bind(this);
},
/**
* RANDOM STRING GENERATOR
*
* Info: http://stackoverflow.com/a/27872144/383904
* Use: randomString(length [,"A"] [,"N"] );
* Default: return a random alpha-numeric string
* Arguments: If you use the optional "A", "N" flags:
* "A" (Alpha flag) return random a-Z string
* "N" (Numeric flag) return random 0-9 string
*/
randomString: function(len, an) {
an = an && an.toLowerCase();

var str = "",
min = an === "a" ? 10 : 0,
max = an === "n" ? 10 : 62,
i = 0,
r;

for (;i ++< len;) {
r = Math.random() * (max - min) + min << 0;
str += String.fromCharCode(r += r > 9 ? r < 36 ? 55 : 61 : 48);
}

return str;
},
abbrNum: function (number, decPlaces) {
decPlaces = Math.pow(10, decPlaces);

Expand Down Expand Up @@ -233,7 +203,7 @@

match.setAttribute('href', 'https://www.facebook.com/sharer/sharer.php?u=' + attrs.url);

this.jsonp('https://graph.facebook.com/?id=' + attrs.url, false, function (data) {
this.jsonp('https://graph.facebook.com/?id=' + attrs.url, function (data) {
match.textContent = this.abbrNum(data.shares, 1);
}.bind(this));
}.bind(this));
Expand Down Expand Up @@ -285,7 +255,7 @@

match.setAttribute('href', 'https://www.linkedin.com/shareArticle?mini=true&url=' + attrs.url + '&title=' + attrs.title + '&summary=' + attrs.text + '&source=' + attrs.image);

this.jsonp('https://www.linkedin.com/countserv/count/share?url=' + attrs.url, true, function (data) {
this.jsonp('https://www.linkedin.com/countserv/count/share?url=' + attrs.url, function (data) {
match.textContent = this.abbrNum(data.count, 1);
}.bind(this));
}.bind(this));
Expand All @@ -298,7 +268,7 @@

match.setAttribute('href', 'https://buffer.com/add?url=' + attrs.url + '&text=' + attrs.title);

this.jsonp('https://api.bufferapp.com/1/links/shares.json?url=' + attrs.url, false, function (data) {
this.jsonp('https://api.bufferapp.com/1/links/shares.json?url=' + attrs.url, function (data) {
match.textContent = this.abbrNum(data.shares, 1);
}.bind(this));
}.bind(this));
Expand All @@ -311,7 +281,7 @@

match.setAttribute('href', 'https://pinterest.com/pin/create/button/?url=' + attrs.url + '&media=' + attrs.image + '&description=' + attrs.title);

this.jsonp('https://api.pinterest.com/v1/urls/count.json?&url=' + attrs.url, false, function (data) {
this.jsonp('https://api.pinterest.com/v1/urls/count.json?&url=' + attrs.url, function (data) {
match.textContent = this.abbrNum(data.count, 1);
}.bind(this));
}.bind(this));
Expand Down

0 comments on commit 77af80d

Please sign in to comment.