User:The Crusader of Truth/common.js

/*Ruler- 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 --*/ /*************************************************************************************************** Style Guard var VersionNumber = "0.7"; var Debugging = false; /*************************************************************************************************** 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 find(string, c, pos) { for (var i = pos; i < string.length; ++i) { if (string[i] == c) return i; } 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(ruleId, complies, text) { track.track("Begin of Message(ruleId: " + ruleId + ", complies: " + complies + ", text: " + text + ")"); var wiki = document.URL.substr(0, find(document.URL, "/", 0)) + "/wiki"; var a = "" + "Rule " + ruleId + ""; var td1 = "" + a + " "; var td2 = "" + (complies? "&#x2714": "&#x2717;") + " "; var td3 = "" + text + " "; var innerHTML = td1 + td2 + td3; var html = "" + innerHTML + " "; /*--*/ this.getHTML = function { return html; }; track.track("End of Message"); } /*************************************************************************************************** Style Guard Rules /*************************************************************************************************** User-Defined Object: Rule1 Rule: The page has at least one category. function Rule1 { track.track("Begin of Rule1"); var html; /*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 = true; if (applies) { var categories = document.getElementById("articleCategories"); if (categories === null ||(categories.getElementsByTagName("ul").length === 0)) { complies = false; }   var text; if (complies) { track.track("Document complies."); text = "This page has at least one category."; } else { track.track("Document does not comply."); text = "This page has no category."; }   message = new Message(1, complies, text); html = message.getHTML; } /*--*/  this.getApplies  = function { return applies;  }; this.getComplies = function { return complies; }; this.getHTML    = function { return html;     }; track.track("End of Rule1"); } /*************************************************************************************************** User-Defined Object: Rule2 Rule: The page has no links to redirection pages. function Rule2 { track.track("Begin of Rule2"); var html; /*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 = true; 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); var text; if (complies) { track.track("Document complies."); text = "This page has no links to redirection pages."; } 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."; }   message = new Message(2, complies, text); html = message.getHTML; } /*--*/  this.getApplies  = function { return applies;  }; this.getComplies = function { return complies; }; this.getHTML    = function { return html;     }; track.track("End of Rule2"); } /*************************************************************************************************** User-Defined Object: Rule3 Rule: The page does not contain the word "you". function Rule3 { track.track("Begin of Rule3"); var html; /*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 = true; if (applies) { var text = document.getElementById('WikiaArticle').textContent; var words = text.split(" "); for (var i = 0; i < words.length; ++i) { if (words[i].toLowerCase == "you") complies = false; }   var text; if (complies) { track.track("Document complies."); text = "This page does not contain the word \"you\"."; } else { track.track("Document does not comply."); text = "This page contains the word \"you\" at least once."; }   message = new Message(3, complies, text); html = message.getHTML; } /*--*/  this.getApplies  = function { return applies;  }; this.getComplies = function { return complies; }; this.getHTML    = function { return html;     }; track.track("End of Rule3"); } /*************************************************************************************************** User-Defined Object: Rule4 Rule: The page does not contain the word "I". function Rule4 { track.track("Begin of Rule4"); var html; /*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; if (applies) { complies = true; var text = document.getElementById('WikiaArticle').textContent; var words = text.split(" "); for (var i = 0; i < words.length; ++i) { if (words[i].toLowerCase == "i") complies = false; }   if (complies) { track.track("Document complies."); text = "This page does not contain the word \"I\"."; } else { track.track("Document does not comply."); text = "This page contains the word \"I\" at least once."; }   message = new Message(4, complies, text); html = message.getHTML; } /*--*/  this.getApplies  = function { return applies;  }; this.getComplies = function { return complies; }; this.getHTML    = function { return html;     }; track.track("End of Rule4"); } /*************************************************************************************************** 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, scrollTop) { 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 rule4 = new Rule4; var complies = true; if (rule1.getApplies) complies = rule1.getComplies && complies; if (rule2.getApplies) complies = rule2.getComplies && complies; if (rule3.getApplies) complies = rule3.getComplies && complies; if (rule4.getApplies) complies = rule4.getComplies && complies; /*2. Determine the appropriate kind of dog.-*/ track.track("Determine the appropriate kind of dog"); var dogHeight; var dogURL; if (complies) { dogURL = "http://static3.wikia.nocookie.net/elderscrolls/images/7/73/Transparent_bulldog.png"; dogHeight = 32; dogWidth = 32; } else { dogURL = "http://static2.wikia.nocookie.net/elderscrolls/images/0/0d/Transparent_mean_bulldog" + "_modified_fixed.png"; //http://static2.wikia.nocookie.net/elderscrolls/images/0/0c/Transparent_mean_bulldog.png";   dogHeight = 64;    dogWidth = 85;  }  var dog = ""; /*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 version = " version " + VersionNumber + " ";  var title = " Style Guard ";  var innerHTML = "";  if (rule1.getApplies) innerHTML += rule1.getHTML;  if (rule2.getApplies) innerHTML += rule2.getHTML;  if (rule3.getApplies) innerHTML += rule3.getHTML;  if (rule4.getApplies) innerHTML += rule4.getHTML;  var messages = " ";  innerHTML = version + title + dog + messages;  html = "" + innerHTML + " ";  /*5. Define member functions.---*/  this.getHTML   = function { return html; };  track.track("End of StyleGuardControl"); } /*************************************************************************************************** Main track.track ("Begin of file."); var styleGuardControl = new StyleGuardControl; $("#TOP_RIGHT_BOXAD").after(""); document.getElementById("styleGuard").innerHTML = styleGuardControl.getHTML; track.track ("End of file.");