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 /*-- Rule: Every WikiaPage shall have a category. --*/ function ApplyRule1 { 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; } function CheckRule1(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; } /*-- Rule: Every WikiaPage shall have no links to redirection pages. --*/ function ApplyRule2 { var URL = document.URL; console.log(URL); return (Search("\?action=edit", URL) == false); } function CheckRule2(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; }

/*************************************************************************************************** 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 = get; this.get = get; this.getMode = getMode; this.setMode = setMode; /*  * This function returns the ModeSelector, as HTML element. */ function get { if (Debugging) console.log("Begin of ModeSelector::get"); 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 = "absolute"; 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::get"); 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); } } }

/*************************************************************************************************** User-Defined Object: StyleGuardControl function StyleGuardControl { if (Debugging) console.log("Begin of StyleGuardControl"); /*Declared these data members with "var" instead of with "this." to make then NOT accessible from the outside.*/ var m_initialized = false; var m_control; this.getHTMLElement = getHTMLElement; /*  * This function returns the StyleGuardControl, as HTML element. */ function getHTMLElement(left, top) { if (Debugging) console.log("Begin of StyleGuardControl::getHTMLElement"); if (!m_initialized) { var Icon = document.createElement("IMG"); Icon.setAttributeNode(CreateAttribute( "src", "http://static3.wikia.nocookie.net/elderscrolls/images/7/73/Transparent_bulldog.png")); Icon.style.position = "relative"; Icon.style.cssFloat = "left"; Icon.style.height = "32px"; Icon.style.width = "32px";

var modeSelector = new ModeSelector; m_control = document.createElement("FORM"); m_control.style.display = "table"; m_control.style.position = "absolute"; m_control.style.left = left; m_control.style.top = top; //     m_control.style.cssFloat = "left"; m_control.style.width = "200px"; m_control.style.height = "32px"; m_control.style.borderWidth = "1px"; m_control.style.borderStyle = "solid"; m_control.style.borderColor = "#303030"; m_control.appendChild(Icon); m_control.appendChild(modeSelector.get);

m_initialized = true; }   if (Debugging) console.log("End of StyleGuardControl::getHTMLElement"); return m_control; } }

/*================================================================================================== Main

=
=====================================================================================*/ if (Debugging) console.log ("Begin of file.");

var styleGuardControl = new StyleGuardControl; var WikiHeader = document.getElementById("WikiHeader"); var WikiNav = WikiHeader.getElementsByClassName("WikiNav"); var navbackground = WikiNav.getElementsByClassName("navbackground"); console.log(navbackground.style.right + ", " + navbackground.style.bottom);

//navbackground.appendChild(styleGuardControl.getHTMLElement( // navbackground.style.right ));

var Success = true; var Message1; if (ApplyRule1) { Message1 = document.createElement("DIV"); Success = CheckRule1(Message1) && Success; } var Message2; if (ApplyRule2) { Message2 = document.createElement("DIV"); Success = CheckRule2(Message2) && Success; } /*--Create the appropriate kind of dog.---*/ var Dog; if (Success) { Dog = document.createElement("IMG"); Dog.setAttributeNode(CreateAttribute( "src", "http://static3.wikia.nocookie.net/elderscrolls/images/7/73/Transparent_bulldog.png")); Dog.setAttributeNode(CreateAttribute("height", "32px")); Dog.setAttributeNode(CreateAttribute("width", "32px")); Dog.style.position = "relative"; Dog.style.cssFloat = "right"; } else { Dog = document.createElement("IMG"); Dog.setAttributeNode(CreateAttribute( "src", "http://static2.wikia.nocookie.net/elderscrolls/images/0/0c/Transparent_mean_bulldog.png")); } /*--Generate HTML-*/ 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 = "86%"; StyleGuard.style.top= "57px"; StyleGuard.style.width = "14%"; 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); } //StyleGuard.appendChild(Control);

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 WikiaPage = document.getElementById("WikiaPage"); //WikiaPage.style.marginLeft = "0px"; //WikiaPage.style.width = "85%"; //WikiaPage.style.maxWidth = "1600px"; //WikiaPage.parentNode.insertBefore(StyleGuard, WikiaPage); //WikiaPage.parentNode.insertBefore(StyleGuardSpaceReservation, StyleGuard); //if (Debugging) console.log ("Begin of file.");