Refresh Button for Dashboard

Adds button that reloads all widgets on a dashboard.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Refresh Button for Dashboard
// @author       Qteb
// @match        https://*.myjetbrains.com/youtrack/dashboard*
// @description  Adds button that reloads all widgets on a dashboard.
// @version 0.0.1.20170302134951
// @namespace https://greasyfork.org/users/106502
// ==/UserScript==

function refreshWidgets() {
    console.log('Updating...');
    $('div[react-value-glyph="#refresh"]').each( function() {
        $(this).click();
    });
}

(function() {
    'use strict';

    var vis = (function(){
        var stateKey, eventKey, keys = {
            hidden: "visibilitychange",
            webkitHidden: "webkitvisibilitychange",
            mozHidden: "mozvisibilitychange",
            msHidden: "msvisibilitychange"
        };
        for (stateKey in keys) {
            if (stateKey in document) {
                eventKey = keys[stateKey];
                break;
            }
        }
        return function(c) {
            if (c) document.addEventListener(eventKey, c);
            return !document[stateKey];
        };
    })();

    vis(function(){
        if(vis()) {
            refreshWidgets();
        }
    });

    // add refresh button
    setTimeout(function () {
        // while button does not exists
        while ($('.ring-button__content:contains("Reload All")').text().length === 0) {
            $('.dashboard-page__toolbar-buttons')
                .prepend($('<button>').addClass("ring-button ring-button_default reloadAll")
                         .prepend($('<span>').addClass("ring-button__content")
                                  .prepend($('<span>').text('Reload All'))));
        }
        // click refresh button action
        $('.ring-button__content:contains("Reload All")').click(function () {
            refreshWidgets();
        });
    }, 1000);
})();