Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save/ Grab svg from website to local machine or convert to image

Tags:

javascript

svg

Is there a way how I can grab html svg from a webpage using js console in Chrome e.g.? Svg code is wrapped with svg tag. And is it possible to save svg to local machine? or convert it to the image? and is it possible to import svg data ( e.g. charts/ graphs ) to google docs or excel?

like image 769
NGix Avatar asked Oct 22 '22 01:10

NGix


1 Answers

You have access to the complete dom and the js objects provided by the browser ( eg. a xpath processor) as well as external libraries.

so all you need is a unique identification of the svg element in question.

start off importing jquery on the fly and selecting all svg elements on the page. this assumes that you have loaded the page you are investigating and have opened the console. the following statements should be entered at the command prompt:

var e = document.createElement('script');
e.setAttribute('type','text/javascript'); 
e.setAttribute('src','http://code.jquery.com/jquery-1.10.2.min.js'); 
(document.getElementsByTagName('head'))[0].appendChild(e);

$('svg');

the last command will provide you with a dom fragment containing the svg element.

the preceding vanilla javascript commands add a script element referencing the jquery library to the current web page. jquery simplifies the dom handling substantially, however, it is not necessary strictly speaking (see fr32c's answer).

if you're done, choose the 'reveal in elements panel' entry of the context menu of the console output just generated. you'll be redirected to the element inside a folding hierarchy representation of the page dom. choose 'copy as html' from the context menu of the svg element selected. save the clipboard data as a svg file.

Note

in chrome (29) and opera (12), jquery is imported in the console, which reduces the element query to $('svg') (thanks to Ir0nm for this information).

like image 200
collapsar Avatar answered Oct 23 '22 18:10

collapsar