BTW here my code in the case you wonder:
As you see the changeMongoType function will do the job as now it only parsers
“Id”: “b52918ea-3d32-4b5c-ac2c-114ac940c47d” to “Id”: “UUID(‘b52918ea-3d32-4b5c-ac2c-114ac940c47d’)" which again is a string and dose not fulfill what I am looking for.
document.addEventListener('DOMContentLoaded', function () {
const copyButton = document.getElementById('copyButton');
const msg = document.getElementById('msg');
copyButton.addEventListener('click', function () {
// Clear the existing URL list
msg.innerHTML = '';
// Get the current active tab's URL
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
let url = new URL(currentTab.url);
//const url = currentTab.url;
const baseUrl = url.protocol + '//' + url.hostname;
console.log(url.hash);
url= new URL(url.hash.replace(/^#/,""),currentTab.url.split("#")[0]);
console.log("URL >>>"+url);
// Extract parameters from the URL (adjust as needed)
const id = url.searchParams.get('id');
const code = url.searchParams.get('code');
const type = url.searchParams.get('type');
// Construct the new URL
const newUrl = `${baseUrl}/api/entity/${id}?companyCode=${code}&module=${type}`;
console.log(newUrl);
// Make the API call using the current URL
fetch(newUrl)
.then(response => response.json())
.then(data => {
data._id = data.Id;
delete data.Id;
const readyMongoData = changeMongoType(data);
// Copy the API response to the clipboard
copyToClipboard(JSON.stringify(readyMongoData)); // JSON.stringify(data)
}).then(()=>{
const newMsg = document.createElement('h3');
newMsg.textContent = new DOMParser().parseFromString(`Copied to cliboard V4 <span>✓</span>`, 'text/html').body.textContent;
msg.appendChild(newMsg);
})
.catch(error => {
console.error('API Call Error:', error);
});
});
});
});
function copyToClipboard(text) {
navigator.clipboard.writeText(text)
.then(() => {
console.log('URL copied to clipboard:', text);
// You can add a success message here
})
.catch(error => {
console.error('Error copying to clipboard:', error);
// You can add an error message here
});
}
function parseDateToISODate(dateString) {
const date = new Date(dateString);
if (isNaN(date)) {
// Handle invalid date input if necessary
return null;
}
return date.toISOString();
}
///////////////////////////////////////////////////////////////////
function changeMongoType(jsonObj) {
for (const key in jsonObj) {
if (jsonObj.hasOwnProperty(key)) {
const value = jsonObj[key];
if (typeof value === 'string' && /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(jsonObj[key])) {
jsonObj[key] = convertStringToUUIDFormat(value);
}else if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/.test(jsonObj[key]) && isStringADate(jsonObj[key])) {
jsonObj[key] = `ISODate("${jsonObj[key]}")`.replace(/\"/g, "");
// jsonObj[key] = `ISODate("${jsonObj[key]}")`.replace(/\"/g, "");
} else if (typeof value === 'object') {
changeMongoType(value); // Recursively process nested objects
}
}
}
return jsonObj;
}
function convertStringToUUIDFormat(input) {
const regex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
if (regex.test(input)) {
console.log(`UUID('${input}')`.replace(/['"]+/g, ''));
//return `UUID('${input}')`.replace(/['"]+/g, '');
console.log("input> "+input);
return UUID(input);
}
return input;
}
function isStringADate(dateString) {
// Try to parse the string into a Date object
const date = new Date(dateString);
// Check if the parsed date is a valid date and the original string is not "Invalid Date"
return !isNaN(date) && date.toString() !== 'Invalid Date';
}
function preservId(dateString) {
// Try to parse the string into a Date object
const date = new Date(dateString);
// Check if the parsed date is a valid date and the original string is not "Invalid Date"
return !isNaN(date) && date.toString() !== 'Invalid Date';
}```