Skip to content

Instantly share code, notes, and snippets.

@winkerVSbecks
Created September 10, 2019 13:15
Show Gist options
  • Save winkerVSbecks/8df14f049b47d9edec88b5c1fc2dcb11 to your computer and use it in GitHub Desktop.
Save winkerVSbecks/8df14f049b47d9edec88b5c1fc2dcb11 to your computer and use it in GitHub Desktop.
var svg = document.querySelector('.blocklySvg');
//get svg source.
var serializer = new XMLSerializer();
var source = serializer.serializeToString(svg);
//add name spaces.
if(!source.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/)){
source = source.replace(/^<svg/, '<svg xmlns="http://www.w3.org/2000/svg"');
}
if(!source.match(/^<svg[^>]+"http\:\/\/www\.w3\.org\/1999\/xlink"/)){
source = source.replace(/^<svg/, '<svg xmlns:xlink="http://www.w3.org/1999/xlink"');
}
//add xml declaration
source = '<?xml version="1.0" standalone="no"?>\r\n' + source;
//convert svg source to URI data scheme.
var url = "data:image/svg+xml;charset=utf-8,"+encodeURIComponent(source);
//set url value to a element's href attribute.
var downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = "newesttree.svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment