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 m_tracking = Debugging; var m_date = new Date;

this.track = track; /*--*/ function track(message) { if (m_tracking) console.log(m_date.toTimeString + " " + message); } }

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; } }

/*************************************************************************************************** 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 m_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 (m_applies) { track.track("Rule applies."); } else { track.track("Rule doesn't apply."); }

/*2. Determine if the document complies with this rule.*/ var m_complies = false; var m_message = document.createElement("DIV"); m_message.style.position = "relative"; m_message.style.width = "auto"; m_message.style.height= "auto"; m_message.style.fontSize = "12px"; if (m_applies) { var Categories = document.getElementById("WikiaArticleCategories"); if (Categories) { if (Categories.getElementsByTagName("ul").length > 0) m_complies = true; }   if (m_complies) { track.track("Document complies."); m_message.appendChild(document.createTextNode("This page has at least one category.")); m_message.style.color = "green"; } else { track.track("Document does not comply."); m_message.appendChild(document.createTextNode("This page has no category.")); m_message.style.color = "red"; } }

this.getApplies = getApplies; this.getComplies = getComplies; this.getMessage = getMessage; /*  * This function returns true if this rule applies to the current ducument, false otherwise. */ function getApplies { return m_applies; } /*   * This function returns true if the current ducument complies with this rule, false otherwise. */ function getComplies { return m_complies; } /*--*/  function getMessage { return m_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 m_applies = !Search("\?action=edit", URL); if (m_applies) { track.track("Rule applies."); } else { track.track("Rule doesn't apply."); }

/*2. Determine if the document complies with this rule.*/ var m_complies = false; var m_message = document.createElement("DIV"); m_message.style.position = "relative"; m_message.style.width = "auto"; m_message.style.height= "auto"; m_message.style.fontSize = "12px"; if (m_applies) { var Redirects = document.getElementsByClassName("mw-redirect"); for (var i = 0; i != Redirects.length; ++i) Redirects[i].style.background = "red"; var m_complies = (Redirects.length == 0); if (m_complies) { track.track("Document complies."); m_message.appendChild(document.createTextNode( "This page has no links to redirection pages.")); m_message.style.color = "green"; } else { track.track("Document does not comply."); m_message.appendChild(document.createTextNode("This page has " + Redirects.length.toString + " links to redirection pages." +       " Style Guard highlighted these links with a red background.")); m_message.style.color = "red"; } }

this.getApplies = getApplies; this.getComplies = getComplies; this.getMessage = getMessage; /*  * This function returns true if this rule applies to the current ducument, false otherwise. */ function getApplies { return m_applies; } /*   * This function returns true if the current ducument complies with this rule, false otherwise. */ function getComplies { return m_complies; } /*--*/  function getMessage { return m_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 m_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 (m_applies) { track.track("Rule applies."); } else { track.track("Rule doesn't apply."); }

/*2. Determine if the document complies with this rule.*/ var m_complies = false; var m_message = document.createElement("DIV"); m_message.style.position = "relative"; m_message.style.width = "auto"; m_message.style.height= "auto"; m_message.style.fontSize = "12px"; if (m_applies) { m_complies = true; var text = document.body.textContent; var words = text.split(" "); for (var i = 0; i < words.length; ++i) { if (words[i].toLowerCase == "you") m_complies = false; }   if (m_complies) { track.track("Document complies."); m_message.appendChild(document.createTextNode("This page does not contain the word \"you\".")); m_message.style.color = "green"; } else { track.track("Document does not comply."); m_message.appendChild(document.createTextNode("This page does contain the word \"you\" at least once.")); m_message.style.color = "red"; } }

this.getApplies = getApplies; this.getComplies = getComplies; this.getMessage = getMessage; /*  * This function returns true if this rule applies to the current ducument, false otherwise. */ function getApplies { return m_applies; } /*   * This function returns true if the current ducument complies with this rule, false otherwise. */ function getComplies { return m_complies; } /*--*/  function getMessage { return m_message; } track.track("End of Rule3"); }

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

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

/*************************************************************************************************** User-Defined Object: ModeSelector function ModeSelector { track.track("Begin of ModeSelector"); var m_mode = new Mode; var m_attributeSelected = CreateAttribute("selected", "selected"); var m_optionDisabled = document.createElement("OPTION"); var m_optionCollapsed = document.createElement("OPTION"); var m_optionExpanded = document.createElement("OPTION"); var m_optionAutomatic = document.createElement("OPTION");

/*4. Generate HMTL.-*/ track.track("Generate HMTL"); var m_html; m_optionDisabled.setAttributeNode(CreateAttribute("value", "disabled")); m_optionDisabled.setAttributeNode(m_attributeSelected); m_optionDisabled.innerHTML = "Disabled"; m_optionCollapsed.setAttributeNode(CreateAttribute("value", "collapsed")); m_optionCollapsed.innerHTML = "Collapsed"; m_optionExpanded.setAttributeNode(CreateAttribute("value", "expanded")); m_optionExpanded.innerHTML = "Expanded"; m_optionAutomatic.setAttributeNode(CreateAttribute("value", "automatic")); m_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(m_mode.get); m_html = document.createElement("SELECT"); m_html.style.position = "relative"; m_html.style.cssFloat = "right"; //m_html.style.left = "35px"; //m_html.style.top = "5px"; m_html.style.width = "120px"; m_html.style.marginTop = "5px"; m_html.setAttributeNode(CreateAttribute("name", "mode")); m_html.appendChild(m_optionDisabled); m_html.appendChild(m_optionCollapsed); m_html.appendChild(m_optionExpanded); m_html.appendChild(m_optionAutomatic); m_html.onchange=function{modeOnChange};

/*5. Define member functions.---*/ this.getHTML = getHTML; this.getMode = getMode; this.setMode = setMode; /*--*/ function getHTML { return m_html; } /*--*/  function getMode { return m_mode.get; } /*--*/  function modeOnChange { track.track("Begin of ModeSelector::modeOnChange."); m_mode.set(m_html.value); } /*--*/  function setMode(cookieValue) { track.track("Begin of ModeSelector::setMode(" + cookieValue + ")"); m_mode.set(cookieValue); m_optionDisabled.removeAttribute("selected"); m_optionCollapsed.removeAttribute("selected"); m_optionExpanded.removeAttribute("selected"); m_optionAutomatic.removeAttribute("selected"); if (cookieValue == "disabled") { m_optionDisabled.setAttributeNode(m_attributeSelected); }   if (cookieValue == "collapsed") { m_optionCollapsed.setAttributeNode(m_attributeSelected); }   if (cookieValue == "expanded") { m_optionExpanded.setAttributeNode(m_attributeSelected); }   if (cookieValue == "automatic") { m_optionAutomatic.setAttributeNode(m_attributeSelected); } }  track.track("End of ModeSelector"); }

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

/*1. Validate the current document.-*/ track.track("Validate the current document"); var m_rule1 = new Rule1; var m_rule2 = new Rule2; var m_rule3 = new Rule3; var m_complies = true; if (m_rule1.getApplies) m_complies = m_rule1.getComplies && m_complies; if (m_rule2.getApplies) m_complies = m_rule2.getComplies && m_complies; if (m_rule3.getApplies) m_complies = m_rule3.getComplies && m_complies;

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

/*3. Determine the height of this control.--*/ track.track("Determine the height of this control"); var m_controlHeight; if (m_complies) { m_controlHeight = "32px"; } else { m_controlHeight = "64px"; }

/*4. Generate HMTL.-*/ track.track("Generate HMTL"); var m_html; var m_modeSelector = new ModeSelector; m_html = document.createElement("FORM"); m_html.style.display = "table"; m_html.style.position = "relative"; //m_html.style.left = 100%; //m_html.style.top = top; m_html.style.cssFloat = "right"; m_html.style.width = "100%"; m_html.style.height = m_controlHeight; m_html.style.zIndex = "1001"; //m_html.style.margin-left = "-200px"; m_html.style.borderWidth = "1px"; m_html.style.borderStyle = "solid"; m_html.style.borderColor = "#303030"; m_html.appendChild(m_modeSelector.getHTML); m_html.appendChild(m_dog); var m_messages; m_messages = document.createElement("DIV"); m_messages.style.visibility = "visible"; m_messages.style.display = "inline-block"; m_messages.style.overflow = "auto"; m_messages.style.position = "relative"; m_messages.style.cssFloat = "left"; m_messages.style.width = "auto"; m_messages.style.height = "100%"; m_messages.style.textAlign = "left"; if (m_rule1.getApplies) m_messages.appendChild(m_rule1.getMessage); if (m_rule2.getApplies) m_messages.appendChild(m_rule2.getMessage); if (m_rule3.getApplies) m_messages.appendChild(m_rule3.getMessage); m_html.appendChild(m_messages);

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

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

var styleGuardControl = new StyleGuardControl;

//var Title = document.createElement("H2"); //Title.appendChild(document.createTextNode("Style Guard")); // //var Version = document.createElement("P"); //Version.appendChild(document.createTextNode("version " + VersionNumber)); // //var StyleGuard = document.createElement("DIV"); //StyleGuard.style.position = "fixed"; //StyleGuard.style.left = "25%"; //StyleGuard.style.top= "25%"; //StyleGuard.style.width = "50%"; //StyleGuard.style.height = "auto"; //StyleGuard.style.minHeight = "50px"; //StyleGuard.style.backgroundColor = "black"; //StyleGuard.setAttributeNode(CreateAttribute("id", "StyleGuard")); //StyleGuard.appendChild(Dog); //StyleGuard.appendChild(Title); //StyleGuard.appendChild(Version); //if (typeof Message1 != "undefined") { // StyleGuard.appendChild(Message1); //} //if (typeof Message2 != "undefined") { // StyleGuard.appendChild(Message2); //} // //var StyleGuardSpaceReservation = document.createElement("DIV"); //StyleGuardSpaceReservation.style.position = "relative"; //StyleGuardSpaceReservation.style.cssFloat = "right"; //StyleGuardSpaceReservation.style.width = "15%"; //StyleGuardSpaceReservation.style.height = "50px"; //StyleGuardSpaceReservation.style.backgroundColor = "transparent"; //StyleGuard.setAttributeNode(CreateAttribute("id", "StyleGuard-SpaceReservation"));

var WikiaTopAds = document.getElementById("WikiaTopAds"); WikiaTopAds.style.width = "100%"; WikiaTopAds.style.height = styleGuardControl.getHeight; WikiaTopAds.style.backgroundColor = "black"; WikiaTopAds.style.padding = "0px"; WikiaTopAds.appendChild(styleGuardControl.getHTML);

//var C = styleGuardControl.getHTMLElement(0, 50); //var WikiaPage = document.getElementById("WikiaPage"); //WikiaPage.parentNode.insertBefore(StyleGuard, WikiaPage); //WikiaPage.parentNode.insertBefore(StyleGuardSpaceReservation, StyleGuard); //WikiaPage.parentNode.insertBefore(C, WikiaPage);

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