Created
January 24, 2024 10:30
-
-
Save HanEmile/75396d3b6f1a9c18cd160cffadb63efc to your computer and use it in GitHub Desktop.
small naive javascript spider for the toolbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: (function() { | |
var visited = []; | |
var recursiveSpider = function(url) { | |
if (visited.indexOf(url) != -1) { | |
return; | |
} | |
visited.push(url); | |
console.log(url); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState === XMLHttpRequest.DONE) { | |
if (xhr.status === 200) { | |
var parser = new DOMParser(); | |
var doc = parser.parseFromString(xhr.responseText, 'text/html'); | |
var links = doc.getElementsByTagName('a'); | |
for (var i = 0; i < links.length; i++) { | |
var href = links[i].getAttribute('href'); | |
if (href && href.indexOf('#') != 0) { | |
var absUrl = (new URL(href, url)).href; | |
recursiveSpider(absUrl); | |
} | |
} | |
} | |
} | |
}; | |
xhr.send(); | |
}; | |
recursiveSpider(window.location.href); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment