JIRA Links

Turn JIRA ticket names into links

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         JIRA Links
// @author       Nik Rolls
// @description  Turn JIRA ticket names into links
// @match        *://*/*
// @require      https://greasyfork.org/scripts/395037-monkeyconfig-modern/code/MonkeyConfig%20Modern.js?version=764968
// @grant        GM_getMetadata
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @grant        GM_addStyle
// @namespace    https://greasyfork.org/users/503103
// @version 0.0.1.20200429221337
// ==/UserScript==

(function() {
  'use strict';
  
  const cfg = new MonkeyConfig({
    menuCommand: true,
    params: {
      domain: {
        label: "Your JIRA domain",
        type: 'text'
      },
      prefix: {
        label: "Ticket name prefix<br/><small>eg: if your ticket IDs look like<br/>'AB-1234', the prefix would be<br/>'AB'.</small>",
        type: 'text'
      }
    }
  });
  
  let interval = null;
  
  document.addEventListener('visibilitychange', detectVisibility);
  detectVisibility();
  
  function detectVisibility() {
    if (document.visibilityState === 'visible') {
      startWatching();
    } else {
      stopWatching();
    }
  }
  
  function startWatching() {
    if (cfg.get('domain') && cfg.get('prefix')) {
      augmentLinks();
      interval = window.setInterval(augmentLinks, 1000);
    }
  }
  
  function stopWatching() {
    if (interval) {
      window.clearInterval(interval);
    }
  }
  
  function augmentLinks() {
    const domain = cfg.get('domain').toLowerCase();
    const prefix = cfg.get('prefix').replace(/[^A-Za-z]/g, '').toUpperCase();
    const items = document.evaluate(`/html/body//*[not(self::style or self::script or self::a or self::input or self::textarea or boolean(@contenteditable)) and 
                                         text()[contains(.,"${prefix}-") or contains(.,"${prefix}_")]]`, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i = 0; i < items.snapshotLength; i++) {
      const item = items.snapshotItem(i);
      console.log(item);
      const pattern = new RegExp(`(?<!<a[^>]+>)\\b(${prefix}(?:-|_)(\\d+))([\\b\\W])`, 'g');
      if (!item.closest('a') && !item.closest('[contenteditable]') && item.innerHTML.match(pattern)) {
        item.innerHTML = item.innerHTML.replace(pattern, `<a href="https://${domain}/secure/QuickSearch.jspa?searchString=$2">$1</a>$3`);
      }
    }
  }
})();