Show all Jira 9 comments (and more), in chronological order

Clicks all 'show more' buttons, and (shift-)clicks 'oldest first'. Works on Jira 9.4.5. Inspired by https://greasyfork.org/scripts/432731 (no external dependencies).

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Show all Jira 9 comments (and more), in chronological order
// @namespace   https://greasyfork.org/users/1047370
// @description Clicks all 'show more' buttons, and (shift-)clicks 'oldest first'. Works on Jira 9.4.5. Inspired by https://greasyfork.org/scripts/432731 (no external dependencies).
// @include     https://jira.*
// @include     http://jira.*
// @match       https://jira.*
// @match       http://jira.*
// @version     0.2
// @author      Marnix Klooster <[email protected]>
// @copyright   public domain
// @license     public domain
// @homepage    https://greasyfork.org/scripts/472161
// @grant       none
// ==/UserScript==

// Configuration options
// ---------------------
const forceSortOrder = true; // whether or not to force the sort order at page load
const useOldestFirstSorting = true; // if forcing sort order, whether to use 'oldest first' == 'asc'
// END Configuration options

(function() {
    var theInterval = null;
    var warnedMultipleSortButtons = false;
    var clickedSortorderButton = false;
    var sortOrderInFinalState = false;
    var clickedShowmoreButton = null;
    var tabSwitchEventHandlerInstalled = false;

    function start() {
        if (theInterval) {
            console.log(`SOMETHING WENT WRONG.  Ignoring this call to start().`);
            return;
        }

        theInterval = setInterval(function() {
            // make sure sort order is as desired (direction determined by useOldestFirstSorting)
            if (forceSortOrder && !sortOrderInFinalState) {
                var sortButtons = document.querySelectorAll('button#sort-button');
                if (sortButtons.length > 1) {
                    if (!warnedMultipleSortButtons) {
                        console.log(`WARN: ${b.length} sort buttons found, will not try to change sort order`);
                        warnedMultipleSortButtons = true;
                    }
                } else if (sortButtons.length == 0) {
                    // retry later
                } else {
                    if (sortButtons[0].getAttribute('data-order') == (useOldestFirstSorting ? 'asc': 'desc')) {
                        if (!clickedSortorderButton) {
                            console.log(`Clicking the sort order button to switch to '${sortButtons[0].getAttribute('data-order')}'...`);
                            sortButtons[0].click();
                            clickedSortorderButton = true;
                            return;
                        }
                        console.log(`waiting for sort order change`);
                        // don't do anything else until we know the sort order is as we want it to be
                        return;
                    }
                }
            }

            // click any 'show more' button in sight...
            var showmoreButtons = document.querySelectorAll('button.show-more-tab-items');
            if (clickedShowmoreButton) {
                for (var b of showmoreButtons) {
                    if (b.isEqualNode(clickedShowmoreButton)) {
                        console.log(`waiting for last "show more..." click to have been handled`);
                        return;
                    }
                }
                clickedShowmoreButton = null;
                console.log(`wait one more round, just to be certain, before we potentially click other "show more..." buttons`);
                return;
            }
            if (showmoreButtons.length > 0) {
                clickedShowmoreButton = showmoreButtons[0];
                console.log(`Shift-clicking the button marked "${clickedShowmoreButton.innerText}"...`);
                const shiftClickEvent = document.createEvent("Event");
                shiftClickEvent.initEvent("click", true, true);
                shiftClickEvent.shiftKey = true;
                clickedShowmoreButton.dispatchEvent(shiftClickEvent);
                return;
            }

            // LATER: Enable the following code, once we know how to detect a tab switch
            //function tabSwitchEventHandler() {
            //    console.log(`tab bar may have been clicked, restart if needed...`);
            //    if (!theInterval) {
            //        console.log(`...restarting`);
            //        start();
            //    }
            //}
            //
            //if (!tabSwitchEventHandlerInstalled) {
            //    // TODO: somewhere register tabSwitchEventHandler...
            //    tabSwitchEventHandlerInstalled = true;
            //}
            //
            //console.log(`Everything is at it should be now; stop, and restart the interval on tab switch.`);
            //clearInterval(theInterval);
            //theInterval = null;
        }, 1000);
    }

    start();
})();