Skip to content

Commit fd3432c

Browse files
committed
rich HTML copying
Signed-off-by: Vedansh Saini <[email protected]>
1 parent 1953617 commit fd3432c

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

src/scripts/popup.js

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,34 @@ document.addEventListener('DOMContentLoaded', function() {
1212
copyBtn.addEventListener('click', function() {
1313
const scrumReport = document.getElementById('scrumReport');
1414

15+
// Create container for HTML content
1516
const tempDiv = document.createElement('div');
1617
tempDiv.innerHTML = scrumReport.innerHTML;
17-
18-
const links = tempDiv.getElementsByTagName('a');
19-
Array.from(links).forEach(link => {
20-
const title = link.textContent;
21-
const url = link.href;
22-
const markdownLink = `[${title}](${url})`;
23-
link.outerHTML = markdownLink;
24-
});
25-
26-
const stateButtons = tempDiv.getElementsByClassName('State');
27-
Array.from(stateButtons).forEach(button => {
28-
button.remove();
29-
});
30-
31-
tempDiv.innerHTML = tempDiv.innerHTML.replace(/<br\s*\/?>/gi, '\n');
32-
33-
const listItems = tempDiv.getElementsByTagName('li');
34-
Array.from(listItems).forEach(item => {
35-
item.innerHTML = '\n- ' + item.innerHTML;
36-
});
37-
38-
tempDiv.innerHTML = tempDiv.innerHTML.replace(/<\/?ul>/gi, '\n');
39-
let textContent = tempDiv.textContent;
40-
textContent = textContent.replace(/\n\s*\n/g, '\n\n');
41-
textContent = textContent.trim();
42-
43-
const textarea = document.createElement('textarea');
44-
textarea.value = textContent;
45-
document.body.appendChild(textarea);
46-
textarea.select();
47-
document.execCommand('copy');
48-
document.body.removeChild(textarea);
49-
50-
Materialize.toast('Report copied to clipboard!', 3000);
18+
document.body.appendChild(tempDiv);
19+
tempDiv.style.position = 'absolute';
20+
tempDiv.style.left = '-9999px';
21+
22+
// Select the content
23+
const range = document.createRange();
24+
range.selectNode(tempDiv);
25+
const selection = window.getSelection();
26+
selection.removeAllRanges();
27+
selection.addRange(range);
28+
29+
try {
30+
// Copy HTML content
31+
const success = document.execCommand('copy');
32+
if (!success) {
33+
throw new Error('Copy command failed');
34+
}
35+
Materialize.toast('Report copied with formatting!', 3000, 'green');
36+
} catch (err) {
37+
console.error('Failed to copy:', err);
38+
Materialize.toast('Failed to copy report', 3000, 'red');
39+
} finally {
40+
// Cleanup
41+
selection.removeAllRanges();
42+
document.body.removeChild(tempDiv);
43+
}
5144
});
5245
});

0 commit comments

Comments
 (0)