page_tester-1.0.x-dev/js/page_tester.js
js/page_tester.js
/**
* @file
* Page Tester behaviors.
*/
(function ($, Drupal, once) {
let refreshInterval;
Drupal.behaviors.pageTesterRefresh = {
attach: function (context, settings) {
const elements = once('PageTester', 'form.page-tester-form', context);
elements.forEach(processingCallback);
function processingCallback() {
const playButton = $('#page-tester-play-button');
const stopButton = $('#page-tester-stop-button');
const testId = $('#page-tester-test-id').val();
if (playButton && stopButton) {
playButton.on('click', function () {
refreshInterval = setTimeout(function () {
// get the test id, test number from the session.
// call set test in the correct plugin
// set the session with the next test id and test number
// window reload.
console.log(testId);
$.ajax({
url: Drupal.url('admin/page_tester/play-tests'),
type: 'POST',
dataType: 'json',
data: {
test_id: testId
},
success: function (res) {
console.log(res);
location.reload();
},
error: function (xhr, status, error) {
console.log('AJAX error: ', error);
}
});
}, 5000);
});
stopButton.on('click', function () {
console.log(testId);
$.ajax({
url: Drupal.url('admin/page_tester/stop-tests'),
type: 'POST',
dataType: 'json',
data: {
test_id: testId
},
success: function (res) {
console.log(res);
location.reload();
},
error: function (xhr, status, error) {
console.log('AJAX error: ', error);
}
});
clearInterval(refreshInterval);
console.log('Page Refresh stopped.');
});
}
}
}
};
})(jQuery, Drupal, once);
