1 OSM.downloadBlob = function (blob, filename) {
2 const url = URL.createObjectURL(blob);
3 const a = document.createElement("a");
6 document.body.appendChild(a);
8 document.body.removeChild(a);
9 URL.revokeObjectURL(url);
12 OSM.getTurboBlobHandler = function (filename) {
13 function handleExportSuccess({ fetchResponse }) {
14 fetchResponse.response.blob()
15 .then(blob => OSM.downloadBlob(blob, filename))
16 .catch(() => notifyExportFailure("(blob error)"));
19 function handleExportError({ error, fetchResponse }) {
22 fetchResponse.responseText.then(extractTextFromHTML)
24 .then(notifyExportFailure)
25 .catch(() => notifyExportFailure("(unknown)"));
28 function extractTextFromHTML(htmlString) {
29 const parser = new DOMParser();
30 const doc = parser.parseFromString(htmlString, "text/html");
31 return doc.body ? doc.body.textContent.trim() : "(unknown)";
34 function notifyExportFailure(reason) {
35 OSM.showAlert(OSM.i18n.t("javascripts.share.export_failed_title"), reason);
38 return function ({ detail }) {
40 handleExportSuccess(detail);
42 handleExportError(detail);
47 OSM.turboHtmlResponseHandler = function (event) {
48 const response = event.detail.fetchResponse.response;
49 const contentType = response.headers.get("content-type");
50 if (!response.ok && contentType?.includes("text/html")) {
51 // Prevent Turbo from replacing the current page with an error HTML response
52 // from the export endpoint
53 event.preventDefault();
54 event.stopPropagation();