<commit_before>var host = "https://upload.gyazo.com/api/upload/easy_auth";
var client_id = "df9edab530e84b4c56f9fcfa209aff1131c7d358a91d85cc20b9229e515d67dd";

function onClickHandler(info, tab) {

  if (info.menuItemId == "gyazo_image") {
    var image = new Image();
    var canvas =  document.createElement('canvas');
    var ctx = canvas.getContext("2d");
    image.crossOrigin = "Anonymous";
    const INCREMENT_SIZE = 5;
    var notification = {
      progress: 3,
      limit: 30,
      id: 'gyazo_notification_' + Date.now(),
      newTabId: null,
      progressIncrement: function(){
        this.progress = Math.min(this.progress + INCREMENT_SIZE, this.limit);
      }
    };
    chrome.notifications.create(notification.id,{
      type: "progress",
      title: "Uploading to Gyazo",
      message: "Uploading...",
      progress: notification.progress,
      iconUrl: "icon128.png",
      priority: 2
    },function(){})
    var timer_id = window.setInterval(function(){
      chrome.notifications.update(notification.id,{
        progress: notification.progress
      },function(){});
      notification.progressIncrement();
      if(notification.newTabId){
        chrome.tabs.get(notification.newTabId,function(newTab){
          if(newTab.status === 'complete'){
            chrome.notifications.update(notification.id,{
              title: "Finish Uploading",
              message: "Gyazo URL has been copied to your clipboard",
              progress: 100
            },function(){});
            window.clearInterval(timer_id);
          }
        })
      }
    },500);
    image.onload = function(){
      canvas.width = image.width;
      canvas.height = image.height;
      ctx.drawImage(image, 0, 0);
      $.ajax({
        type: 'POST',
        url:  host,
        data: {
          client_id: client_id,
          url: canvas.toDataURL(),
          title: tab.title,
          referer: tab.url
        },
        crossDomain: true})
      .done(function(data) {
          chrome.tabs.create({url:data.get_image_url, selected:false}, function(newTab){
            notification.limit = 80;
            notification.newTabId = newTab.id;
            var handler = function (tabId, changeInfo) {
              if (newTab.id == tabId && changeInfo.url) {
                saveToClipboard(changeInfo.url);
                chrome.tabs.onUpdated.removeListener(handler);
                notification.newTabId = tabId;
              }
            };
            chrome.tabs.onUpdated.addListener(handler);
          });
        })
        .fail(function(XMLHttpRequest, textStatus, errorThrown) {
          window.alert("Status: " + XMLHttpRequest.status + "\n Error: " + textStatus + "\n Message: " + errorThrown.message);
        });
    }
    image.src = info.srcUrl;
  }
}

chrome.contextMenus.onClicked.addListener(onClickHandler);

chrome.runtime.onInstalled.addListener(function() {
  chrome.contextMenus.create({
    "title": "Gyazo It",
    "id": "gyazo_image",
    "contexts": ["image"]
  });
});

function tabUpdateListener(tabId, changeInfo, tab){
  saveToClipboard(changeInfo.url);
}

function saveToClipboard(str) {<commit_msg>fetch images as dataUrl with XHR and FileReader<commit_after>var host = "https://upload.gyazo.com/api/upload/easy_auth";
var client_id = "df9edab530e84b4c56f9fcfa209aff1131c7d358a91d85cc20b9229e515d67dd";
const INCREMENT_SIZE = 5;
var notification = {
  progress: 3,
  limit: 30,
  id: 'gyazo_notification_' + Date.now(),
  newTabId: null,
  progressIncrement: function(){
    this.progress = Math.min(this.progress + INCREMENT_SIZE, this.limit);
  }
};

function postToGyazo(dataUrl,tab){
  $.ajax({
    type: 'POST',
    url:  host,
    data: {
      client_id: client_id,
      url: dataUrl,
      title: tab.title,
      referer: tab.url
    },
    crossDomain: true})
  .done(function(data) {
      chrome.tabs.create({url:data.get_image_url, selected:false}, function(newTab){
        notification.limit = 80;
        notification.newTabId = newTab.id;
        var handler = function (tabId, changeInfo) {
          if (newTab.id == tabId && changeInfo.url) {
            saveToClipboard(changeInfo.url);
            chrome.tabs.onUpdated.removeListener(handler);
            notification.newTabId = tabId;
          }
        };
        chrome.tabs.onUpdated.addListener(handler);
      });
    })
    .fail(function(XMLHttpRequest, textStatus, errorThrown) {
      window.alert("Status: " + XMLHttpRequest.status + "\n Error: " + textStatus + "\n Message: " + errorThrown.message);
    });
}

function onClickHandler(info, tab) {

  if (info.menuItemId == "gyazo_image") {
    var xhr = jQuery.ajaxSettings.xhr();
    xhr.open('GET', info.srcUrl, true);
    xhr.responseType = 'blob';
    xhr.onreadystatechange = function(){
      if(xhr.readyState === 4){
        var blob = xhr.response;
        var file_reader = new FileReader();
        file_reader.onload = function(e){
          postToGyazo(file_reader.result,tab);
        }
        file_reader.readAsDataURL(blob);
      }
    };
    xhr.send();
    chrome.notifications.create(notification.id,{
      type: "progress",
      title: "Uploading to Gyazo",
      message: "Uploading...",
      progress: notification.progress,
      iconUrl: "icon128.png",
      priority: 2
    },function(){})
    var timer_id = window.setInterval(function(){
      chrome.notifications.update(notification.id,{
        progress: notification.progress
      },function(){});
      notification.progressIncrement();
      if(notification.newTabId){
        chrome.tabs.get(notification.newTabId,function(newTab){
          if(newTab.status === 'complete'){
            chrome.notifications.update(notification.id,{
              title: "Finish Uploading",
              message: "Gyazo URL has been copied to your clipboard",
              progress: 100
            },function(){});
            window.clearInterval(timer_id);
          }
        })
      }
    },500);
  }
}

chrome.contextMenus.onClicked.addListener(onClickHandler);

chrome.runtime.onInstalled.addListener(function() {
  chrome.contextMenus.create({
    "title": "Gyazo It",
    "id": "gyazo_image",
    "contexts": ["image"]
  });
});

function tabUpdateListener(tabId, changeInfo, tab){
  saveToClipboard(changeInfo.url);
}

function saveToClipboard(str) {
{'fraction_non_alphanumeric': 0.13381726368944297, 'fraction_numerical': 0.017042764715164905, 'mean_word_length': 2.607285145133751, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1227255', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1933, 'n_words': 344}