JIRA templates

Allows saving JIRA issue descriptions and comments for reuse.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name        JIRA templates
// @namespace   devsandbox.nfshost.com
// @description Allows saving JIRA issue descriptions and comments for reuse.
// @include     *.atlassian.net/*
// @version     1.1
// @grant       none
// ==/UserScript==
var modifyEditor = function(editor) {
    if (editor.classList.contains('templatized')) {return;}
    var textarea = editor.querySelector('textarea');
    var child = document.createElement('div');
    var templates_key = 'templates-'+editor.id;
    
    var select = document.createElement('select');
    child.appendChild(select);
    var templates = localStorage.getItem(templates_key) || '[]';
    templates = JSON.parse(templates);
    renderTemplates(select, templates);
    
    var apply = document.createElement('input');
    apply.type = 'button';
    apply.value = 'Apply';
    apply.addEventListener('click', function() {
        var name = select.value;
        var content = localStorage.getItem(templates_key+'-'+name);
        textarea.value = content;
    });
    child.appendChild(apply);
    
    var forget = document.createElement('input');
    forget.type = 'button';
    forget.value = 'Forget';
    forget.addEventListener('click', function() {
       var name = select.value;
       if (!name) {return;}
       if (!confirm('Are you sure you want to forget '+name+'?')) {return;}
       var index = templates.indexOf(name);
       templates.splice(index, 1);
       saveTemplates(templates_key, templates);
       renderTemplates(select, templates);
    });
    child.appendChild(forget);
    
    var create = document.createElement('input');
    create.type = 'button';
    create.value = 'Create';
    create.addEventListener('click', function() {
       var name = prompt('Template name:');
       var content = textarea.value;
       localStorage.setItem(templates_key+'-'+name, content);
       templates.push(name);
       saveTemplates(templates_key, templates);
       renderTemplates(select, templates);
    });
    child.appendChild(create);
        
    editor.appendChild(child);
    editor.classList.add('templatized');
};

var renderTemplates = function(select, templates) {
    select.innerHTML = '';
    templates.forEach(function(name) {
        var option = document.createElement('option');
        option.value = name;
        option.textContent = name;
        select.appendChild(option);
    });    
};

var saveTemplates = function(templatesKey, templates) {
    localStorage.setItem(templatesKey, JSON.stringify(templates));
};

var findEditors = function() {
    var editor;
    if (editor = document.getElementById('description-wiki-edit')) {
       modifyEditor(editor);
    }
    if (editor = document.getElementById('comment-wiki-edit')) {
       modifyEditor(editor);
    }
};

setInterval(findEditors, 500);