User:Ingenuity/ReferenceEditor.js: Difference between revisions

Content deleted Content added
create
 
add
Line 573:
 
async function getArchiveURL(url) {
try {
const response = await fetch("https://archive.org/wayback/available?url=" + url);
const json = await response.json();
 
if (!json["archived_snapshots"] || !json["archived_snapshots"]["closest"]) {
return { url: "", day: "", month: "", year: "" };
}
 
const { timestamp, url: archiveURL } = json["archived_snapshots"]["closest"];
const [_, year, month, day] = timestamp.match(/(\d{4})(\d{2})(\d{2})/);
 
return { url: archiveURL, day, month, year };
} catch (e) {
console.log("Could not fetch archive url: " + e);
return { url: "", day: "", month: "", year: "" };
}
}
 
async function saveButtonClicked(button) {
if (!currentlySelectedRef.item.replace && saveReference() !== currentlySelectedRef.item.wikitext) {
refsSaved++;
Line 602 ⟶ 607:
}
currentlySelectedRef.item.replace = saveReference();
const refText = currentlySelectedRef.refElem.querySelector(".reference-text");
button.parentElement.parentElement.remove();
refText.innerHTML = "Loading...";
refText.innerHTML = await wikitextToHTML(currentlySelectedRef.item.replace);
refText.children[0].style.display = "inline";
}
 
Line 634 ⟶ 643:
___location.reload();
}
 
async function wikitextToHTML(wikitext) {
let deferred = $.Deferred();
$.post("https://en.wikipedia.org/api/rest_v1/transform/wikitext/to/html",
"wikitext=" + encodeURIComponent(wikitext) + "&body_only=true",
function (data) {
deferred.resolve(data);
}
);
return deferred;
}