Skip to content

Commit 8ecf924

Browse files
authored
Add button to copy selected name (#85)
1 parent c62d1d3 commit 8ecf924

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

ui/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ <h1 id="title">PC-Miner</h1>
286286
<button onClick="pcminer.copyToClipboard()">
287287
Display selected names
288288
</button>
289+
<button onClick="pcminer.copySelectedName()">Copy selected name</button>
289290
</div>
291+
290292
<!-- modal content -->
291293
<div id="basic-modal-content">
292294
<!-- content appended to this element -->

ui/js/code.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,48 @@ var pcminer = (function () {
500500
$("#basic-modal-content").modal();
501501
}
502502

503+
function copySelectedName() {
504+
// Get the select element
505+
var selector = document.getElementById("selector");
506+
if (!selector) {
507+
console.error("Selector element not found.");
508+
return;
509+
}
510+
511+
// Get the currently selected option
512+
var selectedOption = selector.options[selector.selectedIndex];
513+
if (!selectedOption) {
514+
console.error("No option is selected.");
515+
return;
516+
}
517+
518+
var text = selectedOption.text;
519+
520+
// Use the Clipboard API if available
521+
if (navigator.clipboard && navigator.clipboard.writeText) {
522+
navigator.clipboard
523+
.writeText(text)
524+
.then(function () {
525+
console.log("Copied selected name: " + text);
526+
})
527+
.catch(function (err) {
528+
console.error("Failed to copy text: ", err);
529+
});
530+
} else {
531+
// Fallback method for older browsers
532+
var textarea = document.createElement("textarea");
533+
textarea.value = text;
534+
document.body.appendChild(textarea);
535+
textarea.select();
536+
try {
537+
document.execCommand("copy");
538+
console.log("Copied selected name: " + text);
539+
} catch (err) {
540+
console.error("Fallback: Unable to copy text", err);
541+
}
542+
document.body.removeChild(textarea);
543+
}
544+
}
503545
/**
504546
* counts the number of publications and committees for an author in the current date range
505547
*/
@@ -945,6 +987,7 @@ var pcminer = (function () {
945987
confSelected: confSelected,
946988
confsSelected: confsSelected,
947989
copyToClipboard: copyToClipboard,
990+
copySelectedName: copySelectedName,
948991
updateExcluded: updateExcluded,
949992
main: main,
950993
};

0 commit comments

Comments
 (0)