2010-07-14 18:08:22 +00:00
|
|
|
<!doctype html>
|
|
|
|
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<h1>Select an image:</h1>
|
|
|
|
<a href="smiley.svg">smiley.svg</a>
|
2010-07-16 18:33:48 +00:00
|
|
|
<br>
|
|
|
|
<a href="../../images/logo.png">logo.png</a>
|
2010-07-14 18:08:22 +00:00
|
|
|
</body>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
$('a').click(function() {
|
2010-09-17 17:13:15 +00:00
|
|
|
var href = this.href;
|
2010-07-14 18:08:22 +00:00
|
|
|
|
2010-07-16 18:33:48 +00:00
|
|
|
// Convert Non-SVG images to data URL first
|
|
|
|
// (this could also have been done server-side by the library)
|
|
|
|
if(this.href.indexOf('.svg') === -1) {
|
2010-09-17 17:13:15 +00:00
|
|
|
|
|
|
|
var meta_str = JSON.stringify({
|
|
|
|
name: $(this).text(),
|
|
|
|
id: href
|
|
|
|
});
|
|
|
|
window.top.postMessage(meta_str, "*");
|
|
|
|
|
2010-07-16 18:33:48 +00:00
|
|
|
var img = new Image();
|
|
|
|
img.onload = function() {
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
canvas.width = this.width;
|
|
|
|
canvas.height = this.height;
|
|
|
|
// load the raster image into the canvas
|
|
|
|
canvas.getContext("2d").drawImage(this,0,0);
|
|
|
|
// retrieve the data: URL
|
|
|
|
try {
|
|
|
|
var dataurl = canvas.toDataURL();
|
|
|
|
} catch(err) {
|
|
|
|
// This fails in Firefox with file:// URLs :(
|
|
|
|
alert("Data URL conversion failed: " + err);
|
|
|
|
var dataurl = "";
|
|
|
|
}
|
2010-09-17 17:13:15 +00:00
|
|
|
window.top.postMessage('|' + href + '|' + dataurl, "*");
|
2010-07-16 18:33:48 +00:00
|
|
|
}
|
2010-09-17 17:13:15 +00:00
|
|
|
img.src = href;
|
2010-07-16 18:33:48 +00:00
|
|
|
} else {
|
2010-09-17 17:13:15 +00:00
|
|
|
// Send metadata (also indicates file is about to be sent)
|
|
|
|
var meta_str = JSON.stringify({
|
|
|
|
name: $(this).text(),
|
|
|
|
id: href
|
|
|
|
});
|
|
|
|
window.top.postMessage(meta_str, "*");
|
2010-07-16 18:33:48 +00:00
|
|
|
// Do ajax request for image's href value
|
2010-09-17 17:13:15 +00:00
|
|
|
$.get(href, function(data) {
|
|
|
|
data = '|' + href + '|' + data;
|
2010-07-16 18:33:48 +00:00
|
|
|
// This is where the magic happens!
|
|
|
|
window.top.postMessage(data, "*");
|
|
|
|
|
|
|
|
}, 'html'); // 'html' is necessary to keep returned data as a string
|
|
|
|
}
|
2010-07-14 18:08:22 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|