The original is used by Twitter and Facebook, because the domestic is strong, so I feel useful is inside a get JSONP that tool class.
My headache is that many popular APIs already require authentication to get information.
Now that I can access these pages and get the information, why don't I use some simple code to get and skip the verification step?
I think Twitter and Facebook require authentication to get the number of articles, but it turns out that you can get that information through JSONP. Please refer to the steps below.
The JavaScript
I will use basic JavaScript to tell you how to do this:
Copy Code code as follows:
Gets the encapsulated object for the number of articles
var Socialgetter = (function () {
/* JSONP: Get the tool function of the script * *
function Injectscript (URL) {
var script = document.createelement (' script ');
Script.async = true;
script.src = URL;
Document.body.appendChild (script);
}
return {
Getfacebookcount:function (URL, callbackname) {
Injectscript (' https://graph.facebook.com/?id= ' + URL + ' &callback= ' + callbackname);
},
Gettwittercount:function (URL, callbackname) {
Injectscript (' http://urls.api.twitter.com/1/urls/count.json?url= ' + URL + ' &callback= ' + callbackname);
}
};
})();
callback method
function Twittercallback (Result) {
Result.count && Console.log (' The Count is: ', result.count);
}
function Facebookcallback (Result) {
Result.shares && Console.log (' The Count is: ', result.shares);
}
Call
Socialgetter.getfacebookcount (' Http://davidwalsh.name/twitter-facebook-jsonp ', ' facebookcallback ');
Socialgetter.gettwittercount (' Http://davidwalsh.name/twitter-facebook-jsonp ', ' twittercallback ');
Because there are many lightweight micro-frameworks to handle JSONP, the most important part of this article may be the URL to get the information. Choose your JSONP tools according to your needs and habits!
Twitter and Facebook are definitely limited in number and frequency for these requests, so if your site has a lot of traffic, the JSONP is likely to be blocked or blocked. A quick solution is to store the article quantity information in Sessionstorage, but this is only a way for individual users. If you run a large site traffic, you'd better choose to crawl the data on the server side and cache it, and automatically refresh within a certain amount of time.