jinlin
2024-09-06 3ecb68c427a627ad8e90d8c555655e7724be2d96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!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>
<br>
<a href="../../images/logo.png">logo.png</a>
</body>
 
<script>
 
$('a').click(function() {
  var href = this.href;
  var target = window.parent;
  // 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) {
 
    var meta_str = JSON.stringify({
      name: $(this).text(),
      id: href
    });
    target.postMessage(meta_str, "*");
  
    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 = "";
      }
      target.postMessage('|' + href + '|' + dataurl, "*");
    }
    img.src = href;
  } else {
    // Send metadata (also indicates file is about to be sent)
    var meta_str = JSON.stringify({
      name: $(this).text(),
      id: href
    });
    target.postMessage(meta_str, "*");
    // Do ajax request for image's href value
    $.get(href, function(data) {
      data = '|' + href + '|' + data;
      // This is where the magic happens!
      target.postMessage(data, "*");
      
    }, 'html'); // 'html' is necessary to keep returned data as a string
  }
  return false;
});
 
</script>