Last active
August 31, 2017 20:26
-
-
Save SigurdMW/7ccdae43a56d3c9076e3682368440060 to your computer and use it in GitHub Desktop.
Start of setup of w3cjs for automated markup validation service.
This file contains 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
// npm install --save-dev w3cjs | |
var w3cjs = require('w3cjs'); | |
var timer = 0; | |
var takeTime = setInterval(function() { | |
timer = timer + 1; | |
}, 1); | |
const validationService = (target) => { | |
return new Promise((resolve, reject) => { | |
var results = w3cjs.validate({ | |
file: target, | |
output: 'json', | |
callback: function (err, res) { | |
if(err){ | |
reject(err); | |
} | |
if (res && res.messages.length > 0 ) { | |
resolve(res); | |
} else { | |
resolve("No errors found on page " + target); | |
} | |
} | |
}); | |
}); | |
} | |
(function validateMarkup() { | |
var promises = []; | |
var validationTargets = ['http://vg.no', 'http://www.vg.no/sport/']; | |
validationTargets.map(target => { | |
promises.push(validationService(target)); | |
}); | |
Promise.all(promises) | |
.then(success => { | |
var totalMessageCount = totalErrorCount = 0; | |
var message = {}; | |
success.map((error, key) => { | |
totalMessageCount += success[key].messages.length; | |
var errors = success[key].messages.filter(item => item.type === 'error'); | |
totalErrorCount += errors.length; | |
var url = success[key].url; | |
message[url] = success[key].messages; | |
}); | |
clearInterval(takeTime); | |
console.log(totalMessageCount + " messages in total after " + timer + " ms."); | |
console.log("Errors: " + totalErrorCount); | |
console.log("Warnings: " + (totalMessageCount - totalErrorCount)); | |
// uncomment to get all messages displayed | |
// console.log(message); | |
}) | |
.catch(err => { | |
clearInterval(takeTime); | |
console.log("Obs! Error occured in the excecution of the script: Promise was rejected. More detailed error: \n"); | |
console.log(err); | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment