Skip to content

Instantly share code, notes, and snippets.

@korvus
Last active March 24, 2016 16:55
Show Gist options
  • Save korvus/4e8df79246b4c34fff65 to your computer and use it in GitHub Desktop.
Save korvus/4e8df79246b4c34fff65 to your computer and use it in GitHub Desktop.
javascript snippet - for bookmark
/**
* Validate with w3c API a mouseover part of any webpage.
* @author Simon ertel
* @license MIT
*/
(function () {
"use strict";
var rawAnalyser = 1;
var focused = document.querySelector("body");
function setCSS() {
var styleEl = document.createElement('style'), styleSheet;
document.head.appendChild(styleEl);
styleSheet = styleEl.sheet;
styleSheet.insertRule("body>span.infosSelectionForW3C{z-index:1000000;position:fixed;top:40px;color:#555;font-size:11px;right:0;right:10px;background-color:#fff;border:1px solid #555;padding:2px;display:block;}", 0);
styleSheet.insertRule("body>span.LazyValidatorShortCut{z-index:1000000;position:fixed;top:10px;color:#999;font-size:11px;right:0;right:10px;background-color:#fff;border:1px solid #555;padding:2px;display:block;}", 1);
styleSheet.insertRule(".popinValidator{z-index: 1100000;background:#fff;position:absolute;top:2px;margin:10px auto;left:50%;display:none;width:80%;padding:10px;border:1px solid #000;transform:translate(-50%, 0);box-shadow:0 0 10px #555;}", 2);
styleSheet.insertRule(".showPopin{display:block;}",3);
styleSheet.insertRule(".showPopin ul{padding:0;margin:0;}",4);
styleSheet.insertRule(".showPopin ul li{position:relative;margin:5px 0;padding:15px 10px 10px;border-radius:5px;display:block;}",5);
styleSheet.insertRule(".showPopin ul li.error:before{position:absolute;top:0;content:'invalid';display:block;font-size:9px;padding:0 10px;height:14px;background-color:#f00;color:#fff;border-radius:0 0 5px 5px;}",6);
styleSheet.insertRule(".showPopin ul li.warning:before{position:absolute;top:0;content:'warning';display:block;font-size:9px;padding:0 10px;height:14px;background-color:#fd0;color:#fff;border-radius:0 0 5px 5px;}",7);
styleSheet.insertRule(".showPopin ul li.info:before{position:absolute;top:0;content:'warning';display:block;font-size:9px;padding:0 10px;height:14px;background-color:#fd0;color:#fff;border-radius:0 0 5px 5px;}",8);
styleSheet.insertRule(".showPopin ul li.error{border:1px solid #f00;}",9);
styleSheet.insertRule(".showPopin ul li.warning{border:1px solid #fd0;}",10);
styleSheet.insertRule(".showPopin ul li.info{border:1px solid #fd0;}",11);
styleSheet.insertRule(".showPopin ul li code{display:block;border: 1px dotted #999;color:#000;background:#eee;padding: 5px;}",12);
styleSheet.insertRule(".showPopin .snippetAnalysed{display:block;height:200px;width:100%;font-size:12px;border:1px dotted #999;color:#000;background:#eee;padding: 5px;}",12);
styleSheet.insertRule(".validatorw3cfocus{outline:1px solid #f00;box-shadow:0 0 15px #ccc inset;}",13);
}
function stickInfos(o) {
var type = o.nodeName.toString();
var rawClass = o.classList.toString();
var allClass = "";
var theId = "";
if(rawClass.length>0){
if(rawClass.indexOf(' ')){
var arrClass = rawClass.split(" ");
allClass = '.'+arrClass.join('.');
}else{
allClass = "."+rawClass;
}
}else{
allClass = "";
}
if(o.getAttribute("id")){
theId = o.getAttribute("id");
theId = "#"+theId;
}
var coordata = document.querySelector(".infosSelectionForW3C");
coordata.textContent = type+""+theId+""+allClass;
}
function unbindMouseMoveEvent(){
window.removeEventListener("mouseover", focusOnTarget, false);
window.removeEventListener("mouseout", leaveFocus, false);
}
function treatJsonBack(jsonFromApi, rawHTML){
unbindMouseMoveEvent();
jsonFromApi = JSON.parse(jsonFromApi);
//alert(jsonFromApi);
var ulRapport = document.createElement("ul");
var listOfMessages = jsonFromApi.messages;
var iterationOnJson = 0;
while (listOfMessages[iterationOnJson]){
var liRapport = document.createElement("li");
var importance = listOfMessages[iterationOnJson].type.toString();
var mssg = listOfMessages[iterationOnJson].message.toString();
var popinValidationTxt = document.createTextNode(mssg);
liRapport.classList.add(importance);
liRapport.appendChild(popinValidationTxt);
if(listOfMessages[iterationOnJson].extract){
var badCode = document.createElement("code");
var extract = listOfMessages[iterationOnJson].extract.toString();
var extractCode = document.createTextNode(extract);
badCode.appendChild(extractCode);
liRapport.appendChild(badCode);
}
ulRapport.appendChild(liRapport);
//Send the whole stuffs
iterationOnJson++;
}
if(iterationOnJson === 0){
var noResults = document.createTextNode("No warnings, no error on this snippet");
var liRapportAlt = document.createElement("li");
liRapportAlt.appendChild(noResults);
ulRapport.appendChild(liRapportAlt);
}
var codeSnip = document.createElement("textarea");
var rawHTMLcontent = document.createTextNode(rawHTML);
codeSnip.classList.add("snippetAnalysed");
codeSnip.appendChild(rawHTMLcontent);
var popin = document.querySelector(".popinValidator");
popin.appendChild(ulRapport);
popin.appendChild(codeSnip);
popin.classList.add("showPopin");
}
function getAPI(rawHTML){
var xhr = new XMLHttpRequest();
xhr.open("post", "https://validator.nu/?out=json", true);
xhr.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
xhr.onload = function(e){
if (xhr.readyState === 4){
if (xhr.status === 200){
treatJsonBack(xhr.responseText, rawHTML);
} else {
alert(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
alert(xhr.statusText);
};
xhr.send(rawHTML);
}
function pasteCurrent(){
var startFrom = "";
var finishTo = "";
if(focused.nodeName.toString() != "BODY"){
startFrom = "<body>";
finishTo = "</body>";
}
var fragment = focused.outerHTML;
fragment = "<!doctype html><html lang='en'><head><meta charset='utf-8'><title>Shell</title></head>"+startFrom+""+fragment+""+finishTo+"</html>";
getAPI(fragment);
}
function destroyPanels(){
document.querySelector(".infosSelectionForW3C").remove();
document.querySelector(".LazyValidatorShortCut").remove();
document.querySelector(".popinValidator").remove();
}
function createStickyInfos(){
var infos = document.createElement("span");
infos.className = "infosSelectionForW3C";
var txtInfos = document.createTextNode("[nodeName, class & ID]");
infos.appendChild(txtInfos);
document.body.appendChild(infos);
var shortCut = document.createElement("span");
shortCut.className = "LazyValidatorShortCut";
var shortCutInfos = document.createTextNode("TOP ARROW key => move to Node node the overred one. V key => Validate the fragment you focus on. ECHAP KEY => remove the behavior");
shortCut.appendChild(shortCutInfos);
document.body.appendChild(shortCut);
var popinW3c = document.createElement("div");
popinW3c.className = "popinValidator";
document.body.appendChild(popinW3c);
}
function focusOnTarget(e){
focused = e.target;
focused.classList.add("validatorw3cfocus");
stickInfos(focused);
}
function leaveFocus(e){
focused.classList.remove("validatorw3cfocus");
}
function bindKeyboard(e){
if(e.charCode == 118){
e.preventDefault();
pasteCurrent();
}
if(e.keyCode == 38){
e.preventDefault();
if(focused !== 0){
if(focused.parentNode && focused.parentNode.nodeName.toString() != "HTML"){
focused.classList.remove("validatorw3cfocus");
focused = focused.parentNode;
focused.classList.add("validatorw3cfocus");
stickInfos(focused);
}
}
}
//Echap
if(e.keyCode == 27){
unbindMouseMoveEvent();
document.body.removeEventListener("keypress", bindKeyboard);
var elmtsFocused = document.querySelector(".validatorw3cfocus");
elmtsFocused.classList.remove("validatorw3cfocus");
destroyPanels();
rawAnalyser = 1;
focused = document.querySelector("body");
}
}
function init(){
rawAnalyser = 0;
createStickyInfos();
document.body.addEventListener('keypress', bindKeyboard);
window.addEventListener("mouseover", focusOnTarget, false);
window.addEventListener("mouseout", leaveFocus, false);
}
setCSS();
if(rawAnalyser===1){
init();
}
})();
/*
Bookmark:
Used with http://chriszarate.github.io/bookmarkleter/ :
javascript:void%20function(){(function(){%22use%20strict%22;function%20e(){var%20e,o=document.createElement(%22style%22);document.head.appendChild(o),e=o.sheet,e.insertRule(%22body%3Espan.infosSelectionForW3C{z-index:1000000;position:fixed;top:40px;color:%23555;font-size:11px;right:0;right:10px;background-color:%23fff;border:1px%20solid%20%23555;padding:2px;display:block;}%22,0),e.insertRule(%22body%3Espan.LazyValidatorShortCut{z-index:1000000;position:fixed;top:10px;color:%23999;font-size:11px;right:0;right:10px;background-color:%23fff;border:1px%20solid%20%23555;padding:2px;display:block;}%22,1),e.insertRule(%22.popinValidator{z-index:%201100000;background:%23fff;position:absolute;top:2px;margin:10px%20auto;left:50%25;display:none;width:80%25;padding:10px;border:1px%20solid%20%23000;transform:translate(-50%25,%200);box-shadow:0%200%2010px%20%23555;}%22,2),e.insertRule(%22.showPopin{display:block;}%22,3),e.insertRule(%22.showPopin%20ul{padding:0;margin:0;}%22,4),e.insertRule(%22.showPopin%20ul%20li{position:relative;margin:5px%200;padding:15px%2010px%2010px;border-radius:5px;display:block;}%22,5),e.insertRule(%22.showPopin%20ul%20li.error:before{position:absolute;top:0;content:'invalid';display:block;font-size:9px;padding:0%2010px;height:14px;background-color:%23f00;color:%23fff;border-radius:0%200%205px%205px;}%22,6),e.insertRule(%22.showPopin%20ul%20li.warning:before{position:absolute;top:0;content:'warning';display:block;font-size:9px;padding:0%2010px;height:14px;background-color:%23fd0;color:%23fff;border-radius:0%200%205px%205px;}%22,7),e.insertRule(%22.showPopin%20ul%20li.info:before{position:absolute;top:0;content:'warning';display:block;font-size:9px;padding:0%2010px;height:14px;background-color:%23fd0;color:%23fff;border-radius:0%200%205px%205px;}%22,8),e.insertRule(%22.showPopin%20ul%20li.error{border:1px%20solid%20%23f00;}%22,9),e.insertRule(%22.showPopin%20ul%20li.warning{border:1px%20solid%20%23fd0;}%22,10),e.insertRule(%22.showPopin%20ul%20li.info{border:1px%20solid%20%23fd0;}%22,11),e.insertRule(%22.showPopin%20ul%20li%20code{display:block;border:%201px%20dotted%20%23999;color:%23000;background:%23eee;padding:%205px;}%22,12),e.insertRule(%22.showPopin%20.snippetAnalysed{display:block;height:200px;width:100%25;font-size:12px;border:1px%20dotted%20%23999;color:%23000;background:%23eee;padding:%205px;}%22,12),e.insertRule(%22.validatorw3cfocus{outline:1px%20solid%20%23f00;box-shadow:0%200%2015px%20%23ccc%20inset;}%22,13)}function%20o(e){var%20o=e.nodeName.toString(),t=e.classList.toString(),n=%22%22,i=%22%22;if(t.length%3E0)if(t.indexOf(%22%20%22)){var%20d=t.split(%22%20%22);n=%22.%22+d.join(%22.%22)}else%20n=%22.%22+t;else%20n=%22%22;e.getAttribute(%22id%22)%26%26(i=e.getAttribute(%22id%22),i=%22%23%22+i);var%20r=document.querySelector(%22.infosSelectionForW3C%22);r.textContent=o+%22%22+i+n}function%20t(){window.removeEventListener(%22mouseover%22,s,!1),window.removeEventListener(%22mouseout%22,l,!1)}function%20n(e,o){t(),e=JSON.parse(e);for(var%20n=document.createElement(%22ul%22),i=e.messages,d=0;i[d];){var%20r=document.createElement(%22li%22),a=i[d].type.toString(),s=i[d].message.toString(),l=document.createTextNode(s);if(r.classList.add(a),r.appendChild(l),i[d].extract){var%20p=document.createElement(%22code%22),c=i[d].extract.toString(),u=document.createTextNode(c);p.appendChild(u),r.appendChild(p)}n.appendChild(r),d++}if(0===d){var%20f=document.createTextNode(%22No%20warnings,%20no%20error%20on%20this%20snippet%22),m=document.createElement(%22li%22);m.appendChild(f),n.appendChild(m)}var%20x=document.createElement(%22textarea%22),h=document.createTextNode(o);x.classList.add(%22snippetAnalysed%22),x.appendChild(h);var%20v=document.querySelector(%22.popinValidator%22);v.appendChild(n),v.appendChild(x),v.classList.add(%22showPopin%22)}function%20i(e){var%20o=new%20XMLHttpRequest;o.open(%22post%22,%22https://validator.nu/%3Fout=json%22,!0),o.setRequestHeader(%22Content-Type%22,%22text/html;charset=UTF-8%22),o.onload=function(t){4===o.readyState%26%26(200===o.status%3Fn(o.responseText,e):alert(o.statusText))},o.onerror=function(e){alert(o.statusText)},o.send(e)}function%20d(){var%20e=%22%22,o=%22%22;%22BODY%22!=f.nodeName.toString()%26%26(e=%22%3Cbody%3E%22,o=%22%3C/body%3E%22);var%20t=f.outerHTML;t=%22%3C!doctype%20html%3E%3Chtml%20lang='en'%3E%3Chead%3E%3Cmeta%20charset='utf-8'%3E%3Ctitle%3EShell%3C/title%3E%3C/head%3E%22+e+t+o+%22%3C/html%3E%22,i(t)}function%20r(){document.querySelector(%22.infosSelectionForW3C%22).remove(),document.querySelector(%22.LazyValidatorShortCut%22).remove(),document.querySelector(%22.popinValidator%22).remove()}function%20a(){var%20e=document.createElement(%22span%22);e.className=%22infosSelectionForW3C%22;var%20o=document.createTextNode(%22[nodeName,%20class%20%26%20ID]%22);e.appendChild(o),document.body.appendChild(e);var%20t=document.createElement(%22span%22);t.className=%22LazyValidatorShortCut%22;var%20n=document.createTextNode(%22TOP%20ARROW%20key%20=%3E%20move%20to%20Node%20node%20the%20overred%20one.%20V%20key%20=%3E%20Validate%20the%20fragment%20you%20focus%20on.%20ECHAP%20KEY%20=%3E%20remove%20the%20behavior%22);t.appendChild(n),document.body.appendChild(t);var%20i=document.createElement(%22div%22);i.className=%22popinValidator%22,document.body.appendChild(i)}function%20s(e){f=e.target,f.classList.add(%22validatorw3cfocus%22),o(f)}function%20l(e){f.classList.remove(%22validatorw3cfocus%22)}function%20p(e){if(118==e.charCode%26%26(e.preventDefault(),d()),38==e.keyCode%26%26(e.preventDefault(),0!==f%26%26f.parentNode%26%26%22HTML%22!=f.parentNode.nodeName.toString()%26%26(f.classList.remove(%22validatorw3cfocus%22),f=f.parentNode,f.classList.add(%22validatorw3cfocus%22),o(f))),27==e.keyCode){t(),document.body.removeEventListener(%22keypress%22,p);var%20n=document.querySelector(%22.validatorw3cfocus%22);n.classList.remove(%22validatorw3cfocus%22),r(),u=1,f=document.querySelector(%22body%22)}}function%20c(){u=0,a(),document.body.addEventListener(%22keypress%22,p),window.addEventListener(%22mouseover%22,s,!1),window.addEventListener(%22mouseout%22,l,!1)}var%20u=1,f=document.querySelector(%22body%22);e(),1===u%26%26c()})()}();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment