Chiron / Bitbucket Pull Request Total Lines Changed

// ==UserScript==
// @name         Bitbucket Pull Request Total Lines Changed
// @description  Fairly self-explanatory, see https://i.imgur.com/pfYxI5r.png
// @version      1.0.3
// @license      MIT
// @author       Chiron
// @match        https://bitbucket.org/*/pull-requests/*/diff
// @downloadURL  https://openuserjs.org/src/scripts/Chiron/Bitbucket_Pull_Request_Total_Lines_Changed.user.js
// @updateURL    https://openuserjs.org/meta/Chiron/Bitbucket_Pull_Request_Total_Lines_Changed.meta.js
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  function probeDiffView() {
    if (document.querySelectorAll('#pullrequest-diff').length === 0) {
      window.requestAnimationFrame(probeDiffView);
    }
    else {
      sumLines();
    }
  }

  function sumOfNodeList(nodeList) {
    let count = 0;
    for (const node of nodeList) {
      count += Math.abs(node.innerHTML.trim());
    }
    return count;
  }

  function sumLines() {
    const changelist = document.querySelector('#commit-files-summary');
    const addedNodes = changelist.querySelectorAll('span.lines-added');
    const removedNodes = changelist.querySelectorAll('span.lines-removed');

    const added = sumOfNodeList(addedNodes);
    const removed = sumOfNodeList(removedNodes);
    const total = added - removed;
    const totalPrefix = total < 0 ? '-' : '+';

    changelist.insertAdjacentHTML('beforeend',
      `<li>
         <div class="commit-file-diff-stats">
           <span class="lines-added">+${added}</span>
           <span class="lines-removed">-${removed}</span>
         </div>
         All files (${totalPrefix}${Math.abs(total)} lines total)
      </li>`
    )
  }

  probeDiffView();

})();