text: function getSyncScriptParams(name) {
    var scripts = document.getElementsByTagName('script');
    var lastScript = scripts[scripts.length-1];
    var scriptName = lastScript;
    return {baseurl: scriptName.getAttribute("data-baseurl"),
      udocname: scriptName.getAttribute("data-udocname")}
  }
  var scparams = getSyncScriptParams("data-baseurl");
  var baseurl = scparams.baseurl;
  var udocname = scparams.udocname;
  require('jquery-ui/themes/base/core.css');
  require('jquery-ui/themes/base/draggable.css');
  require('jquery-ui/themes/base/resizable.css');
  require('jquery-ui/themes/base/theme.css');
  var $ = require('jquery');
  var draggable = require('jquery-ui/ui/widgets/draggable');
  var resizable = require('jquery-ui/ui/widgets/resizable');
  var html2canvas  =require('html2canvas');
  console.log("baseurl="+baseurl);
  if(!HTMLCanvasElement.prototype.toBlob){
    HTMLCanvasElement.prototype.toBlob = function(callback, type, encoderOptions){
      var dataurl = this.toDataURL(type, encoderOptions);
      var bstr = atob(dataurl.split(',')[1]), n = bstr.length, u8arr = new Uint8Array(n);
      while(n--){
        u8arr[n] = bstr.charCodeAt(n);
      }
      var blob = new Blob([u8arr], {type: type});
      callback.call(this, blob);
    };
  }
  $('<link>', {rel: 'stylesheet', type: 'text/css',
    href: "https://code.jquery.com/ui/1.12.0/themes/ui-lightness/jquery-ui.css"
  }).appendTo('head');
  var el=$('<div>').css({
    width: "200px",height:"200px",position:"absolute",
    border:"4px solid red",top:window.scrollY,left:window.scrollX,
    "background-color": "darkgray",opacity: 0.7,
    "z-index":"1000000"}).resizable().draggable().appendTo('body');
  var elimg =$("<img>",{src:"data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTguMS4xLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAv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NUM5MCw0MC40MjcsODguNDMzLDQxLjk5Myw4Ni41LDQxLjk5M3oiIGZpbGw9IiMwMDAwMDAiLz4KCTwvZz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8L3N2Zz4K"}).css({
    display: "block",
    margin: "0 auto"});
  el.append(elimg);
  $(document).keyup(function(e) {
    if (e.keyCode == 27) { // ESC
      el.remove();
    }
  });
  elimg.on('click',function(e){
    $(e.target).remove();
    var url = window.location.href;
    var posx=el.position().left+window.scrollX, width =el.width();
    var posy=el.position().top +window.scrollY, height=el.height();
    var wi = window.open('about:blank',"",'width=800,height=600');
    el.remove();
    // キャプチャ
    html2canvas(document.body
    ).then(function(canvas0) {
      var canvas = document.createElement("canvas");
      canvas.width =width;
      canvas.height=height;
      imgdata = canvas0.getContext("2d").getImageData(posx,posy,width,height);
      var ctx = canvas.getContext("2d");
      ctx.createImageData(imgdata);
      ctx.putImageData(imgdata,0,0);
      canvas.toBlob(
        function(blob){
          var formData = new FormData();
          formData.append("image[image_file]", blob, "capture.jpg");
          $.ajax(baseurl+'/sys/images/upload',{
            method:'POST', contentType: false, processData: false,dataType: 'json',
            data: formData
          }).done(function(json){
            var imgurl = json.url;
            url = "/sys/ud/"+udocname+"/new?"+
            jQuery.param({nomenu: "true",
             "udoc[capture_url]": imgurl,
             "udoc[user_agent]": navigator.userAgent,
             "udoc[pos_from][x]": posx,
             "udoc[pos_from][y]": posy,
             "udoc[pos_to][x]": posx+width,
             "udoc[pos_to][y]": posy+height,
             "udoc[url]": window.location.href});
            wi.location.href=baseurl+url;
          })
        },'image/jpeg',0.8
      );
    }).catch(function(err){
      alert(err);
    });
  });
  