<commit_before>// page.js injection start
var s = document.createElement('script');
    s.src = chrome.extension.getURL('page.js');
    s.onload = function() {
        this.parentNode.removeChild(this);
    };
(document.head||document.documentElement).appendChild(s);
// page.js injection end


var FileDownloader = {
  init: function() {
    FileDownloader.addDownloadTooltip();
    FileDownloader.bindPopState();

    chrome.runtime.sendMessage({key: "feature-1-enable"}, function(response) {
      if(typeof(response.result) === 'undefined' || response.result === true) {
        $(document).on('click', '.octicon-file-text', function(e) {
          FileDownloader.eventHandler(e);
        });
      } else {
        console.log('GitHub Mate click to download file is disabled, you can re-enable it in options page.');
      }
    });
  },

  addDownloadTooltip: function() {
    Array.prototype.slice.call(document.querySelectorAll('.octicon-file-text')).map(function(icon) {
      var td = icon.parentNode;
      td.classList.add('tooltipped', 'tooltipped-se');
      td.setAttribute('aria-label', 'Click to download');
    });
    // if in file detail page
    var rawUrlNode = document.querySelector('#raw-url');
    if (rawUrlNode && !document.querySelector('#download-btn')) {
      var fileName = document.querySelector('.breadcrumb .final-path').textContent;
      rawUrlNode.parentNode.innerHTML = "<a download='"+fileName+"' href='" + rawUrlNode.href + "' class='btn btn-sm' id='download-btn'>Download</a>"
          + "<a href='" + rawUrlNode.href.replace(/github.com(\/.+?\/.+?)\/raw/, 'rawgit.com$1') + "' class='btn btn-sm'>Rawgit</a>"
          + rawUrlNode.parentNode.innerHTML;

    }
  },

  bindPopState: function() {
    document.addEventListener('_pjax:end', function() {
      FileDownloader.addDownloadTooltip();
      ShowSize.init();
    }, false);
    window.onpopstate = function(event) {
      FileDownloader.addDownloadTooltip();
      ShowSize.init();
    };
  },

  eventHandler: function(event) {
    if (this.isFromListPage(event.currentTarget)) {
      var linkNode = event.currentTarget.parentNode.nextElementSibling.querySelector('a');
      var href = linkNode.href.replace('\/blob\/', '\/raw\/');
      this.downloadIt(href, linkNode.textContent);
    }
  },

  downloadIt: function(href, fileName) {
    var downloadNode = document.createElement('a');
    downloadNode.setAttribute('href', href);
    downloadNode.setAttribute('download', fileName);
    downloadNode.click();
    downloadNode = null;
  },

  isFromListPage: function(node) {
    return node.classList.contains('octicon-file-text') &&
      document.querySelector('.files') &&
      document.querySelector('.files').contains(node) &&
      document.querySelector('.file') === null;
  },
}

FileDownloader.init();
<commit_msg>fix file download, close #29<commit_after>// page.js injection start
var s = document.createElement('script');
    s.src = chrome.extension.getURL('page.js');
    s.onload = function() {
        this.parentNode.removeChild(this);
    };
(document.head||document.documentElement).appendChild(s);
// page.js injection end


var FileDownloader = {
  init: function() {
    FileDownloader.addDownloadTooltip();
    FileDownloader.bindPopState();

    chrome.runtime.sendMessage({key: "feature-1-enable"}, function(response) {
      if(typeof(response.result) === 'undefined' || response.result === true) {
        $(document).on('click', '.octicon-file-text', function(e) {
          FileDownloader.eventHandler(e);
        });
      } else {
        console.log('GitHub Mate click to download file is disabled, you can re-enable it in options page.');
      }
    });
  },

  addDownloadTooltip: function() {
    Array.prototype.slice.call(document.querySelectorAll('.octicon-file-text')).map(function(icon) {
      var td = icon.parentNode;
      td.classList.add('tooltipped', 'tooltipped-se');
      td.setAttribute('aria-label', 'Click to download');
    });
    // if in file detail page
    var rawUrlNode = document.querySelector('#raw-url');
    if (rawUrlNode && !document.querySelector('#download-btn')) {
      var fileName = document.querySelector('.breadcrumb .final-path').textContent;
      let btn = document.createElement('a');
      btn.setAttribute('class', 'btn btn-sm btn-primary tooltipped tooltipped-nw');
      btn.setAttribute('id', 'download-btn');
      btn.setAttribute('href', rawUrlNode.href);
      btn.setAttribute('aria-label', 'Click to download ' + fileName);
      btn.textContent = 'Download';
      rawUrlNode.parentNode.parentNode.prepend(btn);
    }
  },

  bindPopState: function() {
    document.addEventListener('_pjax:end', function() {
      FileDownloader.addDownloadTooltip();
      ShowSize.init();
    }, false);
    window.onpopstate = function(event) {
      FileDownloader.addDownloadTooltip();
      ShowSize.init();
    };
  },

  eventHandler: function(event) {
    if (this.isFromListPage(event.currentTarget)) {
      var linkNode = event.currentTarget.parentNode.nextElementSibling.querySelector('a');
      var href = linkNode.href.replace('\/blob\/', '\/raw\/');
      this.downloadIt(href, linkNode.textContent);
    }
  },

  downloadIt: function(href, fileName) {
    var downloadNode = document.createElement('a');
    downloadNode.setAttribute('href', href);
    downloadNode.setAttribute('download', fileName);
    downloadNode.click();
    downloadNode = null;
  },

  isFromListPage: function(node) {
    return node.classList.contains('octicon-file-text') &&
      document.querySelector('.files') &&
      document.querySelector('.files').contains(node) &&
      document.querySelector('.file') === null;
  },
}

FileDownloader.init();
{'fraction_non_alphanumeric': 0.16303400174367916, 'fraction_numerical': 0.0008718395815170009, 'mean_word_length': 3.527229676400947, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 51, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3155647', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1692, 'n_words': 284}