refactor: optimize selection capture and remove dead code
- Removed Node.js export from capture.js (browser-only code) - Simplified selection capture to avoid expensive getComputedStyle() calls - Added performance notes for selection clipping - Aligned content.js with capture.js implementation Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -46,6 +46,8 @@ function captureFullPage() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Capture selected content from the page
|
* Capture selected content from the page
|
||||||
|
* Note: For performance, selection clipping uses basic HTML structure without
|
||||||
|
* per-element computed styles. The resulting PDF will use browser default styles.
|
||||||
* @returns {Object} Selection data with HTML, text, and metadata
|
* @returns {Object} Selection data with HTML, text, and metadata
|
||||||
*/
|
*/
|
||||||
function captureSelection() {
|
function captureSelection() {
|
||||||
@@ -92,11 +94,3 @@ function captureSelection() {
|
|||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export functions for use in other scripts
|
|
||||||
if (typeof module !== 'undefined' && module.exports) {
|
|
||||||
module.exports = {
|
|
||||||
captureFullPage,
|
|
||||||
captureSelection
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ function captureFullPage() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Capture selected content from the page
|
* Capture selected content from the page
|
||||||
|
* Note: For performance, uses basic HTML structure without per-element computed styles.
|
||||||
|
* The resulting PDF will use browser default styles.
|
||||||
*/
|
*/
|
||||||
function captureSelection() {
|
function captureSelection() {
|
||||||
const selection = window.getSelection();
|
const selection = window.getSelection();
|
||||||
@@ -57,27 +59,6 @@ function captureSelection() {
|
|||||||
const container = document.createElement('div');
|
const container = document.createElement('div');
|
||||||
container.appendChild(range.cloneContents());
|
container.appendChild(range.cloneContents());
|
||||||
|
|
||||||
// Get computed styles for the selection
|
|
||||||
const elements = container.querySelectorAll('*');
|
|
||||||
elements.forEach(el => {
|
|
||||||
const computed = window.getComputedStyle(el);
|
|
||||||
// Only preserve essential styles
|
|
||||||
const essentialStyles = [
|
|
||||||
'font-family', 'font-size', 'font-weight', 'color',
|
|
||||||
'background-color', 'text-align', 'margin', 'padding'
|
|
||||||
];
|
|
||||||
let styleStr = '';
|
|
||||||
essentialStyles.forEach(prop => {
|
|
||||||
const value = computed.getPropertyValue(prop);
|
|
||||||
if (value) {
|
|
||||||
styleStr += `${prop}: ${value}; `;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (styleStr) {
|
|
||||||
el.setAttribute('style', styleStr);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const html = `<!DOCTYPE html>
|
const html = `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@@ -99,6 +80,8 @@ function captureSelection() {
|
|||||||
<hr>
|
<hr>
|
||||||
${container.innerHTML}
|
${container.innerHTML}
|
||||||
</body>
|
</body>
|
||||||
|
</html>`;
|
||||||
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user