diff --git a/CheckURL.js b/CheckURL.js index 4657a6e..fff72ea 100644 --- a/CheckURL.js +++ b/CheckURL.js @@ -43,6 +43,8 @@ if (opts[0].startsWith("--") || opts[0].startsWith("/")) { //if it's a commn // }); printJSON(opts[1]) + } else if (opts[0] == "--ignore") { + readUrlsWithoutIgnore(opts[1], opts[2]); } else { console.log("Invalid input, please enter '--v' for version or filename for testing."); } @@ -59,6 +61,51 @@ function printResult() { console.log(resultList) } +function readUrlsWithoutIgnore(ignoreFile, file) { + fs.readFile(ignoreFile, (err, data) => { + if (err) { + console.log(err); + } else { + var strData = data.toString(); + var ignoreURLs = strData.match(regexp); + ignoreURLs = [...new Set(ignoreURLs)]; + + fs.readFile(file, (err, data) => { + if (err) { + console.log(err); + } else { + var strData = data.toString(); + var URLs = strData.match(regexp); + URLs = [...new Set(URLs)]; + + for (let i = 0; i < URLs.length; i++) { + let isIgnore = false; + for (let j = 0; j < ignoreURLs.length; j++) { + if (URLs[i].startsWith(ignoreURLs[j])) { + isIgnore = true; + } + } + + if (!isIgnore) { + request.get({ uri: URLs[i], timeout: 5000 }, function (err, res, body) { + if (err) { + console.log(colors.yellow(`${err} ${URLs[i]}`)); + } else if (res.statusCode == 200) { + console.log("This page is ok: " + URLs[i].green) + } else if (res.statusCode == 404 || res.statusCode == 400) { + console.log("Can not find this page: " + URLs[i].red) + } else { + console.log("Unkown status: " + URLs[i].grey) + } + }) + } + } + } + }) + } + }) +} + function printJSON(filename) { fs.readFile(filename, (err, data) => { //read the file if (err) { diff --git a/README.md b/README.md index c598f33..4d081fb 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ npm i --save colors request 2. allow passing directory paths vs. file paths, and recursively process all children under that directory 3. add support for more HTTP result codes. For example, redirects with 301, 307, 308 (i.e., follow the redirect to the new location) 4. add support for timeouts, DNS resolution issues, or other server errors when accessing a bad URL. A bad domain, URL, or server shouldn't crash your tool. +5. add feature for ignore url(s). If you enter 'Webpage-status-checking-tool --ignore [ignore url list filename] [url list filename]', this tool test all urls without ignore urls. ## Instructions diff --git a/ignore-urls.txt b/ignore-urls.txt new file mode 100644 index 0000000..a07f78a --- /dev/null +++ b/ignore-urls.txt @@ -0,0 +1,6 @@ +# Ignore All Seneca College URLs +https://senecacollege.ca +# Ignore all Learn@Seneca URLs +https://learn.senecacollege.ca/ +#Ignore all s9y.org +http://s9y.org \ No newline at end of file