<commit_before>(function() {
  var tweetListNode, loaderNode;

  cacheSelectors();
  setupEventListeners();
  getProfile();
  getTweets();

  function cacheSelectors() {
    tweetListNode = document.querySelector('.bmr-tweet-list');
    loaderNode = document.querySelector('.uil-squares-css');
  }

  function setupEventListeners() {
    tweetListNode.addEventListener('click', function(event) {
      var url = '';
      var target = event.target; // Clicked element

      if(target.tagName === 'A') {
        //For anchor elements open their link in a new window.

        event.preventDefault();
        url = target.href;
      } else {
        //Search upto li tag.

        while (target && target.parentNode !== tweetListNode) {
          target = target.parentNode; // If the clicked element isn't a direct child
          if(!target) { return; } // If element doesn't exist
        }

        if (target.tagName === 'LI'){
          var id = target.getAttribute('data-id');
          var username = target.getAttribute('data-username');

          if(id && username) {
            url = `https://twitter.com/${username}/status/${id}`;
          } else {
            getTweets();
            return;
          }

        }
      }

      window.open(url, '_blank');
    });
  }

  function getProfile() {
    fetchData('profile').then(function(profile) {
      document.querySelector('.bmr-header .avatar').setAttribute('src', profile.profile_image_url);
      document.querySelector('.bmr-header .author-name').innerHTML = `${profile.name} (@${profile.screen_name})`;
    });
  }

  function getTweets() {
    var domParser = new DOMParser();
    var fragment = document.createDocumentFragment();

    fetchData('tweets').then(function(response) {
      if(response.tweets.length === 0) {
        alert('No more tweets at the moment. Please try again after some time.');
      } else {
        response.tweets.forEach(function(tweet) {
          var tweetMarkup = tweet.html;

          var retweetStatusClass= 'hide';
          if(tweet.retweeted_status) {
            var retweetProfileName = tweet.user.name + '(@' + tweet.user.screen_name + ')';
            retweetStatusClass = 'show';
            tweet = tweet.retweeted_status;
          }

          var tweetId = tweet.id_str;
          var username = tweet.user.screen_name;
          var profileImage = tweet.user.profile_image_url;
          var profileName = tweet.user.name + '(@' + tweet.user.screen_name + ')';
          var tweetTime = moment.duration(tweet.created_at).humanize();

          var tweetNodeStr = `<li class="bmr-list-item" data-id="${tweetId}" data-username="${username}">

            <div class="bmr-retweet-status ${retweetStatusClass}">${retweetProfileName} Retweeted</div>

            <div class="bmr-tweet-author">
              <img class="avatar" src="${profileImage}"/>
              <h4 class="author-name">${profileName}</h4>
              <span class="time-difference">${tweetTime}</span>
            </div>

            <div class="bmr-tweet-text">${tweetMarkup}</div>
          </li>`;

          fragment.appendChild(domParser.parseFromString(tweetNodeStr, "text/html").body.firstChild);
        });

        tweetListNode.insertBefore(fragment, document.querySelector('.bmr-item-next'));
      }
    });
  }

  function fetchData(url, callback) {
    loaderNode.hidden = false;
    return fetch(url, {
      method: 'get',
      credentials: 'same-origin',
      headers: new Headers({accept: 'application/json'})
    })<commit_msg>Need to check for trailing slash or url for fetch will get messed up.<commit_after>(function() {
  var tweetListNode, loaderNode;

  cacheSelectors();
  setupEventListeners();
  getProfile();
  getTweets();

  function cacheSelectors() {
    tweetListNode = document.querySelector('.bmr-tweet-list');
    loaderNode = document.querySelector('.uil-squares-css');
  }

  function setupEventListeners() {
    tweetListNode.addEventListener('click', function(event) {
      var url = '';
      var target = event.target; // Clicked element

      if(target.tagName === 'A') {
        //For anchor elements open their link in a new window.

        event.preventDefault();
        url = target.href;
      } else {
        //Search upto li tag.

        while (target && target.parentNode !== tweetListNode) {
          target = target.parentNode; // If the clicked element isn't a direct child
          if(!target) { return; } // If element doesn't exist
        }

        if (target.tagName === 'LI'){
          var id = target.getAttribute('data-id');
          var username = target.getAttribute('data-username');

          if(id && username) {
            url = `https://twitter.com/${username}/status/${id}`;
          } else {
            getTweets();
            return;
          }

        }
      }

      window.open(url, '_blank');
    });
  }

  function getProfile() {
    fetchData('profile').then(function(profile) {
      document.querySelector('.bmr-header .avatar').setAttribute('src', profile.profile_image_url);
      document.querySelector('.bmr-header .author-name').innerHTML = `${profile.name} (@${profile.screen_name})`;
    });
  }

  function getTweets() {
    var domParser = new DOMParser();
    var fragment = document.createDocumentFragment();

    fetchData('tweets').then(function(response) {
      if(response.tweets.length === 0) {
        alert('No more tweets at the moment. Please try again after some time.');
      } else {
        response.tweets.forEach(function(tweet) {
          var tweetMarkup = tweet.html;

          var retweetStatusClass= 'hide';
          if(tweet.retweeted_status) {
            var retweetProfileName = tweet.user.name + '(@' + tweet.user.screen_name + ')';
            retweetStatusClass = 'show';
            tweet = tweet.retweeted_status;
          }

          var tweetId = tweet.id_str;
          var username = tweet.user.screen_name;
          var profileImage = tweet.user.profile_image_url;
          var profileName = tweet.user.name + '(@' + tweet.user.screen_name + ')';
          var tweetTime = moment.duration(tweet.created_at).humanize();

          var tweetNodeStr = `<li class="bmr-list-item" data-id="${tweetId}" data-username="${username}">

            <div class="bmr-retweet-status ${retweetStatusClass}">${retweetProfileName} Retweeted</div>

            <div class="bmr-tweet-author">
              <img class="avatar" src="${profileImage}"/>
              <h4 class="author-name">${profileName}</h4>
              <span class="time-difference">${tweetTime}</span>
            </div>

            <div class="bmr-tweet-text">${tweetMarkup}</div>
          </li>`;

          fragment.appendChild(domParser.parseFromString(tweetNodeStr, "text/html").body.firstChild);
        });

        tweetListNode.insertBefore(fragment, document.querySelector('.bmr-item-next'));
      }
    });
  }

  function fetchData(url, callback) {
    loaderNode.hidden = false;

    url = location.href.replace(/\/?$/, '/') + url;

    return fetch(url, {
      method: 'get',
      credentials: 'same-origin',
      headers: new Headers({accept: 'application/json'})
    })
{'fraction_non_alphanumeric': 0.14706700027800945, 'fraction_numerical': 0.0008340283569641367, 'mean_word_length': 2.469141755062681, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '2186780', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2070, 'n_words': 400}