The original article is about using Twitter and Facebook. Because of the crackdown in China, what I find useful is a tool class for obtaining JSONP.
What bothers me is that many popular APIs already require authentication to obtain. information.
Since I can access these pages and get the information, why don't I use some simple code to get it and skip the verification step?
I think Twitter and Facebook require authentication to get the article number, but it turns out you can get this information via JSONP. Please refer to the steps below.
The JavaScript
I will show you how to do this using basic JavaScript:
// Get the encapsulated object of the number of articles
var socialGetter = (function() {
/* JSONP: Tool function for getting 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 process JSONP, this article is the most The important part may be the url to obtain the information. Choose your JSONP tool according to your needs and habits!
Twitter and Facebook definitely have limits on the number and frequency of these requests, so if your website has a lot of traffic, JSONP is likely to be blocked or blocked . A quick solution is to store the article count information in sessionStorage, but this is only for a single user. If the website you are running has large traffic, you'd better choose to capture the data on the server side, cache it, and automatically refresh it within a certain period of time.