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;

/*************************************************************************************************** Utitilies /**/ function CreateAttribute(Name, Value) { var Att = document.createAttribute(Name); Att.value = Value; return Att; } /**/ function GetCookie(cname) { if (Debugging) console.log("Begin of getCookie"); var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i<ca.length; i++) { var c = ca[i].trim; console.log(c); if (c.indexOf(name)==0) return c.substring(name.length,c.length); } 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(Name, Value, ExpiryDays) { if (Debugging) console.log("Begin of setCookie"); var ExpiryDate = new Date; ExpiryDate.setDate(ExpiryDate.getDate + ExpiryDays); var CookieValue = escape(Value) + ((ExpiryDays == null)? "": "; expires="+ExpiryDate.toUTCString); document.cookie= Name + "=" + CookieValue; }

/*************************************************************************************************** Style Guard Rules /*************************************************************************************************** User-Defined Object: Rule1 Rule: Every WikiaPage shall have a category. function Rule1 { this.apply = apply; this.check = check; /*  * This function returns true if this rule applies to the current ducument, false otherwise. */ function ApplyRule { var URL = document.URL; console.log(URL); var Exception = false /*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"); return ! Exception; } /*   * This function returns true if the current ducument complies with this rule, false otherwise. */ function check(message) { var Categories = document.getElementById("WikiaArticleCategories"); var success = (Categories != null); Message.style.position = "relative"; Message.style.width = "100%"; if (success) { Message.appendChild(document.createTextNode("This page has at least one category.")); Message.style.color = "green"; } else { Message.appendChild(document.createTextNode("This page has no category.")); Message.style.color = "red"; }   return success; } } /*************************************************************************************************** User-Defined Object: Rule2 Rule: Every WikiaPage shall have no links to redirection pages. function Rule2 { this.apply = apply; this.check = check; /*  * This function returns true if this rule applies to the current ducument, false otherwise. */ function apply { var URL = document.URL; console.log(URL); return (Search("\?action=edit", URL) == false); } /*   * This function returns true if the current ducument complies with this rule, false otherwise. */ function check(message) { var Redirects = document.getElementsByClassName("mw-redirect"); for (var i = 0; i != Redirects.length; ++i) Redirects[i].style.background = "red"; var Success = (Redirects.length == 0); Message.style.position = "relative"; Message.style.width = "100%"; if (Success) { Message.appendChild(document.createTextNode( "This page has no links to redirection pages.")); Message.style.color = "green"; } else { Message.appendChild(document.createTextNode("This page has " + Redirects.length.toString + " links to redirection pages." +       " Style Guard highlighted these links with a red background.")); Message.style.color = "red"; }   return Success; } }you

/*************************************************************************************************** User-Defined Object: Mode function Mode { if (Debugging) console.log("Begin of Mode"); /*Declared these data members with "var" instead of with "this." to make then NOT accessable from the outside.*/ var m_initialized = false; var m_cookieName = "StyleGuardMode"; var m_cookieValue = get; 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 { if (!m_initialized) { var cookieValue = GetCookie(m_cookieName); if (cookieValue == null) { set("automatic"); } else { m_cookieValue = normalize(cookieValue); }     m_initialized = true; }   return m_cookieValue; } /*--*/  function set(cookieValue) { if (Debugging) console.log("Begin of Mode::set"); m_cookieValue = normalize(cookieValue); document.cookie= m_cookieName + "=" + m_cookieValue; SetCookie(m_cookieName, m_cookieValue, 100); //console.log(GetCookie(m_cookieName)); //console.log(m_cookieValue); //if (m_cookieValue != GetCookie(m_cookieName)) { // console.log("Mode::set(" + cookieValue + "): Cookies are disabled."); //} } }

/*************************************************************************************************** User-Defined Object: ModeSelector function ModeSelector { if (Debugging) console.log("Begin of ModeSelector"); /*Declared these data members with "var" instead of with "this." to make then NOT accessible from the outside.*/ var m_initialized = false; 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"); var m_selector = getHTMLElement; this.getHTMLElement = getHTMLElement; this.getMode = getMode; this.setMode = setMode; /*  * This function returns the ModeSelector, as HTML element. */ function getHTMLElement { if (Debugging) console.log("Begin of ModeSelector::getHTMLElement"); if (!m_initialized) { 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_selector = document.createElement("SELECT"); m_selector.style.position = "relative"; m_selector.style.cssFloat = "right"; //m_selector.style.left = "35px"; //m_selector.style.top = "5px"; m_selector.style.width = "120px"; m_selector.setAttributeNode(CreateAttribute("name", "mode")); m_selector.appendChild(m_optionDisabled); m_selector.appendChild(m_optionCollapsed); m_selector.appendChild(m_optionExpanded); m_selector.appendChild(m_optionAutomatic); m_selector.onchange=function{modeOnChange}; m_initialized = true; }   if (Debugging) console.log("End of ModeSelector::getHTMLElement"); return m_selector; } /*--*/  function getMode { return m_mode.get; } /*--*/  function modeOnChange { console.log ("Begin of ModeSelector::modeOnChange."); m_mode.set(m_selector.value); } /*--*/  function setMode(cookieValue) { if (Debugging) console.log("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); } }  if (Debugging) console.log("End of ModeSelector"); }

/*************************************************************************************************** User-Defined Object: StyleGuardControl function StyleGuardControl { if (Debugging) console.log("Begin of StyleGuardControl"); /*1. Validate the current document.-*/ if (Debugging) console.log("Validate the current document"); var m_rule1 = new Rule1; var m_rule2 = new Rule2; var m_success = true; var m_message1; if (rule1.apply) { m_message1 = document.createElement("DIV"); m_success = m_rule1.check(m_message1) && m_success; } var m_message2; if (rule2.apply) { m_message2 = document.createElement("DIV"); m_success = m_rule2.check(m_message2) && m_success; } /*2. Determine the appropriate kind of dog.-*/ if (Debugging) console.log("Determine the appropriate kind of dog"); var m_dog = document.createElement("IMG"); if (m_success) { 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.--*/ if (Debugging) console.log("Determine the height of this control"); var m_controlHeight; if (m_success) { m_controlHeight = "32px"; } else { controlHeight = "64px"; } /*4. Generate HMTL.-*/ if (Debugging) console.log("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 = 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(modeSelector.getHTMLElement); m_html.appendChild(dog); /*5. Define member function.*/ this.getHeight = getHeight; this.getHTML = getHTML; /*--*/ function getHeight { return m_controlHeight; } /*--*/  function getHTML { return m_html; } if (Debugging) console.log("End of StyleGuardControl"); }

/*************************************************************************************************** Main if (Debugging) console.log ("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);

if (Debugging) console.log ("End of file.");