Skip to content

Commit 988f847

Browse files
authored
Make attribute filters optional in attribute extraction mode
2 parents da40ee6 + b71d794 commit 988f847

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

contentExtr.html

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ <h2 class="text-xl font-semibold text-gray-800 mb-4">Results</h2>
114114

115115
function extractContent() {
116116
const htmlInput = document.getElementById('htmlInput').value;
117-
const attributeName = document.getElementById('attributeName').value;
118-
const attributeValue = document.getElementById('attributeValue').value;
117+
const attributeName = document.getElementById('attributeName').value.trim();
118+
const attributeValue = document.getElementById('attributeValue').value.trim();
119119
const extractionMode = document.querySelector('input[name="extractionMode"]:checked').value;
120120
const resultsDiv = document.getElementById('results');
121121
extractedValues = [];
@@ -125,24 +125,32 @@ <h2 class="text-xl font-semibold text-gray-800 mb-4">Results</h2>
125125
const parser = new DOMParser();
126126
const doc = parser.parseFromString(htmlInput, 'text/html');
127127

128-
// Find all elements with matching attribute
129-
const elements = doc.querySelectorAll(`[${attributeName}="${attributeValue}"]`);
130-
131-
if (elements.length === 0) {
132-
resultsDiv.innerHTML = '<p class="text-red-500">No matching elements found.</p>';
133-
return;
134-
}
135-
136-
let output = '<div class="space-y-4">';
128+
let elements;
137129

138130
if (extractionMode === 'attribute') {
139131
// Extract attribute values
140-
const extractAttribute = document.getElementById('extractAttribute').value;
132+
const extractAttribute = document.getElementById('extractAttribute').value.trim();
141133
if (!extractAttribute) {
142134
resultsDiv.innerHTML = '<p class="text-red-500">Please enter an attribute name to extract.</p>';
143135
return;
144136
}
145137

138+
// Build selector based on provided filters
139+
if (attributeName && attributeValue) {
140+
elements = doc.querySelectorAll(`[${attributeName}="${attributeValue}"]`);
141+
} else if (attributeName) {
142+
elements = doc.querySelectorAll(`[${attributeName}]`);
143+
} else {
144+
// No filter - select all elements with the attribute to extract
145+
elements = doc.querySelectorAll(`[${extractAttribute}]`);
146+
}
147+
148+
if (elements.length === 0) {
149+
resultsDiv.innerHTML = '<p class="text-red-500">No matching elements found.</p>';
150+
return;
151+
}
152+
153+
let output = '<div class="space-y-4">';
146154
elements.forEach((element, index) => {
147155
const attrValue = element.getAttribute(extractAttribute);
148156
if (attrValue !== null) {
@@ -164,8 +172,22 @@ <h2 class="text-xl font-semibold text-gray-800 mb-4">Results</h2>
164172
output += '</div>';
165173
output += `<p class="mt-4 text-sm text-gray-600">Found ${extractedValues.length} values from ${elements.length} elements</p>`;
166174
output += '<button onclick="copyExtractedValues()" class="mt-4 bg-primary hover:bg-primary-dark text-white px-4 py-2 rounded-lg transition duration-200">Copy All Values</button>';
175+
resultsDiv.innerHTML = output;
167176
} else {
168-
// Extract text content (original behavior)
177+
// Extract text content (original behavior) - requires attribute name/value
178+
if (!attributeName || !attributeValue) {
179+
resultsDiv.innerHTML = '<p class="text-red-500">Please enter both attribute name and value for text extraction.</p>';
180+
return;
181+
}
182+
183+
elements = doc.querySelectorAll(`[${attributeName}="${attributeValue}"]`);
184+
185+
if (elements.length === 0) {
186+
resultsDiv.innerHTML = '<p class="text-red-500">No matching elements found.</p>';
187+
return;
188+
}
189+
190+
let output = '<div class="space-y-4">';
169191
elements.forEach((element, index) => {
170192
extractedValues.push(element.textContent);
171193
output += `
@@ -181,9 +203,8 @@ <h2 class="text-xl font-semibold text-gray-800 mb-4">Results</h2>
181203
});
182204
output += '</div>';
183205
output += '<button onclick="copyAllText()" class="mt-4 bg-primary hover:bg-primary-dark text-white px-4 py-2 rounded-lg transition duration-200">Copy All Texts</button>';
206+
resultsDiv.innerHTML = output;
184207
}
185-
186-
resultsDiv.innerHTML = output;
187208
} catch (error) {
188209
resultsDiv.innerHTML = `<p class="text-red-500">Error: ${error.message}</p>`;
189210
}

0 commit comments

Comments
 (0)