PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.1
WebberZone Top 10 — Popular Posts v4.5.1
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
← All changes | includes/js/top-10.js +64 -28 4.4.3 → 4.5.1 View file →
@@ -1,31 +1,67 @@
1 -document.addEventListener('DOMContentLoaded', function () {
2 - var params = {
3 - action: 'tptn_tracker',
4 - top_ten_id: ajax_tptn_tracker.top_ten_id,
5 - top_ten_blog_id: ajax_tptn_tracker.top_ten_blog_id,
6 - activate_counter: ajax_tptn_tracker.activate_counter,
7 - top_ten_debug: ajax_tptn_tracker.top_ten_debug
8 - };
1 +(function () {
2 + if ('undefined' === typeof ajax_tptn_tracker) {
3 + return;
4 + }
9 5
10 - fetch(ajax_tptn_tracker.ajax_url, {
11 - method: 'POST',
12 - headers: {
13 - 'Content-Type': 'application/x-www-form-urlencoded',
14 - 'Cache-Control': 'no-cache'
15 - },
16 - body: new URLSearchParams(params).toString()
17 - })
18 - .then(function (response) {
19 - if (!response.ok && 204 !== response.status) {
20 - throw new Error('Tracker request failed');
21 - }
6 + var tracker = ajax_tptn_tracker;
7 + var tracked = false;
22 8
23 - return response.text();
24 - })
25 - .then(function (data) {
26 - // handle the response data
27 - })
28 - .catch(function (error) {
29 - console.error('Error:', error);
9 + function send(id, context) {
10 + var params = new URLSearchParams({
11 + action: 'tptn_tracker',
12 + top_ten_id: id,
13 + top_ten_blog_id: tracker.top_ten_blog_id,
14 + activate_counter: tracker.activate_counter,
15 + top_ten_debug: tracker.top_ten_debug
30 16 });
31 -});
17 +
18 + if (context) {
19 + params.append('top_ten_sitewide_context', context);
20 + }
21 +
22 + var url = tracker.ajax_url;
23 + if ('query_based' === tracker.tracker_type) {
24 + url += (url.includes('?') ? '&' : '?') + params;
25 + }
26 +
27 + // Keep URLSearchParams as the beacon body so WordPress parses the payload as form data.
28 + if ('1' === tracker.top_ten_debug || !navigator.sendBeacon || !navigator.sendBeacon(url, params)) {
29 + fetch(url, { method: 'POST', body: params, keepalive: true }).catch(function () {});
30 + }
31 + }
32 +
33 + function track() {
34 + if (tracked) {
35 + return;
36 + }
37 + if (document.prerendering) {
38 + document.addEventListener('prerenderingchange', track, { once: true });
39 + return;
40 + }
41 + if ('hidden' === document.visibilityState) {
42 + document.addEventListener('visibilitychange', track, { once: true });
43 + return;
44 + }
45 +
46 + tracked = true;
47 + if (tracker.top_ten_id > 0) {
48 + send(tracker.top_ten_id, '');
49 + }
50 + if (tracker.top_ten_sitewide_context) {
51 + send(0, tracker.top_ten_sitewide_context);
52 + }
53 + }
54 +
55 + if ('loading' === document.readyState) {
56 + document.addEventListener('DOMContentLoaded', track);
57 + } else {
58 + track();
59 + }
60 +
61 + window.addEventListener('pageshow', function (event) {
62 + if (event.persisted) {
63 + tracked = false;
64 + track();
65 + }
66 + });
67 +})();