Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy-to-clipboard action for thread dumps #170

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,45 @@ Admin.addEventListener(window, 'load', function()
{
toolName = newToolName;
};

AdminTD.copyToClipboard = function copyToClipBoard(toSave)
{
var textToWrite, dump, tDump, area;

textToWrite = '';
dump = el(toSave);
if (dump)
{
tDump = dump.innerHTML;
textToWrite += tDump + '\n';
}

textToWrite = textToWrite.replace(/<\/?span[^>]*>/g, '');
textToWrite = textToWrite.replace(/&lt;/g, '<');
textToWrite = textToWrite.replace(/&gt;/g, '>');

area = document.createElement('textarea');
area.value = textToWrite;
document.body.appendChild(area);
area.select();
document.execCommand('copy');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this approach work in all modern browsers? Last I checked, this would not work in Chrome.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, did not actually check other browsers apart from Firefox, which I should have done and will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Windows 10 with IE 11.778.18362.0, Firefox 76.0.1, Edge 44, Chrome 81.0.4044.138 - copy to clipboard works in all environments, although IE 11 needs a bit of a fixup with regards to stripping HTML markup out of before copying to clipboard.

document.body.removeChild(area);
};

AdminTD.saveTextAsFile = function saveTextAsFile(toSave)
{
var textToWrite, allDumps, i, tDump, dump, textFileAsBlob, fileNameToSaveAs, ie, ieVer, ie11, downloadLink;

textToWrite = "";
textToWrite = '';

if (toSave === "all")
if (toSave === 'all')
{
allDumps = document.getElementsByClassName("thread");
allDumps = document.getElementsByClassName('thread');

for (i = 0; i < allDumps.length; i++)
{
tDump = allDumps[i].innerHTML;
textToWrite += tDump + "\n";
textToWrite += tDump + '\n';
}
}
else
Expand All @@ -77,20 +101,18 @@ Admin.addEventListener(window, 'load', function()
if (dump)
{
tDump = dump.innerHTML;
textToWrite += tDump + "\n";
textToWrite += tDump + '\n';
}
}

textToWrite = textToWrite.replace(/<span id="date" class="highlight">/g, "");
textToWrite = textToWrite.replace(/<span class="highlight">/g, "");
textToWrite = textToWrite.replace(/<\/span>/g, "");
textToWrite = textToWrite.replace(/&lt;/g, "<");
textToWrite = textToWrite.replace(/&gt;/g, ">");
textToWrite = textToWrite.replace(/<\/?span[^>]*>/g, '');
textToWrite = textToWrite.replace(/&lt;/g, '<');
textToWrite = textToWrite.replace(/&gt;/g, '>');

textFileAsBlob = new Blob([ textToWrite ], {
type : 'text/plain'
});
fileNameToSaveAs = toolName + ".txt";
fileNameToSaveAs = toolName + '.txt';

ie = navigator.userAgent.match(/MSIE\s([\d.]+)/);
ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/);
Expand All @@ -99,7 +121,7 @@ Admin.addEventListener(window, 'load', function()
if (ie && ieVer < 10)
{
// TODO I18n
alert("No blobs on IE ver < 10");
alert('No blobs on IE ver < 10');
return;
}

Expand All @@ -109,11 +131,11 @@ Admin.addEventListener(window, 'load', function()
}
else
{
downloadLink = document.createElement("a");
downloadLink = document.createElement('a');

downloadLink.download = fileNameToSaveAs;
// TODO I18n
downloadLink.innerHTML = "Download File";
downloadLink.innerHTML = 'Download File';

if (window.webkitURL)
{
Expand All @@ -126,7 +148,7 @@ Admin.addEventListener(window, 'load', function()
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.style.display = "none";
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
}

Expand All @@ -138,60 +160,61 @@ Admin.addEventListener(window, 'load', function()
{
var allTabs, i, selectors;

allTabs = document.getElementsByClassName("thread");
allTabs = document.getElementsByClassName('thread');
for (i = 0; i < allTabs.length; i++)
{
if (allTabs[i].id === tabName)
{
Admin.removeClass(allTabs[i], "hidden");
Admin.removeClass(allTabs[i], 'hidden');
}
else
{
Admin.addClass(allTabs[i], "hidden");
Admin.addClass(allTabs[i], 'hidden');
}
}

selectors = document.getElementsByClassName("selector");
selectors = document.getElementsByClassName('selector');

for (i = 0; i < selectors.length; i++)
{
if (selectors[i].id === "s" + tabName)
if (selectors[i].id === 's' + tabName)
{
Admin.addClass(selectors[i], "selected");
el("savecurrent").setAttribute("onclick", "AdminTD.saveTextAsFile('" + tabName + "');");
Admin.addClass(selectors[i], 'selected');
el('copycurrent').setAttribute('onclick', 'AdminTD.copyToClipboard("' + tabName + '");');
el('savecurrent').setAttribute('onclick', 'AdminTD.saveTextAsFile("' + tabName + '");');
}
else
{
Admin.removeClass(selectors[i], "selected");
Admin.removeClass(selectors[i], 'selected');
}
}
};

AdminTD.getDump = function getDump()
{
Admin.request({
url : serviceContext + "/ootbee/admin/" + encodeURIComponent(toolName) + "-getone.html",
requestContentType : "text/html",
responseContentType : "text/html",
url : serviceContext + '/ootbee/admin/' + encodeURIComponent(toolName) + '-getone.html',
requestContentType : 'text/html',
responseContentType : 'text/html',
fnSuccess : function(res)
{
var counter, viewer, control, firstDel, secondDel, tabTitle;
if (res.responseText)
{
counter = document.getElementsByClassName("thread").length;
viewer = document.getElementById("viewer");
control = document.getElementById("control");
counter = document.getElementsByClassName('thread').length;
viewer = document.getElementById('viewer');
control = document.getElementById('control');
// Find the date in the response text:
firstDel = res.responseText.indexOf(">", res.responseText.indexOf("id=\"date\""));
secondDel = res.responseText.indexOf("<", firstDel);
firstDel = res.responseText.indexOf('>', res.responseText.indexOf('id="date"'));
secondDel = res.responseText.indexOf('<', firstDel);
tabTitle = res.responseText.substr(firstDel + 1, secondDel - (firstDel + 1));

viewer.innerHTML = viewer.innerHTML
+ res.responseText.replace("__id__", "td" + counter).replace("__class__", "thread hidden");
control.innerHTML = control.innerHTML + "<input type=\"button\" class=\"selector\" value=\"" + tabTitle + "\" id=\"std"
+ counter + "\" onclick=\"AdminTD.showTab('td" + counter + "');\" /> ";
+ res.responseText.replace('__id__', 'td' + counter).replace('__class__', 'thread hidden');
control.innerHTML = control.innerHTML + '<input type="button" class="selector" value="' + tabTitle + '" id="std'
+ counter + '" onclick="AdminTD.showTab(\'td' + counter + '\');" />';

AdminTD.showTab("td" + counter);
AdminTD.showTab('td' + counter);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Copyright (C) 2005 - 2020 Alfresco Software Limited.
<p class="intro">${msg("threaddump.intro-text")?html}</p>

<@button label=msg("threaddump.get-another") onclick="AdminTD.getDump();"/>
<@button id="copycurrent" class="copy" label=msg("threaddump.copycurrent") onclick="AdminTD.copyToClipboard('current');"/>
<@button id="savecurrent" class="save" label=msg("threaddump.savecurrent") onclick="AdminTD.saveTextAsFile('current');"/>
<@button class="save" label=msg("threaddump.saveall") onclick="AdminTD.saveTextAsFile('all');"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ threaddump.title=Thread Dump
threaddump.intro-text=View and save thread dumps.
threaddump.get-another=Get Another Thread Dump
threaddump.saveall=Save All
threaddump.copycurrent=Copy Current Thread to Clipboard
threaddump.savecurrent=Save Current Thread
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
threaddump.title=Prozessabbild
threaddump.intro-text=Hier k\u00f6nnen Sie Prozessabbilder erstellen und speichern.
threaddump.get-another=Weiteres Abbild erstellen
threaddump.saveall=Alles Speichern
threaddump.savecurrent=Aktuelles Abbild Speichern
threaddump.saveall=Alle speichern
threaddump.copycurrent=Aktuelles Abbild in Zwischenablage kopieren
threaddump.savecurrent=Aktuelles Abbild speichern
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ threaddump.title=Thread Dump
threaddump.intro-text=View and save thread dumps.
threaddump.get-another=Get Another Thread Dump
threaddump.saveall=Save All
threaddump.copycurrent=Copy Current Thread to Clipboard
threaddump.savecurrent=Save Current Thread
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ threaddump.title=Thread Dump
threaddump.intro-text=Ver y salvar Thread Dumps (Volcados de hilos).
threaddump.get-another=Obtener un Thread Dump
threaddump.saveall=Salvar Todo
threaddump.copycurrent=Copy Current Thread to Clipboard
threaddump.savecurrent=Salvar este Thread Dump
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ threaddump.title=Dump del Thread
threaddump.intro-text=Visualizza e salva i dump del thread
threaddump.get-another=Ottieni un altro dump del Thread
threaddump.saveall=Salva Tutto
threaddump.copycurrent=Copy Current Thread to Clipboard
threaddump.savecurrent=Salva il thread corrente
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ threaddump.title=Dump de Thread
threaddump.intro-text=Visualizar e salvar dumps de threads.
threaddump.get-another=Obter outro Dump de Thread
threaddump.saveall=Salvar Tudo
threaddump.copycurrent=Copy Current Thread to Clipboard
threaddump.savecurrent=Salvar Thread Atual