xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
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
/* globals jQuery */
const $ = jQuery;
$('a').click(function () {
  const {href} = this;
  const target = window.parent;
  const post = (message) => {
    // Todo: Make origin customizable as set by opening window
    // Todo: If dropping IE9, avoid stringifying
    target.postMessage(JSON.stringify({
      namespace: 'imagelib',
      ...message
    }), '*');
  };
  // Convert Non-SVG images to data URL first
  // (this could also have been done server-side by the library)
  // Send metadata (also indicates file is about to be sent)
  post({
    name: $(this).text(),
    id: href
  });
  if (!href.includes('.svg')) {
    const img = new Image();
    img.onload = function () {
      const 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
      let data;
      try {
        data = canvas.toDataURL();
      } catch (err) {
        // This fails in Firefox with `file:///` URLs :(
        alert('Data URL conversion failed: ' + err);
        data = '';
      }
      post({href, data});
    };
    img.src = href;
  } else {
    // Do ajax request for image's href value
    $.get(href, function (data) {
      post({href, data});
    }, 'html'); // 'html' is necessary to keep returned data as a string
  }
  return false;
});