-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare-image-replace.js
More file actions
73 lines (67 loc) · 2.8 KB
/
cloudflare-image-replace.js
File metadata and controls
73 lines (67 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
jQuery(document).ready(function($) {
var interval;
// Start/Stop button logic
$('#cloudflare-image-replace-button').on('click', function() {
var $button = $(this);
$.ajax({
url: cloudflareImageReplaceAjax.ajax_url,
method: 'POST',
data: {
action: 'toggle_image_replace',
nonce: cloudflareImageReplaceAjax.nonce
},
success: function(response) {
if (response.success) {
if (response.data.status === 'started') {
$button.text('Stop Image Replacement');
startProgressTracking();
} else if (response.data.status === 'stopped') {
$button.text('Start Image Replacement');
stopProgressTracking();
}
} else {
alert('Something went wrong! Please try again.');
}
}
});
});
// Function to start progress tracking
function startProgressTracking() {
interval = setInterval(function() {
$.ajax({
url: cloudflareImageReplaceAjax.ajax_url,
method: 'POST',
data: {
action: 'get_image_replace_progress',
nonce: cloudflareImageReplaceAjax.nonce
},
success: function(response) {
if (response.success) {
var totalImages = response.data.total_images;
var processedImages = response.data.processed_images;
var successfulImages = response.data.successful_images;
var failedImages = response.data.failed_images;
// Update counters
$('#total-images').text(totalImages);
$('#processed-images').text(processedImages);
$('#successful-images').text(successfulImages);
$('#failed-images').text(failedImages);
// Update progress bar
if (totalImages > 0) {
var progressPercentage = (processedImages / totalImages) * 100;
$('#progress-bar').css('width', progressPercentage + '%');
}
}
}
});
}, 2000); // Poll every 2 seconds
}
// Function to stop progress tracking
function stopProgressTracking() {
clearInterval(interval);
}
// Start progress tracking immediately if the process is already in progress
if ($('#cloudflare-image-replace-button').text() === 'Stop Image Replacement') {
startProgressTracking();
}
});