User:Documentalist/common.js

/*Ruler- 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 --*/

/*Object Browser.*/ //importArticle({type: "script", article:"User:Documentalist/Objects.js"}); //importArticle({type: "script", article:"User:Documentalist/ObjectBrowser.js"}); //importArticle({type: "script", article:"User:Documentalist/StyleGuard.js"}); /*Ruler- 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 --*/

/*************************************************************************************************** Style Guard var VersionNumber = "0.4+"; var Debugging = true;

/*************************************************************************************************** User-Defined Object: Track Track is used to write information to the console that can be used to trace this program. function Track { var tracking = Debugging; var date = new Date; /*--*/ this.track = function(message) { if (tracking) console.log(date.toTimeString + " " + message); } }

var track = new Track;

/*************************************************************************************************** Utitilies /**/ function CreateAttribute(Name, Value) { var Att = document.createAttribute(Name); Att.value = Value; return Att; } /**/ function getCookie(name) { track.track("Begin of getCookie(name:" + name + ")"); var ca = document.cookie.split(";"); for (var i = 0; i < ca.length; ++i) { var c = ca[i].trim; if (c.substr(0, name.length + 1) == name + "=") { track.track("returning: " + c.substr(name.length + 1)); return c.substr(name.length + 1); } }  track.track("returning: null"); return null; } /**/ function Search(SearchThis, In) { for (var Index = 0; Index < In.length; ++Index) { if (In.substr(Index, SearchThis.length) == SearchThis) return true; } return false; } /**/ function setCookie(path, name, value, expiryDays) { track.track(    "Begin of setCookie(path:" + path + ", name:" + name + ", value:" + value + ", expiry days:" + expiryDays + ")"); if (expiryDays == null) { //track.track("Begin of setCookie(path:" + path + ", name:" + name + ", value: " + value + ")"); var concatenation = name + "=" + escape(value) + "; path=" + path; track.track("concatenation: " + concatenation); document.cookie = concatenation; } else { var expiryDate = new Date; expiryDate.setDate(expiryDate.getDate + expiryDays); var concatenation = name + "=" + escape(value) + "; expires=" + expiryDate.toUTCString + "; path=" + path; track.track("concatenation: " + concatenation); document.cookie = concatenation; } }

/*************************************************************************************************** User-Defined Object: Message This message is the kind of message a rule reports. function Message(link, text, color) { track.track("Begin of Message(link: " + link + ", text: " + text + ", color: " + color + ")"); var messageText = document.createElement("TD"); messageText.style.fontSize = "12px"; //message.appendChild(document.createTextNode("This page has at least one category.")); messageText.style.width = "auto"; messageText.style.height = "auto"; messageText.style.color = color; messageText.innerHTML = text; var messageLink = document.createElement("TD"); messageLink.style.width = "50px"; messageLink.style.height = "auto"; messageLink.style.fontSize = "12px"; messageText.innerHTML = link; var message = document.createElement("TR"); message.style.position = "relative"; message.style.width = "100%"; message.style.height= "auto"; message.style.fontSize = "12px"; message.appendChild(messageLink); message.appendChild(messageText);

/*--*/ this.getHTML = function { return message; } track.track("End of Message"); }

/*************************************************************************************************** Style Guard Rules

/*************************************************************************************************** User-Defined Object: Rule1 Rule: This page has at least one category. function Rule1 { track.track("Begin of Rule1"); /*1. Determine if rule applies.*/ var url = document.URL; console.log(url); var applies = true /*Special page.*/ && (url.substr(0, 43) != "http://elderscrolls.wikia.com/wiki/Special:") /*User talk page.*/ && (url.substr(0, 45) != "http://elderscrolls.wikia.com/wiki/User_talk:") /*Edit page.*/ && (!Search("?action=edit", url)) /*Forum board page.*/ && (url.substr(0, 41) != "http://elderscrolls.wikia.com/wiki/Board:") /*Forum thread page.*/ && (url.substr(0, 42) != "http://elderscrolls.wikia.com/wiki/Thread:") /*User subpage.*/ && (url.substr(0, 40) != "http://elderscrolls.wikia.com/wiki/User:") || !Search("/", url.substr(40)) /*Style page.*/ && (url.substr(url.length - 4) != ".css") /*Script page.*/ && (url.substr(url.length - 3) != ".js"); if (applies) { track.track("Rule applies."); } else { track.track("Rule doesn't apply."); }

/*2. Determine if the document complies with this rule.*/ var complies = false; var text; var color; var message; if (applies) { var categories = document.getElementById("WikiaArticleCategories"); if (categories) { if (categories.getElementsByTagName("ul").length > 0) complies = true; }   if (complies) { track.track("Document complies."); text = "This page has at least one category."; color = "green"; } else { track.track("Document does not comply."); text = "This page has no category."; color = "red"; }   message = new Message(      "Rule 1", text, color); }

/*--*/ this.getApplies  = function { return applies;  } this.getComplies = function { return complies; } this.getMessage = function { return message;  } track.track("End of Rule1"); } /*************************************************************************************************** User-Defined Object: Rule2 Rule: Every WikiaPage shall have no links to redirection pages. function Rule2 { track.track("Begin of Rule2"); /*1. Determine if rule applies.*/ var url = document.URL; console.log(url); var applies = !Search("\?action=edit", url); if (applies) { track.track("Rule applies."); } else { track.track("Rule doesn't apply."); }

/*2. Determine if the document complies with this rule.*/ var complies = false; var text; var color; var message; if (applies) { var Redirects = document.getElementsByClassName("mw-redirect"); for (var i = 0; i != Redirects.length; ++i) Redirects[i].style.background = "red"; var complies = (Redirects.length == 0); if (complies) { track.track("Document complies."); text = "This page has no links to redirection pages."; color = "green"; } else { track.track("Document does not comply."); text = "This page has " + Redirects.length.toString + " links to redirection pages." +       " Style Guard highlighted these links with a red background."; color = "red"; }   message = new Message(      "Rule 2", text, color); }

/*--*/ this.getApplies  = function { return applies;  } this.getComplies = function { return complies; } this.getMessage = function { return message;  } track.track("End of Rule2"); }

/*************************************************************************************************** User-Defined Object: Rule3 Rule: The page shall not contain the word "you". function Rule3 { track.track("Begin of Rule3"); /*1. Determine if rule applies.*/ var url = document.URL; console.log(url); var applies = true /*Special page.*/ && (url.substr(0, 43) != "http://elderscrolls.wikia.com/wiki/Special:") /*User talk page.*/ && (url.substr(0, 45) != "http://elderscrolls.wikia.com/wiki/User_talk:") /*Edit page.*/ && (!Search("?action=edit", url)) /*Forum board page.*/ && (url.substr(0, 41) != "http://elderscrolls.wikia.com/wiki/Board:") /*Forum thread page.*/ && (url.substr(0, 42) != "http://elderscrolls.wikia.com/wiki/Thread:") /*User subpage.*/ && (url.substr(0, 40) != "http://elderscrolls.wikia.com/wiki/User:") || !Search("/", url.substr(40)) /*Style page.*/ && (url.substr(url.length - 4) != ".css") /*Script page.*/ && (url.substr(url.length - 3) != ".js"); if (applies) { track.track("Rule applies."); } else { track.track("Rule doesn't apply."); }

/*2. Determine if the document complies with this rule.*/ var complies = false; var text; var color; var message; if (applies) { complies = true; var text = document.body.textContent; var words = text.split(" "); for (var i = 0; i < words.length; ++i) { if (words[i].toLowerCase == "you") complies = false; }   if (complies) { track.track("Document complies."); text = "This page does not contain the word \"you\"."; color = "green"; } else { track.track("Document does not comply."); text = "This page does contain the word \"you\" at least once."; color = "red"; }   message = new Message(      "Rule 3", text, color); }

/*--*/ this.getApplies  = function { return applies;  } this.getComplies = function { return complies; } this.getMessage = function { return message;  } track.track("End of Rule3"); }

/*************************************************************************************************** User-Defined Object: Mode function Mode { track.track("Begin of Mode"); var cookieName = "StyleGuardMode"; var cookieValue = getCookie(cookieName); if (cookieValue == null) { set("automatic"); } else { cookieValue = normalize(cookieValue); }

/*--*/ function normalize(cookieValue) { if (cookieValue == "disabled" || cookieValue == "collapsed" ||       cookieValue == "expanded" || cookieValue == "automatic") { return cookieValue; }   console.log("Mode::normalize(" + cookieValue + "): Invalid cookie value."); return "automatic"; } /*--*/  this.get = function { return cookieValue; } /*--*/  this.set = function(value) { track.track("Begin of Mode::set(value: " + value + ")"); cookieValue = normalize(value); setCookie("/", cookieName, cookieValue, 100); } }

/*************************************************************************************************** User-Defined Object: ModeSelector function ModeSelector { track.track("Begin of ModeSelector"); var mode = new Mode; var attributeSelected = CreateAttribute("selected", "selected"); var optionDisabled = document.createElement("OPTION"); var optionCollapsed = document.createElement("OPTION"); var optionExpanded = document.createElement("OPTION"); var optionAutomatic = document.createElement("OPTION");

/*4. Generate HMTL.-*/ track.track("Generate HMTL"); var html; optionDisabled.setAttributeNode(CreateAttribute("value", "disabled")); optionDisabled.setAttributeNode(attributeSelected); optionDisabled.innerHTML = "Disabled"; optionCollapsed.setAttributeNode(CreateAttribute("value", "collapsed")); optionCollapsed.innerHTML = "Collapsed"; optionExpanded.setAttributeNode(CreateAttribute("value", "expanded")); optionExpanded.innerHTML = "Expanded"; optionAutomatic.setAttributeNode(CreateAttribute("value", "automatic")); optionAutomatic.innerHTML = "Automatic"; /*Call to setMode must be after the creation of the options to avoid "InUseAttributeError", and before appending the options to the selector to avoid that the mode set, isn't visible.*/ setMode(mode.get); html = document.createElement("SELECT"); html.style.display = "none"; //"relative"; html.style.position = "relative"; html.style.cssFloat = "right"; //html.style.left = "35px"; //html.style.top = "5px"; html.style.width = "120px"; html.style.marginTop = "5px"; html.setAttributeNode(CreateAttribute("name", "mode")); html.appendChild(optionDisabled); html.appendChild(optionCollapsed); html.appendChild(optionExpanded); html.appendChild(optionAutomatic); html.onchange=function{modeOnChange};

/*5. Define member functions.---*/ /*--*/ this.getHTML = function { return html; } /*--*/  this.getMode = function { return mode.get; } /*--*/  function modeOnChange { track.track("Begin of ModeSelector::modeOnChange."); mode.set(html.value); } /*--*/  function setMode(cookieValue) { track.track("Begin of ModeSelector::setMode(" + cookieValue + ")"); mode.set(cookieValue); optionDisabled.removeAttribute("selected"); optionCollapsed.removeAttribute("selected"); optionExpanded.removeAttribute("selected"); optionAutomatic.removeAttribute("selected"); if (cookieValue == "disabled") { optionDisabled.setAttributeNode(attributeSelected); }   if (cookieValue == "collapsed") { optionCollapsed.setAttributeNode(attributeSelected); }   if (cookieValue == "expanded") { optionExpanded.setAttributeNode(attributeSelected); }   if (cookieValue == "automatic") { optionAutomatic.setAttributeNode(attributeSelected); } }  track.track("End of ModeSelector"); }

/*************************************************************************************************** User-Defined Object: StyleGuardControl function StyleGuardControl(width) { track.track("Begin of StyleGuardControl(width: " + width + ")");

/*1. Validate the current document.-*/ track.track("Validate the current document"); var rule1 = new Rule1; var rule2 = new Rule2; var rule3 = new Rule3; var complies = true; if (rule1.getApplies) complies = rule1.getComplies && complies; if (rule2.getApplies) complies = rule2.getComplies && complies; if (rule3.getApplies) complies = rule3.getComplies && complies;

/*2. Determine the appropriate kind of dog.-*/ track.track("Determine the appropriate kind of dog"); var dog = document.createElement("IMG"); var dogHeight; if (complies) { dog.setAttributeNode(CreateAttribute( "src", "http://static3.wikia.nocookie.net/elderscrolls/images/7/73/Transparent_bulldog.png")); dogHeight = "32"; dogWidth = "32"; } else { dog.setAttributeNode(CreateAttribute( "src", "http://static2.wikia.nocookie.net/elderscrolls/images/0/0c/Transparent_mean_bulldog.png")); dogHeight = "64"; dogWidth = "85"; } dog.style.position = "relative"; dog.style.cssFloat = "right"; dog.setAttributeNode(CreateAttribute("height", dogHeight + "px")); dog.setAttributeNode(CreateAttribute("width", dogWidth + "px"));

/*3. Determine the height of this control.--*/ track.track("Determine the height of this control"); var height = dogHeight;

/*4. Generate HMTL.-*/ track.track("Generate HMTL"); var title = document.createElement("H2"); title.appendChild(document.createTextNode("Style Guard")); title.style.position = "relative"; title.style.cssFloat = "left"; title.style.height = "auto"; title.style.width = "90px"; title.style.fontSize = "14px"; title.style.marginTop = "7px"; //title.style.marginRight = "2px;" title.style.marginBottom = "auto";

var version = document.createElement("DIV"); version.appendChild(document.createTextNode("version " + VersionNumber)); version.style.position = "relative"; version.style.cssFloat = "left"; version.style.width = "50px"; version.style.height = height; version.style.fontSize = "12px"; var modeSelector = new ModeSelector;

var messages; var tableWidth = width - 90 /*title*/ - 50 /*version*/ - dogWidth; track.track("tableWidth: " + tableWidth); messages = document.createElement("TABLE"); messages.style.visibility = "visible"; messages.style.display = "table"; messages.style.overflowY = "auto"; messages.style.position = "relative"; messages.style.cssFloat = "right"; messages.style.width = tableWidth + "px"; messages.style.height = height; messages.style.maxHeight = height; messages.style.textAlign = "left"; messages.style.fontSize = "14px"; messages.marginTop = "auto" if (rule1.getApplies) messages.appendChild(rule1.getMessage.getHTML); if (rule2.getApplies) messages.appendChild(rule2.getMessage.getHTML); if (rule3.getApplies) messages.appendChild(rule3.getMessage.getHTML);

var html; html = document.createElement("FORM"); html.style.display = "table"; html.style.position = "fixed"; html.style.right = "0px"; html.style.top = "37px"; //html.style.cssFloat = "right"; html.style.width = width + "px"; html.style.height = height; html.style.zIndex = "1001"; html.style.marginLeft = "0px"; html.style.marginTop = "0px"; html.style.borderWidth = "1px"; html.style.borderStyle = "solid"; html.style.borderColor = "#303030"; html.appendChild(title); html.appendChild(version); html.appendChild(modeSelector.getHTML); html.appendChild(dog); html.appendChild(messages);

/*5. Define member functions.---*/ //this.getHeight = function { return height; } this.getHTML  = function { return html;   } track.track("End of StyleGuardControl"); }

/*************************************************************************************************** Main track.track ("Begin of file.");

var clientWidth=window.innerWidth track.track("client width: " + clientWidth);
 * document.documentElement.clientWidth
 * document.body.clientWidth;

var styleGuardControl = new StyleGuardControl(clientWidth - 16 /*vertical scroll bar*/) var html = styleGuardControl.getHTML; var WikiaTopAds = document.getElementById("WikiaTopAds"); WikiaTopAds.style.width = "100%"; WikiaTopAds.style.height = html.style.borderTop + html.style.paddingTop + html.style.height + html.style.paddingBottom + html.style.borderBottom + "px"; WikiaTopAds.style.backgroundColor = "black"; WikiaTopAds.style.padding = "0px"; WikiaTopAds.style.zIndex = "1"; WikiaTopAds.appendChild(html);

track.track ("End of file.");