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

As of 2015-07-06. See the latest version.

// ==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);