Fix dynamic img and form

This commit is contained in:
Adrian Vollmer 2022-09-26 17:42:25 +02:00
parent c6c2ffb4a8
commit 6c82664001

View File

@ -89,31 +89,40 @@ var fix_link = function(a) {
} }
}; };
var fix_form = function(form) {
var href = form.getAttribute('action');
if (is_virtual(href) && form.getAttribute('method').toLowerCase() == 'get') {
form.addEventListener('submit', virtual_click);
}
};
var fix_forms = function() { var fix_forms = function() {
Array.from(document.querySelectorAll("form")).forEach( form => { Array.from(document.querySelectorAll("form")).forEach( form => {
var href = form.getAttribute('action'); fix_form(form);
if (is_virtual(href) && form.getAttribute('method').toLowerCase() == 'get') {
form.addEventListener('submit', virtual_click);
}
}); });
}; };
var embed_img = function() { var embed_img = function(img) {
Array.from(document.querySelectorAll("img")).forEach( img => { if (img.hasAttribute('src')) {
if (img.hasAttribute('src')) { const src = img.getAttribute('src');
const src = img.getAttribute('src'); if (is_virtual(src)) {
if (is_virtual(src)) { var path = normalize_path(src);
var path = normalize_path(src); const file = retrieve_file(path);
const file = retrieve_file(path); // TODO handle mime type
// TODO handle mime type if (file.startsWith('<svg')) {
if (file.startsWith('<svg')) { img.setAttribute('src', "data:image/svg+xml;charset=utf-8;base64, " + btoa(file));
img.setAttribute('src', "data:image/svg+xml;charset=utf-8;base64, " + btoa(file)); } else {
} else { img.setAttribute('src', "data:image/png;base64, " + file);
img.setAttribute('src', "data:image/png;base64, " + file); }
}
};
}; };
};
};
var embed_imgs = function() {
Array.from(document.querySelectorAll("img")).forEach( img => {
embed_img(img);
}); });
}; };
@ -166,7 +175,7 @@ var normalize_path = function(path) {
var fix_document = function() { var fix_document = function() {
embed_js(); // This might change the DOM, so do this first embed_js(); // This might change the DOM, so do this first
embed_css(); embed_css();
embed_img(); embed_imgs();
fix_links(); fix_links();
fix_forms(); fix_forms();
}; };
@ -205,7 +214,10 @@ const observer = new MutationObserver((mutationList) => {
fix_link(a); fix_link(a);
}); });
Array.from(mutation.target.querySelectorAll("img")).forEach( img => { Array.from(mutation.target.querySelectorAll("img")).forEach( img => {
// embed_img TODO embed_img(img);
});
Array.from(mutation.target.querySelectorAll("form")).forEach( form => {
fix_form(form);
}); });
} }
}); });