Toggl-Button TeamWork

Toggl (see http://toggl.com) is an online time tracking platform and this user script integrates it so that you can easily start and stop time entries and quickly recognize which task is currently active. More details can be found at https://github.com/jurgenhaas/toggl-button-greasemonkey

当前为 2015-07-06 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Toggl-Button TeamWork
// @namespace   https://github.com/jurgenhaas/toggl-button-greasemonkey
// @version     1.0
// @include     http*://*.teamwork.com/*
// @grant       GM_xmlhttpRequest
// @grant       GM_addStyle
// @grant       GM_getResourceText
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_info
// @grant       GM_registerMenuCommand
// @require     https://greasyfork.org/scripts/2670-toggllibrary/code/TogglLibrary.js
// @resource    togglStyle https://raw.githubusercontent.com/jurgenhaas/toggl-button-greasemonkey/v1.1/TogglLibrary.css
// @description Toggl (see http://toggl.com) is an online time tracking platform and this user script integrates it so that you can easily start and stop time entries and quickly recognize which task is currently active. More details can be found at https://github.com/jurgenhaas/toggl-button-greasemonkey
// ==/UserScript==

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
  target = document.querySelector('#mainContent'),
  bodyTarget = document.querySelector('body'),
  stopTB, stopProcessed,
  config = { attributes: true, childList: true, characterData: true },
  observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      if (mutation.type === 'childList') {
        // TODO: if none of the mutations is a task, then remove the toggle element if visible.
        var task = document.querySelector('#Task');
        if (task !== null) {
          var tb = new TogglButtonGM('#Task', function (elem) {
            var taskDetail = document.querySelector('.taskDetailHolder');
            if (taskDetail !== null) {
              var taskID = taskDetail.id.substr(13),
                taskDetailTarget = taskDetail.querySelector('div'),
                taskDetailProcessed = false,
                innerObserver = new MutationObserver(function (mutations) {
                  mutations.forEach(function (mutation) {
                    if (mutation.type === 'childList' && !taskDetailProcessed) {
                      var titleElem = document.querySelector('.taskDetailsName span');
                      if (titleElem !== null) {
                        innerObserver.disconnect();
                        taskDetailProcessed = true;
                        var tb = new TogglButtonGM('#Task', function (elem) {
                          var description, projectIds = [],
                            projectElem = document.querySelector('#projectName'),
                            linkElem = document.querySelector('#ViewTaskSidebar .blue2.ql');

                          description = taskID + " " + titleElem.textContent.trim();

                          if (projectElem !== null) {
                            projectIds.push(projectElem.textContent.trim());
                          }
                          if (linkElem !== null) {
                            projectIds.push(linkElem.textContent.trim());
                          }

                          return {
                            className: 'teamwork',
                            description: description,
                            projectIds: projectIds
                          };
                        });

                        document.querySelector('.startTimer button').addEventListener('click', function () {
                          tb.clickLinks();
                        });
                      }
                    }
                  });
                });
              innerObserver.observe(taskDetailTarget, config);
            }
          });
        }
      }
    });
  }),
  bodyObserver = new MutationObserver(function (mutations) {
    if (stopTB == null) {
      stopTB = new TogglButtonGM();
      stopProcessed = false;
    }
    mutations.forEach(function (mutation) {
      if (mutation.type === 'childList') {
        var stopElem = document.querySelector('#timerBar');
        if (stopElem !== null && !stopProcessed) {
          stopProcessed = true;
          stopElem.querySelector('.timer-log-time, .timer-body .btn-success').addEventListener('click', function () {
            stopTB.stopTimeEntry();
          });
        }
      }
    });
  });

observer.observe(target, config);
bodyObserver.observe(bodyTarget, config);