Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Last active March 20, 2018 19:41
Show Gist options
  • Select an option

  • Save NickDeckerDevs/28b280afc0adcdeeb2165f8042599d4d to your computer and use it in GitHub Desktop.

Select an option

Save NickDeckerDevs/28b280afc0adcdeeb2165f8042599d4d to your computer and use it in GitHub Desktop.
Loop through Sitemap.xml file and query pages in XML file for a string. Useful for finding links or other content. To use, paste the script in the console on the sitemap.xml page. Update the variable stringToCheckFor to the text you are looking for
const urlNodes = document.querySelectorAll('.text'); // use '.line .text' if you want to only search site pages, not blog also
for (let k = 0; k < urlNodes.length; k++) {
const nodeObject = urlNodes[k];
const text = nodeObject.innerText;
const url = text.toString();
if (url.includes('http')) {
// console.log('fetching from ' + url)
fetch(url)
.then(response => {
if (response.ok) {
return Promise.resolve(response);
} else {
console.log('this is an error');
return Promise.reject(new Error('failed to load: ' + url));
}
})
.then(response => response.text())
.then(data => {
const pageHtml = data;
// console.log(pageHtml);
// updated this to be the string you are checking the page for
const stringToCheckFor = 'it-assessment-checklist-new-year';
// console.log(`checking page: ${url}`);
if (pageHtml.toString().includes(stringToCheckFor)) {
// the pages that display in console will have that string somewhere in the page
console.log(url);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment