feat: moved to fetch instead of ajax call

This commit is contained in:
SamTV12345 2025-08-01 21:53:34 +02:00
parent d72b76b574
commit 62348e2bfc
2 changed files with 36 additions and 14 deletions

View file

@ -1,16 +1,39 @@
'use strict';
import express from "express";
const log4js = require('log4js');
const clientLogger = log4js.getLogger('client');
const {Formidable} = require('formidable');
const apiHandler = require('../../handler/APIHandler');
const util = require('util');
function objectAsString(obj) {
var output = '';
for (var property in obj) {
if(obj.hasOwnProperty(property) && typeof obj[property] !== 'function') {
var value = obj[property];
if(typeof value === 'object' && !Array.isArray(value) && value !== null) {
value = '{' + objectAsString(value) + '}';
}
output += property + ': ' + value +'; ';
}
}
return output;
}
exports.expressPreSession = async (hookName:string, {app}:any) => {
app.use(express.json());
// The Etherpad client side sends information about how a disconnect happened
app.post('/ep/pad/connection-diagnostic-info', async (req:any, res:any) => {
const [fields, files] = await (new Formidable({})).parse(req);
clientLogger.info(`DIAGNOSTIC-INFO: ${fields.diagnosticInfo}`);
if (!req.body ||!req.body.diagnosticInfo || typeof req.body.diagnosticInfo !== 'object') {
clientLogger.warn('DIAGNOSTIC-INFO: No diagnostic info provided');
res.status(400).end('No diagnostic info provided');
return;
}
clientLogger.info(`DIAGNOSTIC-INFO: ${objectAsString(req.body.diagnosticInfo)}`);
res.end('OK');
});

View file

@ -725,18 +725,17 @@ const pad = {
}
},
asyncSendDiagnosticInfo: () => {
window.setTimeout(() => {
$.ajax(
{
type: 'post',
url: '../ep/pad/connection-diagnostic-info',
data: {
diagnosticInfo: JSON.stringify(pad.diagnosticInfo),
},
success: () => {},
error: () => {},
});
}, 0);
fetch('../ep/pad/connection-diagnostic-info', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
diagnosticInfo: pad.diagnosticInfo,
}),
}).catch((error) => {
console.error('Error sending diagnostic info:', error);
})
},
forceReconnect: () => {
$('form#reconnectform input.padId').val(pad.getPadId());