Skip to content

Instantly share code, notes, and snippets.

@palaniraja
Last active April 12, 2024 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palaniraja/500c9c4a4ebd124bf445603c6450a32e to your computer and use it in GitHub Desktop.
Save palaniraja/500c9c4a4ebd124bf445603c6450a32e to your computer and use it in GitHub Desktop.
jira desc history diff - quick hack
var hItems = document.querySelectorAll("div[data-testid='issue-history.ui.history-items.generic-history-item.history-item']")
var desc = hItems[1]
if (hItems.length < 1) {
alert("switch to history tab and re-run")
return
}
else {
Array.from(hItems).forEach(function (desc, idx){
if (desc.childNodes.length > 1 ){
prof = desc.childNodes[0]
descAction = desc.childNodes[1]
descActionText = descAction.childNodes[0].innerText
descDiff = descAction.childNodes[1]
var oldDesc
var newDesc
if (descDiff.childNodes.length > 2) {
oldDesc = descDiff.childNodes[0]
newDesc = descDiff.childNodes[2]
}
var strDesc = 'updated the Description'
if (descActionText.includes(strDesc)) {
var diffBtn = document.createElement('button');
diffBtn.textContent = 'Diff!';
diffBtn.addEventListener('click', () => {
diffThis(idx, descDiff, oldDesc, newDesc)
});
prof.appendChild(diffBtn)
}
}
})
function injectJS(url) {
var po = document.createElement('script');
po.type = 'text/javascript';
po.async = true;
po.src = url;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
}
injectJS('https://cdnjs.cloudflare.com/ajax/libs/jsdiff/5.2.0/diff.min.js');
function diffThis(idx, container, left, right) {
// console.log(idx, container, left, right)
var diffDiv = document.createElement('div');
// diffDiv.style.whiteSpace = 'break-spaces'
left.parentElement.parentElement.appendChild(diffDiv)
var diffedText = ""
const diff = Diff.diffWords(left.textContent, right.textContent),
display = diffDiv,
fragment = document.createDocumentFragment();
diff.forEach((part) => {
if (part.added) {
diffedText += '{color:#36b37e}' + part.value + '{color}'
}
else if (part.removed) {
diffedText += '{color:#ff5630}' + part.value + '{color}'
}
else {
diffedText += '{color:#97a0af}' + part.value + '{color}'
}
});
// console.log(diffedText)
jQuery.ajax({
contentType: 'application/json',
type: 'POST',
url: '/rest/api/1.0/render',
data: '{"rendererType":"atlassian-wiki-renderer","unrenderedMarkup":' + JSON.stringify(diffedText) +',"issueKey":"SUPPORT-1"}',
success: function( response ) {
console.log(response);
display.innerHTML = response
}
});
}
}
@palaniraja
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment