jason.henriksen / PullRequestCommentNavigator

// ==UserScript==
// @name         PullRequestCommentNavigator
// @description Adds buttons in the upper left to jump to the next PR Comment
// @license MIT
// @copyright 2019, jason.henriksen
// @match bitbucket.org/*/pull-requests/*
// ==/UserScript==

(function() {
    'use strict';

    window.sqaCurPR=-1;

    function nextComment(){
        var comBlockList = document.querySelectorAll('[class*="comment-thread-container"]');
        window.sqaCurPR++;
        if(window.sqaCurPR>=comBlockList.length){window.sqaCurPR=comBlockList.length-1;}
        comBlockList[window.sqaCurPR].scrollIntoView();
        console.log(window.sqaCurPR);
    }

    function prevComment(){
        var comBlockList = document.querySelectorAll('[class*="comment-thread-container"]');
        window.sqaCurPR--;
        if(window.sqaCurPR<0){window.sqaCurPR=0;}
        comBlockList[window.sqaCurPR].scrollIntoView();
        console.log(window.sqaCurPR);
    }


    function addNextPrevBtn() {
        var b1 = document.createElement('button');
        b1.innerHTML = '<<';
        b1.style.position='fixed';
        b1.style.top='0px';
        b1.style.left='0px';
        b1.style.zIndex='1000';
        b1.onclick=prevComment;

        var b2 = document.createElement('button');
        b2.innerHTML = '>>';
        b2.style.position='fixed';
        b2.style.top='0px';
        b2.style.left='35px';
        b2.style.zIndex='1000';
        b2.onclick=nextComment;

        document.querySelector('[id="adg3-navigation"]').appendChild(b1);
        document.querySelector('[id="adg3-navigation"]').appendChild(b2);

    }
    addNextPrevBtn();
})();