I'm trying to access an SVG's contentDocument
(and change fill colors for specific paths, but that I don't have a problem with) via JavaScript. The SVG comes from user input. Whenever I try to access the contentDocument
I get the error:
(index):41 Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLObjectElement': Blocked a frame with origin "http://localhost" from accessing a cross-origin frame.
As you can see I'm using a local server the but results are the same when accessing the file locally. Here's my code:
document.getElementById("input-image").addEventListener("load", function () {
console.log("image loaded");
let svgDoc = this.contentDocument;
}, false);
var src = document.getElementById("file-input");
var target = document.getElementById("input-image");
var fr = new FileReader();
fr.onload = function (e) {
target.data = this.result;
};
src.addEventListener("change", function () {
fr.readAsDataURL(src.files[0]);
});
And the HTML:
<object data="" type="image/svg+xml" id="input-image" width="100%" height="100%"></object>
<input id="file-input" type="file" accept=".svg" />
And here's a jsFiddle.
I know this can be fixed by actually uploading the file to the server and then accessing it locally (as in, on the server) but is there a way to get this to work without introducting server side scripting and uploads?
file://
andhttp://localhost/
access. – traktor