Last active
July 29, 2025 16:46
-
-
Save ncesar/cfbc737b6b361605f695cd95d8026c0a to your computer and use it in GitHub Desktop.
Fixes invalid date format in requests to PE Cidadão AP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Fix PE Cidadão Agendamento Payload (XHR + Fetch) | |
// @namespace http://tampermonkey.net/ | |
// @version 1.1 | |
// @description Fixes invalid date format in requests to PE Cidadão API | |
// @author cesar.dev.br | |
// @match https://pecidadao.pe.gov.br/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const targetUrlPart = "/horarioAgenda/consultarParaAgendamento/"; | |
function fixDatePayload(body) { | |
try { | |
let data = JSON.parse(body); | |
if (data.data && typeof data.data === "string") { | |
let parts = data.data.split("T"); | |
let dateParts = parts[0].split("-"); | |
if (dateParts.length === 3) { | |
let year = dateParts[0]; | |
let month = dateParts[1]; | |
let day = dateParts[2]; | |
// Detect wrong ordering (day > 12 means it's probably day-month swapped) | |
if (parseInt(month) > 12) { | |
let tmp = month; | |
month = day; | |
day = tmp; | |
} | |
// Ensure zero-padding | |
month = month.toString().padStart(2, "0"); | |
day = day.toString().padStart(2, "0"); | |
data.data = `${year}-${month}-${day}T${parts[1]}`; | |
console.log("[Tampermonkey] Fixed date to:", data.data); | |
} | |
} | |
return JSON.stringify(data); | |
} catch (e) { | |
console.warn("[Tampermonkey] Could not parse payload:", e); | |
return body; | |
} | |
} | |
// ---- Patch fetch ---- | |
const origFetch = window.fetch; | |
window.fetch = async function(resource, config) { | |
try { | |
if (typeof resource === "string" && resource.includes(targetUrlPart) && config && config.body) { | |
config.body = fixDatePayload(config.body); | |
} | |
} catch (e) { | |
console.warn("[Tampermonkey] Fetch hook error:", e); | |
} | |
return origFetch.apply(this, arguments); | |
}; | |
// ---- Patch XHR ---- | |
const origOpen = XMLHttpRequest.prototype.open; | |
const origSend = XMLHttpRequest.prototype.send; | |
XMLHttpRequest.prototype.open = function(method, url) { | |
this._intercept = url.includes(targetUrlPart); | |
return origOpen.apply(this, arguments); | |
}; | |
XMLHttpRequest.prototype.send = function(body) { | |
if (this._intercept && typeof body === "string") { | |
body = fixDatePayload(body); | |
} | |
return origSend.call(this, body); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment