Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
bg-az-counter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
azbyka_bg
bg-az-counter
Commits
37badd20
Commit
37badd20
authored
Nov 06, 2019
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Version 2.6.4
Оптимизирована подгрузка новых счетчиков
parent
0e8c96ab
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
154 additions
and
146 deletions
+154
-146
bg_az-counter.php
bg_az-counter.php
+2
-2
counter.js
js/counter.js
+70
-62
rating.js
js/rating.js
+82
-82
No files found.
bg_az-counter.php
View file @
37badd20
...
...
@@ -3,7 +3,7 @@
Plugin Name: Bg Az-Counter
Plugin URI: https://bogaiskov.ru
Description: Подсчет количества посещений страниц на базе stat.azbyka.ru
Version: 2.6.
2
Version: 2.6.
4
Author: VBog
Author URI: https://bogaiskov.ru
License: GPL2
...
...
@@ -38,7 +38,7 @@
if
(
!
defined
(
'ABSPATH'
)
)
{
die
(
'Sorry, you are not allowed to access this page directly.'
);
}
define
(
'BG_COUNTER_VERSION'
,
'2.6.
2
'
);
define
(
'BG_COUNTER_VERSION'
,
'2.6.
4
'
);
define
(
'BG_COUNTER_LOG'
,
dirname
(
__FILE__
)
.
'/bg_counter.log'
);
define
(
'BG_COUNTER_STAT_COUNTERS'
,
'https://stat.azbyka.ru/counters'
);
...
...
js/counter.js
View file @
37badd20
...
...
@@ -13,7 +13,11 @@ jQuery( document ).ready(function() {
var
socket
=
new
ReconnectingWebSocket
(
request
,
null
,
{
timeoutInterval
:
10000
});
socket
.
onopen
=
function
()
{
if
(
bg_counter
.
debug
)
console
.
log
(
" Соединение установлено: "
+
request
);
GetAllCounters
();
// Обновляет счетчики каждые 3 сек, если добавлены элементы.
let
timerAllCountersId
=
setTimeout
(
function
tickAllCounters
()
{
GetAllCounters
();
timerAllCountersId
=
setTimeout
(
tickAllCounters
,
bg_counter
.
updatetime
?
bg_counter
.
updatetime
:
3000
);
},
bg_counter
.
updatetime
?
bg_counter
.
updatetime
:
3000
);
};
// Обработка ошибок
if
(
bg_counter
.
debug
)
{
...
...
@@ -29,9 +33,17 @@ jQuery( document ).ready(function() {
console
.
log
(
'Код: '
+
event
.
code
+
' причина: '
+
event
.
reason
);
};
}
}
else
GetAllCounters
();
bg_counter_elements_reloaded_on_scroll
();
}
else
{
// Обновляет счетчики каждые 3 сек, если добавлены элементы.
let
timerAllCountersId
=
setTimeout
(
function
tickAllCounters
()
{
GetAllCounters
();
timerAllCountersId
=
setTimeout
(
tickAllCounters
,
bg_counter
.
updatetime
?
bg_counter
.
updatetime
:
3000
);
},
bg_counter
.
updatetime
?
bg_counter
.
updatetime
:
3000
);
}
// Обновляет счетчики после прокрутки страницы, если добавлены элементы.
// jQuery(window).on('scroll', function() {
// GetAllCounters();
// });
/*********************************************************************************
Просомтр счетчиков читающих в реальном времени.
...
...
@@ -190,51 +202,66 @@ GET /counters/project/test/author/1/book/3
**********************************************************************************/
function
GetAllCounters
()
{
jQuery
(
'span.bg-az-counter'
).
each
(
function
()
{
var
el
=
jQuery
(
this
);
// bg_counter_elements = el.length;
var
type
=
el
.
attr
(
'data-type'
);
var
id
=
el
.
attr
(
'data-ID'
);
var
project
=
el
.
attr
(
'data-project'
);
if
(
project
==
""
)
request
=
bg_counter
.
counterurl
;
else
{
if
(
project
)
project
=
'/project/'
+
project
;
else
project
=
bg_counter
.
project
;
if
(
!
type
||
!
id
)
var
request
=
bg_counter
.
counterurl
+
project
;
else
var
request
=
bg_counter
.
counterurl
+
project
+
"/"
+
type
+
"/"
+
id
;
}
var
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"GET"
,
request
,
true
);
xhr
.
onreadystatechange
=
function
()
{
if
(
xhr
.
readyState
==
4
&&
xhr
.
status
==
200
)
{
if
(
xhr
.
responseText
)
{
var
response
=
JSON
.
parse
(
xhr
.
responseText
);
if
(
response
.
success
)
{
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
);
if
(
bg_counter
.
debug
)
console
.
log
(
JSON
.
stringify
(
response
.
data
));
el
.
find
(
'span.bg-az-counter-views'
).
text
(
bg_counter_number_format
(
response
.
data
.
total
));
el
.
find
(
'span.bg-az-counter-now'
).
text
(
addDelimiter
(
response
.
data
.
now
));
var
elem
=
jQuery
(
'span.bg-az-counter'
);
if
(
typeof
elem
==
'undefined'
)
{
return
;
}
var
elem_num
=
0
;
if
(
elem
.
length
>
bg_counter_elements
)
{
elem
.
each
(
function
()
{
if
(
elem_num
>=
bg_counter_elements
)
{
var
el
=
jQuery
(
this
);
var
type
=
el
.
attr
(
'data-type'
);
var
id
=
el
.
attr
(
'data-ID'
);
var
project
=
el
.
attr
(
'data-project'
);
if
(
project
==
""
)
request
=
bg_counter
.
counterurl
;
else
{
if
(
project
)
project
=
'/project/'
+
project
;
else
project
=
bg_counter
.
project
;
if
(
!
type
||
!
id
)
var
request
=
bg_counter
.
counterurl
+
project
;
else
var
request
=
bg_counter
.
counterurl
+
project
+
"/"
+
type
+
"/"
+
id
;
}
var
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"GET"
,
request
,
true
);
xhr
.
onreadystatechange
=
function
()
{
if
(
xhr
.
readyState
!=
4
)
return
;
if
(
xhr
.
status
==
200
)
{
if
(
xhr
.
responseText
)
{
var
response
=
JSON
.
parse
(
xhr
.
responseText
);
if
(
response
.
success
)
{
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
);
if
(
bg_counter
.
debug
)
console
.
log
(
JSON
.
stringify
(
response
.
data
));
el
.
find
(
'span.bg-az-counter-views'
).
text
(
bg_counter_number_format
(
response
.
data
.
total
));
el
.
find
(
'span.bg-az-counter-now'
).
text
(
addDelimiter
(
response
.
data
.
now
));
}
else
{
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
+
' ERROR '
+
xhr
.
status
+
': '
+
response
.
error
);
el
.
find
(
'span.bg-az-counter-views'
).
text
(
'0'
);
el
.
find
(
'span.bg-az-counter-now'
).
text
(
'0'
);
}
}
else
{
if
(
bg_counter
.
debug
)
console
.
warn
(
'GET REQUEST: '
+
request
+
' Warning: responseText is empty!'
);
el
.
find
(
'span.bg-az-counter-views'
).
text
(
' - '
);
el
.
find
(
'span.bg-az-counter-now'
).
text
(
' - '
);
}
}
else
{
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
+
' ERROR '
+
xhr
.
status
+
': '
+
response
.
error
);
el
.
find
(
'span.bg-az-counter-views'
).
text
(
'
0
'
);
el
.
find
(
'span.bg-az-counter-now'
).
text
(
'
0
'
);
if
(
bg_counter
.
debug
)
console
.
warn
(
'ERROR '
+
xhr
.
status
+
': '
+
xhr
.
statusText
);
el
.
find
(
'span.bg-az-counter-views'
).
text
(
'
-
'
);
el
.
find
(
'span.bg-az-counter-now'
).
text
(
'
-
'
);
}
}
else
{
if
(
bg_counter
.
debug
)
console
.
warn
(
'GET REQUEST: '
+
request
+
' Warning: responseText is empty!'
);
}
xhr
.
onerror
=
function
(
err
)
{
console
.
warn
(
err
.
type
+
" "
+
err
.
target
.
status
+
". Check if the server is running!"
);
el
.
find
(
'span.bg-az-counter-views'
).
text
(
' - '
);
el
.
find
(
'span.bg-az-counter-now'
).
text
(
' - '
);
}
xhr
.
send
();
}
}
xhr
.
onerror
=
function
(
err
)
{
console
.
warn
(
err
.
type
+
" "
+
err
.
target
.
status
+
". Check if the server is running!"
);
el
.
find
(
'span.bg-az-counter-views'
).
text
(
' - '
);
el
.
find
(
'span.bg-az-counter-now'
).
text
(
' - '
);
}
xhr
.
send
();
});
elem_num
++
;
});
bg_counter_elements
=
elem
.
length
;
}
}
/*********************************************************************************
Отображает значения счетчика на странице
...
...
@@ -280,22 +307,3 @@ function bg_counter_number_format (num) {
return
num
;
}
/*********************************************************************************
Обновляет счетчики после прокрутки страницы,
если добавлены элементы.
**********************************************************************************/
function
bg_counter_elements_reloaded_on_scroll
()
{
jQuery
(
window
).
on
(
'scroll'
,
function
()
{
var
elem
=
jQuery
(
'span.bg-az-counter'
);
if
(
typeof
elem
==
'undefined'
)
{
return
;
}
if
(
elem
.
length
>
bg_counter_elements
)
{
bg_counter_elements
=
elem
.
length
;
GetAllCounters
();
// getAllRates();
}
});
}
js/rating.js
View file @
37badd20
...
...
@@ -6,43 +6,51 @@ var bg_counter_ratings = 0;
jQuery
(
document
).
ready
(
function
()
{
// Обновлять рейтинги каждые 3 сек, если добавлены элементы.
let
timerAllRatesId
=
setTimeout
(
function
tickAllRates
()
{
getAllRates
();
timerAllRatesId
=
setTimeout
(
tickAllRates
,
bg_counter
.
updatetime
?
bg_counter
.
updatetime
:
3000
);
},
bg_counter
.
updatetime
?
bg_counter
.
updatetime
:
3000
);
// Обновлять рейтинги после прокрутки страницы, если добавлены элементы.
// jQuery(window).on('scroll', function() {
// getAllRates();
bg_counter_ratings_reloaded_on_scroll
();
if
(
!
bg_counter
.
ID
)
return
;
// У объекта нет ID
if
(
jQuery
(
"div"
).
is
(
".bg_counter_rating"
)
==
false
)
return
;
// На странице нет счетчика
start_rate_index
=
parseFloat
(
jQuery
(
"#bg_counter_score"
).
html
());
rating_voted
=
(
jQuery
(
"#bg_counter_score"
).
attr
(
"data-voted"
)
==
'true'
)?
true
:
false
;
iniRatingState
(
start_rate_index
,
rating_voted
);
getRate
(
bg_counter
.
type
,
bg_counter
.
ID
);
jQuery
(
"#bg_counter_rate_box li"
).
mouseover
(
function
()
{
if
(
!
rating_voted
){
var
index
=
jQuery
(
this
).
index
();
iniRatingState
(
index
+
1
,
rating_voted
);
jQuery
(
'#bg_counter_popup_help'
).
text
(
bg_counter
.
price
[
index
]);
}
else
{
jQuery
(
'#bg_counter_popup_help'
).
text
(
bg_counter
.
voted
);
}
jQuery
(
'#bg_counter_popup_help'
).
show
();
});
// });
if
(
!
bg_counter
.
ID
)
return
;
// У объекта нет ID
if
(
jQuery
(
"div"
).
is
(
".bg_counter_rating"
)
==
false
)
return
;
// На странице нет счетчика
start_rate_index
=
parseFloat
(
jQuery
(
"#bg_counter_score"
).
html
());
rating_voted
=
(
jQuery
(
"#bg_counter_score"
).
attr
(
"data-voted"
)
==
'true'
)?
true
:
false
;
iniRatingState
(
start_rate_index
,
rating_voted
);
getRate
(
bg_counter
.
type
,
bg_counter
.
ID
);
jQuery
(
"#bg_counter_rate_box li"
).
mouseover
(
function
()
{
if
(
!
rating_voted
){
var
index
=
jQuery
(
this
).
index
();
iniRatingState
(
index
+
1
,
rating_voted
);
jQuery
(
'#bg_counter_popup_help'
).
text
(
bg_counter
.
price
[
index
]);
}
else
{
jQuery
(
'#bg_counter_popup_help'
).
text
(
bg_counter
.
voted
);
}
jQuery
(
'#bg_counter_popup_help'
).
show
();
});
jQuery
(
"#bg_counter_rate_box"
).
mouseout
(
function
()
{
if
(
!
rating_voted
){
iniRatingState
(
start_rate_index
,
rating_voted
);
}
jQuery
(
'#bg_counter_popup_help'
).
hide
();
});
jQuery
(
"#bg_counter_rate_box li"
).
click
(
function
()
{
if
(
!
rating_voted
){
rating_voted
=
true
;
jQuery
(
"#bg_counter_rate_box li"
).
css
(
'cursor'
,
'default'
);
var
sindex
=
jQuery
(
this
).
index
()
+
1
;
sendRate
(
bg_counter
.
type
,
bg_counter
.
ID
,
sindex
);
}
});
jQuery
(
"#bg_counter_rate_box"
).
mouseout
(
function
()
{
if
(
!
rating_voted
){
iniRatingState
(
start_rate_index
,
rating_voted
);
}
jQuery
(
'#bg_counter_popup_help'
).
hide
();
});
jQuery
(
"#bg_counter_rate_box li"
).
click
(
function
()
{
if
(
!
rating_voted
){
rating_voted
=
true
;
jQuery
(
"#bg_counter_rate_box li"
).
css
(
'cursor'
,
'default'
);
var
sindex
=
jQuery
(
this
).
index
()
+
1
;
sendRate
(
bg_counter
.
type
,
bg_counter
.
ID
,
sindex
);
}
});
});
function
iniRatingState
(
sindex
,
voted
){
if
(
!
voted
)
jQuery
(
"#bg_counter_rate_box li"
).
css
(
'cursor'
,
'pointer'
);
...
...
@@ -130,41 +138,51 @@ function getRate(type, id) {
function
getAllRates
()
{
jQuery
(
'span.bg-az-counter'
).
each
(
function
()
{
var
el
=
jQuery
(
this
);
// bg_counter_ratings = el.length;
var
type
=
el
.
attr
(
'data-type'
);
var
id
=
el
.
attr
(
'data-ID'
);
var
project
=
el
.
attr
(
'data-project'
);
if
(
project
)
project
=
'/project/'
+
project
;
else
project
=
bg_counter
.
project
;
if
(
!
type
||
!
id
)
return
;
var
request
=
bg_counter
.
scoreurl
+
bg_counter
.
project
+
"/"
+
type
+
"/"
+
id
;
var
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"GET"
,
request
,
true
);
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
);
xhr
.
onreadystatechange
=
function
()
{
if
(
xhr
.
readyState
==
4
&&
xhr
.
status
==
200
)
{
if
(
xhr
.
responseText
)
{
var
response
=
JSON
.
parse
(
xhr
.
responseText
);
if
(
response
.
success
)
{
// Вывод данных на экран
if
(
bg_counter
.
debug
)
console
.
log
(
JSON
.
stringify
(
response
.
data
));
el
.
find
(
'span.bg-az-counter-score'
).
text
(
parseFloat
(
response
.
data
.
score
).
toFixed
(
1
));
var
elem
=
jQuery
(
'span.bg-az-counter'
);
if
(
typeof
elem
==
'undefined'
)
{
return
;
}
if
(
elem
.
length
>
bg_counter_ratings
)
{
bg_counter_ratings
=
elem
.
length
;
jQuery
(
'span.bg-az-counter'
).
each
(
function
()
{
var
el
=
jQuery
(
this
);
var
type
=
el
.
attr
(
'data-type'
);
var
id
=
el
.
attr
(
'data-ID'
);
var
project
=
el
.
attr
(
'data-project'
);
if
(
project
)
project
=
'/project/'
+
project
;
else
project
=
bg_counter
.
project
;
if
(
!
type
||
!
id
)
return
;
var
request
=
bg_counter
.
scoreurl
+
bg_counter
.
project
+
"/"
+
type
+
"/"
+
id
;
var
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
"GET"
,
request
,
true
);
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
);
xhr
.
onreadystatechange
=
function
()
{
if
(
xhr
.
readyState
!=
4
)
return
;
if
(
xhr
.
status
==
200
)
{
if
(
xhr
.
responseText
)
{
var
response
=
JSON
.
parse
(
xhr
.
responseText
);
if
(
response
.
success
)
{
// Вывод данных на экран
if
(
bg_counter
.
debug
)
console
.
log
(
JSON
.
stringify
(
response
.
data
));
el
.
find
(
'span.bg-az-counter-score'
).
text
(
parseFloat
(
response
.
data
.
score
).
toFixed
(
1
));
}
else
{
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
+
' ERROR: '
+
response
.
error
);
el
.
find
(
'span.bg-az-counter-score'
).
text
(
'0'
);
}
}
else
{
if
(
bg_counter
.
debug
)
console
.
log
(
'GET REQUEST: '
+
request
+
' ERROR: '
+
response
.
error
);
el
.
find
(
'span.bg-az-counter-score'
).
text
(
'
0
'
);
if
(
bg_counter
.
debug
)
console
.
warn
(
'GET REQUEST: '
+
request
+
' Warning: responseText is empty!'
);
el
.
find
(
'span.bg-az-counter-score'
).
text
(
'
-
'
);
}
}
else
{
if
(
bg_counter
.
debug
)
console
.
warn
(
'
GET REQUEST: '
+
request
+
' Warning: responseText is empty!'
);
el
.
find
(
'span.bg-az-counter-score'
).
text
(
'
-
'
);
if
(
bg_counter
.
debug
)
console
.
warn
(
'
ERROR '
+
xhr
.
status
+
': '
+
xhr
.
statusText
);
el
.
find
(
'span.bg-az-counter-score'
).
text
(
'
-
'
);
}
}
}
xhr
.
send
();
});
xhr
.
send
();
});
}
}
...
...
@@ -226,21 +244,3 @@ function sendRate(type, id, number) {
xhr
.
send
(
'{"rating": '
+
number
+
'}'
);
}
/*********************************************************************************
Обновляет рейтинги после прокрутки страницы,
если добавлены элементы.
**********************************************************************************/
function
bg_counter_ratings_reloaded_on_scroll
()
{
jQuery
(
window
).
on
(
'scroll'
,
function
()
{
var
elem
=
jQuery
(
'span.bg-az-counter'
);
if
(
typeof
elem
==
'undefined'
)
{
return
;
}
if
(
elem
.
length
>
bg_counter_ratings
)
{
bg_counter_ratings
=
elem
.
length
;
getAllRates
();
}
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment