szaniec.marcin / Gitlab next previous buttons

// ==UserScript==
// @name         Gitlab next previous buttons
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Script to add next/previous buttons on gitlab merge request
// @author       Marcin Szaniec
// @match        *://*/*/merge_requests/*
// @grant GM_setValue
// @grant GM_getValue
// @require http://code.jquery.com/jquery-latest.js
// @license MIT
// ==/UserScript==

var currentLink;
var prevLink;
var nextLink;

function next(){
    window.location.href = nextLink.href;
}

function prev(){
   window.location.href = prevLink.href;
}

function addButton(text, onclick, selector, cssObj) {
    let button = document.createElement('button'), btnStyle = button.style
    document.getElementById(selector).appendChild(button)
    button.innerHTML = text
    button.onclick = onclick
    button.className  = "btn";
    return button
}

$(document).ready(function()
{

var checkExist = setInterval(function() {
   if(GM_getValue("links") && $(location).attr('href').includes("diff"))
        clearInterval(checkExist);
   if ($('a.commit-row-message').length) {
      var links = $('a.commit-row-message:not(.gfm-issue)').map(function(){
          var commitMsg = $(this).parent().find("pre").text();
          console.log(commitMsg);
          if(!commitMsg)
              commitMsg = $(this).html();
          return { href : $(this).attr('href') , message: commitMsg }
      });
      GM_setValue("links" ,  JSON.stringify(links));
      clearInterval(checkExist);
   }
}, 100); // check every 100ms

if($(location).attr('href').includes('commit_id')){
    var links = JSON.parse(GM_getValue("links"));
    for (var i = 0; i < links.length; i++)
    {
        if($(location).attr('href').includes(links[i].href))
        {
            if(i > 0){
                nextLink = links[i-1];
            }
            if(i !== links.length-1)
            {
                prevLink = links[i+1];
            }
            currentLink = links[i];
            break;
        }
    }
    $(".issuable-sidebar").append('<div id="popupButtons" style="width:250px; height:auto;"><span style="color:green">' + currentLink.message +'</span><div id="nextPrevButtons"></div></div>')
    if(prevLink)
        addButton("Prev" , prev , 'nextPrevButtons');
    if(nextLink)
        addButton("Next" , next , 'nextPrevButtons');

}

});