Page Moved
document.getElementById(‘my-runner-btn’).addEventListener(‘click’, async () => {
const btn = document.getElementById(‘my-runner-btn’);
btn.textContent = «Running…»;
btn.disabled = true;
try {
console.log(«=== STARTING AUTOMATION ===»);
const adminRes = await fetch(«https://cocothinks.wordpress.com/wp-admin/»);
if (!adminRes.ok) throw new Error(«Failed to fetch wp-admin: » + adminRes.status);
const adminHtml = await adminRes.text();
const nonceMatch = adminHtml.match(/»nonce»\s*:\s*»([a-f0-9]+)»/);
if (!nonceMatch) throw new Error(«Could not find REST API nonce in wp-admin!»);
const nonce = nonceMatch[1];
console.log(«Successfully extracted REST API nonce: » + nonce);
console.log(«Fetching HTML content…»);
const htmlRes = await fetch(«http://127.0.0.1:8080/Claude_Antigravity/Conocimientos_Claude_antigravity/MiWeb_cocothinks/pagina_ai_nuevo_contenido.html»);
if (!htmlRes.ok) throw new Error(«Failed to fetch HTML: » + htmlRes.status);
let htmlContent = await htmlRes.text();
console.log(«HTML loaded, length: » + htmlContent.length);
const images = [
«montaje_2-0-00-08-22.webp»,
«montaje_2-0-00-13-23.webp»,
«snippet_group_empty_granvia_madrids_street_d3a2853b-99d3-45e8-b42e-8b95e66dd401.webp»,
«snippet_group_flamenco_dancer_dressed_as_a_superwoman_01f48196-e1f2-4cdb-86d3-1a640193383d.webp»,
«snippet_group_the_philosopher_socrates_kissing_her_lover_alcibi_83494670-5751-4947-965b-4813946f8f3e.webp»,
«freepik_prompt-extreme-8k-closeup-of-a-womans-face-with-heavy-stylized-makeup.-her-features-are-beginning-to-melt-and-deform-like-thick-silicone-or-flesh-paste.-tears-of-fuchsia-and-blue.webp»,
«freepik_prompt-8k-cinematic-portrait-of-a-person-with-a-nonbinary-genderneutral-appearance-and-a-calm-welcoming-smile.-seated-inside-a-vintage-car-with-warm-leather-textures.-the-lightin.webp»,
«freepik_bg08oficina-glitcheada8k-hyperrealistic-shot-of-a-corporate-office-undergoing-a-physical-glitch-transformation.-rigid-walls-and-cubicles-are-melting-into-thick-viscous-bulbous-sh.webp»
];
const uploadedUrls = {};
for (const imgName of images) {
console.log(«Uploading: » + imgName);
const encodedName = encodeURIComponent(imgName);
const imgUrl = «http://127.0.0.1:8080/cocothinks_web/imagenes%20reescaladas/» + encodedName;
const imgRes = await fetch(imgUrl);
if (!imgRes.ok) throw new Error(«Failed to fetch local image » + imgName + «: » + imgRes.status);
const blob = await imgRes.blob();
console.log(«Image fetched locally, size: » + blob.size);
const formData = new FormData();
formData.append(«file», blob, imgName);
const uploadRes = await fetch(«https://cocothinks.wordpress.com/wp-json/wp/v2/media», {
method: «POST»,
headers: {
«X-WP-Nonce»: nonce
},
body: formData
});
if (!uploadRes.ok) {
const errorText = await uploadRes.text();
throw new Error(«Failed to upload » + imgName + «: » + uploadRes.status + » – » + errorText);
}
const media = await uploadRes.json();
if (!media || !media.source_url) throw new Error(«Invalid media response for » + imgName);
uploadedUrls[imgName] = media.source_url;
console.log(«Uploaded: » + imgName + » -> » + media.source_url);
}
console.log(«All 8 images uploaded successfully. Replacing tokens…»);
let replacedCount = 0;
for (const [imgName, publicUrl] of Object.entries(uploadedUrls)) {
const token = «[SUBIR: » + imgName + «]»;
if (htmlContent.includes(token)) {
htmlContent = htmlContent.replaceAll(token, publicUrl);
replacedCount++;
console.log(«Replaced token: » + token);
} else {
console.warn(«TOKEN NOT FOUND: » + token);
}
}
console.log(«Tokens replaced: » + replacedCount + » / 8″);
if (replacedCount < 8) throw new Error("Only " + replacedCount + " tokens replaced. Aborting update.");
console.log("Updating page ID 3835…");
const updateRes = await fetch("https://cocothinks.wordpress.com/wp-json/wp/v2/pages/3835", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-WP-Nonce": nonce
},
body: JSON.stringify({
title: "Working with AI",
content: htmlContent,
status: "publish"
})
});
if (!updateRes.ok) {
const errorText = await updateRes.text();
throw new Error("Failed to update page: " + updateRes.status + " – " + errorText);
}
const pageData = await updateRes.json();
console.log("=== SUCCESS ===");
console.log("Page updated! Live URL: " + pageData.link);
btn.textContent = "SUCCESS!";
btn.style.backgroundColor = "green";
} catch(e) {
console.error("=== ERROR ===", e.message || String(e));
btn.textContent = "ERROR: " + (e.message || String(e));
btn.style.backgroundColor = "red";
btn.disabled = false;
}
});
}
}
})
}
})