JSFiddle - React, Tailwind, and code Playground
HTML
Всего: <span class="total"></span><br /><br />
VK: <span class="vk"></span><br />
OK: <span class="ok"></span><br />
FB: <span class="fb"></span><br />
TW: <span class="tw"></span><br />
GP: <span class="gp"></span><br />
JavaScript
$(document).ready(function() {
// Переменные
var url = 'http://youtube.com';
// Создаём массив
var totalCount = new Array();
// Функция округления цифр
var roundCount = function(number) {
if (number > 999 && number <= 999999) var result = number/1000 + 'k';
else if (number > 999999) var result = '>1M';
else var result = number;
return result;
};
// Проверяем наличие
var existCount = function(element) {
return ($(element).length > 0);
};
$.fn.getCount = function() {
var dfd = new $.Deferred();
// VK
if (existCount('span.vk')) {
$.getJSON('https://vk.com/share.php?act=count&index=1&url=' + encodeURIComponent(url) + '&callback=?', function(response) {});
VK = {};
VK.Share = {};
VK.Share.count = function(index, count) {
$('span.vk').text(roundCount(count));
totalCount.push(count);
dfd.resolve();
};
};
// FB
if (existCount('span.fb')) {
$.getJSON('http://graph.facebook.com/?id=' + encodeURIComponent(url) + '&callback=?', function(response) {
if (response.shares === undefined) {
$('span.fb').text('0');
totalCount.push(0);
}
else {
$('span.fb').text(roundCount(response.shares));
totalCount.push(response.shares);
dfd.resolve();
};
});
};
// OK
if (existCount('span.ok')) {
$.getJSON('https://connect.ok.ru/dk?st.cmd=extLike&uid=1&ref=' + encodeURIComponent(url) + '&callback=?', function(response) {});
ODKL = {};
ODKL.updateCount = function(index, count) {
$('span.ok').text(roundCount(count));
...