Created
March 14, 2017 01:43
-
-
Save nickcoutsos/eda040d4f3d1031748afd1b2ada527fc to your computer and use it in GitHub Desktop.
A userscript to add a Jira-Markdown converter to Jira text fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name JIRA Markdown Converter | |
// @namespace https://nickcoutsos.github.io/ | |
// @version 0.1 | |
// @description Add context menu action to convert text in a field to/from Github Flavored Markdown | |
// @author Nick Coutsos | |
// @match https://*.atlassian.net/* | |
// @require https://raw.githubusercontent.com/FokkeZB/J2M/master/src/J2M.js | |
// ==/UserScript== | |
var syntaxes = { | |
jira: {converter: J2M.toM, label: 'Convert to Markdown'}, | |
markdown: {converter: J2M.toJ, label: 'Convert to JIRA'} | |
}; | |
function watch(selector, interval, onChange) { | |
if (typeof interval === 'function') { | |
onChange = interval; | |
interval = 200; | |
} | |
let timer = setInterval(function () { | |
let element = document.querySelector(selector); | |
if (element && !element.dataset.lastObserved) { | |
element.setAttribute('data-last-observed', Date.now()); | |
onChange(element); | |
} | |
}, interval); | |
} | |
function addConverter(element) { | |
var textarea = element.parentNode.parentNode.querySelector('textarea'); | |
var button = document.createElement('input'); | |
var isJira = true; | |
button.type = 'image'; | |
button.src = 'https://cdn0.iconfinder.com/data/icons/octicons/1024/mark-github-128.png'; | |
button.height = '15'; | |
button.style.verticalAlign = 'text-top'; | |
button.setAttribute('state', 'g2j'); | |
element.appendChild(button); | |
element.addEventListener('click', e => { | |
e.preventDefault(); | |
var syntax = syntaxes[isJira ? 'jira' : 'markdown']; | |
isJira = !isJira; | |
button.alt = syntax.label; | |
textarea.textContent = syntax.converter(textarea.textContent); | |
}); | |
} | |
(function() { | |
'use strict'; | |
watch('.save-options .field-tools', addConverter); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment