Reputation: 482
I have a folder with many HTML documents, named 001.html to 999.html. In each document, there are the following lines:
<span class="my-name">Name X</span>
I want access to the content of this <span>
, which I can get by opening the document in my browser and then entering the following in my addressbar:
javascript:document.getElementById('my-name').innerHTML;
Without altering the HTML documents, and without having to do it manually, how can I write a script which loads these HTML documents externally, applies the javascript above and returns the content of the <span>
?
Upvotes: 1
Views: 50
Reputation: 11330
Sounds like you need some kind of HTML parser. HTML Agility Pack is a good option. You can read each individual file, search for the span, and print out the result. JavaScript is not enough for what you are trying to do.
Upvotes: 0
Reputation: 15742
Run this from an HTML file in the same folder. Code is not tested:
function read(i, toIncl, done, result, iframe) {
if (i <= toIncl) {
if (!result) {
result = [];
}
if (!iframe) {
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.onload = function () {
result.push(iframe.contentWindow.document.querySelector('.my-name').textContent);
read(++i, toIncl, done, result, iframe);
}
}
iframe.src = ('000' + i).substr(-3) + '.html';
} else {
iframe.parentNode.removeChild(iframe);
done(result);
}
}
read(0, 999, function (result) {
console.log('Result:', result);
})
Upvotes: 1