Sort JIRA comments.

Force JIRA comments to be sorted in the order you prefer.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sort JIRA comments.
// @namespace    https://github.com/skehlet/jira-sort-comments-tampermonkey
// @version      0.7
// @description  Force JIRA comments to be sorted in the order you prefer.
// @author       @skehlet (Steve Kehlet)
// @contributor  @saurabhere
// @contributor  @catholicon
// @match        https://jira.atlassian.com/browse/*
// ... include more match statements here, including your private JIRA repos
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var desiredSortOrder = 'asc'; // change me to 'desc' if you prefer

    // apparently sometimes $ isn't available and jQuery is there (e.g. issues.apache.org/jira)
    $ = $ || jQuery;

    // some jira setups (e.g. issues.apache.org/jira) have sort toggle button as
    // class="icon icon-sort-down ajax-activity-content" OR class="icon icon-sort-up ajax-activity-content"
    // depending on the context. So, using those selectors too.
    $('.issue-activity-sort-link')
    .add($('.icon-sort-down.ajax-activity-content'))
    .add($('.icon-sort-up.ajax-activity-content'))
    .each(function () {
        var sortLink = $(this);
        // the link shows the order that will result if you click on it
        if (sortLink.attr('data-order') == desiredSortOrder || sortLink.attr('href').endsWith(desiredSortOrder)) {
            // use .issue-view if present (there's a sidebar and scrollable frame to the right), otherwise window
            var selector = $('.issue-view').length > 0 ? '.issue-view' : window;
            var scrollPos = $(selector).scrollTop();
            // scroll back to where we were after clicking
            $(selector).one('scroll', function () {
                $(selector).scrollTop(scrollPos);
            });
            sortLink.click();
        }
    });
})();