JSFiddle - React, Tailwind, and code Playground
by platonfedr
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 />MR: <span class="mr"></span>
<br />LI: <span class="li"></span>
<br />TM: <span class="tm"></span>
<br />PT: <span class="pt"></span>
<br />RD: <span class="rd"></span>
<br />SU: <span class="su"></span>
<br />PO: <span class="po"></span>
<br />SB: <span class="sb"></span>
<br />BF: <span class="bf"></span>
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() {
// VK
function getVK() {
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);
};
}
else return;
};
// OK
function getOK() {
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));
totalCount.push(count);
};
};
};
// FB
function getFB() {
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);
...