User:Mdd4696/Image Autotagger/script.js

//

/*----------------------------------------------------------------------------+

| Global vars |

+----------------------------------------------------------------------------*/

var imageViewer;

var messageTimeoutId;

var fastTaggerBodyFlag = false;

var fastTaggerTabFlag = false;

var listLimit = 1000;

var summaryDefaultText = "Thousands of images have no copyright tags. You can help!";

/*----------------------------------------------------------------------------+

| Tag arrays |

+----------------------------------------------------------------------------*/

var commonTags = new Array( "subst:nld", "subst:nsd", "or-fu-nr", "or-fu-re", "GFDL-presumed", "imagevio", "Missing image" );

var publicDomainTags = new Array( "Military-Insignia", "PD", "PD-flag-50", "PD-flag-70",

"PD-flag-100", "PD-flag-US", "PD-ineligible", "PD-old", "PD-old-50",

"PD-old-70", "PD-stamp", "CopyrightedFreeUse", "PD-release", "PD-link",

"PD-self", "PD-user", "PD-art", "PD-art-life-50", "PD-art-life-70",

"PD-art-US", "Iraqcopyright", "PD-AR-Photo", "PD-Australia", "PD-Australia-CC",

"PD-AustraliaGov", "PD-BritishGov", "PD-Canada", "PD-China", "PD-CzechGov",

"PD-India", "PD-Iran", "PD-LithuaniaGov", "PD-Pakistan", "PD-PakistanGov",

"PD-Philippines", "PD-PhilippinesGov", "PD-Poland", "PD-PolishGov",

"PD-RU-exempt", "PD-SAGov", "PD-UA-exempt", "PD-USSR", "PD-BY-exempt",

"Gray's Anatomy plate", "PD-8BS", "PD-GWPDA", "PD-Meyers",

"PD-Open Clipart Library", "PD-PCL", "PD-PDphoto.org", "PD-Vardion",

"PD-Catholic", "money-US"

);

var freeTags = new Array( "ABr", "AMI", "Attribution",

"cc-by-2.0", "CACTVSGIF", "CopyrightedFreeUse", "CopyrightedFreeUse-Link",

"CopyrightedFreeUse-User", "CopyrightedFreeUseProvidedThat", "EU image",

"FinnishDefenceForces", "Flphoto", "money-EU", "MosNews", "No rights reserved",

"Sejm", "SocEur", "Trainweb", "WorldCoin", "BSD", "BSDu", "cc-by", "cc-by-2.0",

"cc-by-2.0-map", "cc-by-2.0-map-of", "cc-by-2.5", "cc-sa", "cc-by-sa",

"cc-by-sa-2.0", "cc-by-sa-2.0-kr", "cc-by-sa-2.5", "FAL", "GermanGov", "GFDL",

"GFDL-small", "GFDL 1.2", "GFDL-self", "GFDL-retouched", "GFDL-invariants",

"GPL", "LGPL", "GFDL-OpenGeoDB", "MIT", "NZCrownCopyright", "PD-NZSection27",

"CrownCopyright", "LearningandSkillsCouncilCopyright",

"NationalAuditOfficeCopyright", "NHSCopyright"

);

var fairUseTags = new Array( "art",

"Digimonimage", "Fairusein", "Music sample", "Pokeimage", "smithsonian",

"Standard test", "USPSstamp", "YuGiOhimage", "Logo", "DisneyLogo", "icon",

"radiologo", "scoutlogo", "sports-logo", "hqfl_logos", "Olympics-logo",

"tv-logo", "tv-program-logo", "Albumcover", "Boardgamecover", "Bookcover",

"Comiccover", "Comicpanel", "Comicscene", "DVDcover", "Gamecover",

"Magazinecover", "Newspapercover", "TIME", "Video tape cover",

"DisneyAttractionPoster", "Eventposter", "Movieposter",

"ParliamentofAustralia", "Politicalposter", "Promophoto", "Promotional",

"Sportsposter", "Screenshot", "film-screenshot", "game-screenshot",

"musicpromo-screenshot", "software-screenshot", "linux-software-screenshot",

"mac-software-screenshot", "windows-software-screenshot",

"web-software-screenshot", "tv-screenshot",

"web-screenshot", "wikipedia-screenshot"

);

/*----------------------------------------------------------------------------+

| Message arrays |

+----------------------------------------------------------------------------*/

var generalMessages = new Array (

"Wunderbar!", "That's it!", "Keep going!", "Alright now!", "Superb!",

"Amazing!", "Keep it coming.", "Wow. Just wow.", "Speed demon!",

"You're on fire!", ":-)", "Sweet!", "Who's da man?", "Watch it now!",

"Woo woo!", "The image tagging train is chugging along!", "Going to town!",

"Zoooom!", "You like to move it, move it.", "Bot-like.",

"You are the image tagging master!", "Sha-wing!", "Tired yet?",

"Wrist hurt yet?", "Jolly good.", "Brilliant!", "Sacre Bleu!",

"Oh. My. God. Becky, look at her tagger. It is so big.", "Tagger skillz.",

"ur l33t", "Blimey!", "Gotta catch them all!", "Gooooaaaal!",

"Such a good little Wikipedian.", "Faster than a speeding bullet.",

"Einstein would approve.", "Ben Franklin would approve.",

"Jefferson would approve.", "Washington would approve.", "Shiny!",

"Double whammy!", "Home run!", "That minty fresh feeling!", "Another?",

"Bingo!", "Another round!", "Please, sir, I want some more.", "So fast!",

"If you were a car, I'd call you Ferrari.", "Tag. It. Tag it.",

"I am a banana!", "Tickle-me tagger!",

"The wonderful thing about taggers is taggers are wonderful things!",

"Booyah!", "No need to thank me, just doing my job.", "Bagged and tagged."

);

/*----------------------------------------------------------------------------+

| Creation functions |

+----------------------------------------------------------------------------*/

function createTaggerBody() {

window.name = "taggerWin";

var taggerBox = document.createElement( "div" );

taggerBox.id = "taggerBox";

taggerBox.style.background = "#F9F9F9";

taggerBox.style.border = "1px solid #000000";

taggerBox.style.left = "15%";

taggerBox.style.padding = "15px";

taggerBox.style.position = "absolute";

taggerBox.style.top = "15%";

taggerBox.style.width = "635px";

taggerBox.style.zIndex = "100";

var retVal = "";

retVal += "×";

retVal += "

";

retVal += " ";

retVal += "
";

retVal += " ";

retVal += "
";

retVal += " ";

retVal += "
";

retVal += " ";

retVal += " ";

retVal += "

";

retVal += "";

retVal += "
";

retVal += "";

retVal += "
";

retVal += "";

retVal += "
";

retVal += "";

retVal += "
";

retVal += " Default summary";

retVal += "
";

retVal += "";

retVal += "";

retVal += "
";

retVal += "";

taggerBox.innerHTML = retVal;

document.body.appendChild( taggerBox );

populateSelect( document.getElementById( "commonSel" ), commonTags );

populateSelect( document.getElementById( "publicDomainSel" ), publicDomainTags );

populateSelect( document.getElementById( "freeSel" ), freeTags );

populateSelect( document.getElementById( "fairUseSel" ), fairUseTags );

}

function createFastTaggerBody( mouseXPos, mouseYPos ) {

var fastTaggerBox = document.createElement( "div" );

fastTaggerBox.id = "fastTaggerBox";

fastTaggerBox.setAttribute( "onMouseOut", "setFastTaggerBodyFlag( false );" );

fastTaggerBox.setAttribute( "onMouseOver", "setFastTaggerBodyFlag( true );" );

fastTaggerBox.style.background = "#F9F9F9";

fastTaggerBox.style.border = "1px solid #000000";

fastTaggerBox.style.left = mouseXPos + "px";

fastTaggerBox.style.padding = "15px";

fastTaggerBox.style.position = "absolute";

fastTaggerBox.style.top = mouseYPos + "px";

fastTaggerBox.style.zIndex = "101";

var retVal = "";

retVal += "";

retVal += "
";

retVal += "";

retVal += "
";

retVal += "";

retVal += "

";

retVal += " Default summary";

retVal += "
";

retVal += "";

fastTaggerBox.innerHTML = retVal;

document.body.appendChild( fastTaggerBox );

}

function createImageViewer( atUrl ) {

if ( atUrl == "" ) {

atUrl = "http://en.wikipedia.org/wiki/Image:TestImage.jpg";

}

if ( ! imageViewer || imageViewer.closed ) {

imageViewer = window.open( atUrl, "imageViewer", "width = 800, height = 600, resizable = yes, scrollbars = yes, toolbar = no, location = yes, directories = no, status = no, menubar = no, copyhistory = no" );

imageViewer.name = "editorWin";

} else {

imageViewer.location.href = atUrl;

imageViewer.focus();

}

}

/*----------------------------------------------------------------------------+

| Show/Hide functions |

+----------------------------------------------------------------------------*/

function hideMessage() {

document.getElementById( "messageText" ).innerHTML = "";

}

function showMessage() {

var randIndex = Math.round( Math.random() * ( generalMessages.length - 1 ) );

document.getElementById( "messageText" ).innerHTML = generalMessages[ randIndex ];

window.clearTimeout( messageTimeoutId );

messageTimeoutId = window.setTimeout( "hideMessage()", 5000 );

}

function hideTagger() {

window.clearTimeout( messageTimeoutId );

hideMessage();

document.getElementById( "taggerBox" ).style.display = "none";

}

function showTagger() {

if ( document.getElementById( "taggerBox" ) ) {

document.getElementById( "taggerBox" ).style.display = "block";

} else {

createTaggerBody();

}

}

function setFastTaggerBodyFlag( newFlag, mouseXPos, mouseYPos ) {

fastTaggerBodyFlag = newFlag;

toggleFastTagger( mouseXPos, mouseYPos );

}

function setFastTaggerTabFlag( newFlag, mouseXPos, mouseYPos ) {

fastTaggerTabFlag = newFlag;

toggleFastTagger( mouseXPos, mouseYPos );

}

function toggleFastTagger( mouseXPos, mouseYPos ) {

if ( fastTaggerTabFlag || fastTaggerBodyFlag ) {

if ( document.getElementById( "fastTaggerBox" ) ) {

document.getElementById( "fastTaggerBox" ).style.display = "block";

} else {

createFastTaggerBody( mouseXPos, mouseYPos );

}

if ( mouseXPos < parseInt( document.getElementById( "fastTaggerBox" ).style.left ) ) {

document.getElementById( "fastTaggerBox" ).style.left = mouseXPos + "px";

}

if ( mouseYPos > parseInt( document.getElementById( "fastTaggerBox" ).style.top ) ) {

document.getElementById( "fastTaggerBox" ).style.top = mouseYPos + "px";

}

} else {

document.getElementById( "fastTaggerBox" ).style.display = "none";

}

}

function loadImage( imageName ) {

if ( ! imageViewer || imageViewer.closed ) {

createImageViewer( "http://en.wikipedia.org/wiki/" + imageName );

} else {

imageViewer.location.href = "http://en.wikipedia.org/wiki/" + imageName;

}

var xmlhttp = new XMLHttpRequest();

xmlhttp.open( "GET", "http://en.wikipedia.org/w/index.php?title=" + imageName + "&action=raw&ctype=text/xml", true );

xmlhttp.onreadystatechange = function() {

if ( xmlhttp.readyState == 4 ) {

if ( xmlhttp.status == 200 ) {

document.getElementById( "pageText" ).value = xmlhttp.responseText;

} else if ( xmlhttp.status == 404 ) {

alert( "Error 404: Document not found" );

} else {

alert( "Unspecified error: " + xmlhttp.status );

}

}

}

xmlhttp.send( null );

document.getElementById( "pageText" ).value = "Loading...";

}

function loadList( pageName ) {

var xmlhttp = new XMLHttpRequest();

xmlhttp.open( "GET", "http://en.wikipedia.org/w/index.php?title=" + pageName + "&action=raw&ctype=text/xml", true );

xmlhttp.onreadystatechange = function() {

if ( xmlhttp.readyState == 4 ) {

if ( xmlhttp.status == 200 ) {

var rawListText = xmlhttp.responseText;

// Remove Wiki formatting

rawListText = rawListText.replace( /# \[\[:/g, "" );

rawListText = rawListText.replace( /\]\]/g, "" );

// Escape any remaining special characters

//var specials = [ "/", ".", "*", "+", "?", "|", "(", ")", "[", "]", "{", "}", "\\" ];

//rawListText = rawListText.replace( new RegExp( "(\\" + specials.join( "|\\" ) + ")", "g" ), "\\$1" );

var listArr = rawListText.split( "\n" );

for ( var i = 0; i < listArr.length && i < listLimit; i++ ) {

document.getElementById( "imageList" ).options[ i ] = new Option( listArr[ i ], listArr[ i ] );

}

} else if ( xmlhttp.status == 404 ) {

alert( "Error 404: Document not found" );

} else {

alert( "Unspecified error: " + xmlhttp.status );

}

}

}

xmlhttp.send( null );

document.getElementById( "imageList" ).options.length = 0;

document.getElementById( "imageList" ).options[ 0 ] = new Option( "Loading...", "" );

}

function defaultSummary( summaryField, defaultFlag ) {

if ( defaultFlag ) {

summaryField.value = summaryDefaultText;

}

}

function populateSelect( selectObj, optionArr ) {

for ( var i = 1; i <= optionArr.length; i++ ) {

var newOption = document.createElement( "option" );

selectObj.options[ i ] = new Option( optionArr[ i - 1 ], optionArr[ i - 1 ] );

}

}

function toggleSummary() {

if ( document.getElementById( "summaryCheckbox" ).checked ) {

document.getElementById( "customSummary" ).style.display = "none";

document.getElementById( "customSummary" ).value = "Thousands of images have no copyright tags. You can help!";

} else {

document.getElementById( "customSummary" ).style.display = "inline";

}

}

/*

* Name: insertTag()

* Description: Inserts tag template into the edit box

* Parameters: tag - Tag text

* location - Where to place tag (top, summary,

* licensing, bottom)

* Returns: Boolean value indicating success

*/

function insertTag( textFieldObj, tag, location ) {

var retVal = true;

var sumTagNewline = "\n";

var sumRegExp = /=+\s*Summary\s*=+\n?/i;

var licRegExp = /=+\s*Licens[a-zA-Z]+\s*=+\n?/i;

var wpTextboxVal = textFieldObj.value;

tag = "{{" + tag + "}}";

// Summary heading

if ( ! wpTextboxVal.match( sumRegExp ) ) {

var newlines = "\n";

if ( wpTextboxVal.search( licRegExp ) == 0 ) {

newlines += "\n\n";

sumTagNewline = "";

}

wpTextboxVal = "== Summary ==" + newlines + wpTextboxVal;

}

// Licensing heading

if ( ! wpTextboxVal.match( licRegExp ) ) {

wpTextboxVal = wpTextboxVal + "\n== Licensing ==\n";

}

// Add the tag

switch ( location ) {

case "top":

wpTextboxVal = tag + "\n\n" + wpTextboxVal;

break;

case "summary":

wpTextboxVal = wpTextboxVal.replace( sumRegExp, "== Summary ==\n" + tag + sumTagNewline );

break;

case "licensing":

wpTextboxVal = wpTextboxVal.replace( licRegExp, "== Licensing ==\n" + tag + "\n" );

break;

case "bottom":

wpTextboxVal = wpTextboxVal + "\n" + tag;

break;

default:

retVal = false;

}

// Update the textarea

textFieldObj.value = wpTextboxVal;

return retVal;

}

function imageUpdated() {

document.getElementById( "imageList" ).options[ document.getElementById( "imageList" ).selectedIndex ] = null;

document.getElementById( "pageText" ).value = "";

}

function tagImage() {

showMessage();

var imageName = document.getElementById( "imageList" ).value;

if ( ! imageViewer || imageViewer.closed ) {

createImageViewer( "http://en.wikipedia.org/w/index.php?title=" + imageName + "&action=edit" );

} else {

imageViewer.location.href = "http://en.wikipedia.org/w/index.php?title=" + imageName + "&action=edit";

}

//createImageViewer( "http://en.wikipedia.org/w/index.php?title=Image:TestImage.jpg&action=edit&tagImage=true" );

//imageViewer.location.href = "";

}

function fastTagImage( tag, location, summaryText ) {

if ( document.title.indexOf( "Editing Image:" ) == 0 ) {

insertTag( document.editform.wpTextbox1, tag, location );

document.editform.wpSummary.value = summaryText;

document.editform.wpMinoredit.checked = false;

document.editform.wpWatchthis.checked = false;

document.editform.submit();

} else {

var imageName = document.title.substring( 0, document.title.indexOf( " - Wikipedia, the free encyclopedia" ) );

document.location.href = "http://en.wikipedia.org/w/index.php?title="

+ escape( imageName )

+ "&action=edit&tagImage=true&tagText=" + escape( tag )

+ "&tagLocation=" + escape( location )

+ "&summaryText=" + escape( summaryText );

}

}

addOnloadHook( function() {

addToolboxLink( "javascript:showTagger()", "Image Autotagger", "ca-showtagger" );

if ( window.name == "editorWin" && window.opener != null ) {

document.getElementById( "ca-edit" ).parentNode.removeChild( document.getElementById( "ca-edit" ) );

}

if ( document.title.indexOf( "Editing Image:" ) == 0 && window.name == "editorWin" && window.opener != null ) {

document.editform.wpTextbox1.value = window.opener.document.getElementById( "pageText" ).value;

document.editform.wpSummary.value = window.opener.document.getElementById( "taggerEditSummary" ).value;

document.editform.wpMinoredit.checked = false;

document.editform.wpWatchthis.checked = false;

document.editform.submit();

window.opener.imageUpdated();

} else if ( document.title.indexOf( "Image:" ) == 0 || document.title.indexOf( "Editing Image:" ) == 0 ) {

addTab( "", "tag", "ca-tagnow", "Tag this image", "" );

document.getElementById( "ca-tagnow" ).setAttribute( "onMouseOver", "setFastTaggerTabFlag( true, event.clientX, event.clientY );" )

document.getElementById( "ca-tagnow" ).setAttribute( "onMouseOut", "setFastTaggerTabFlag( false, event.clientX, event.clientY );" )

if ( document.location.search.indexOf( "tagImage" ) > -1 ) {

var keyValArr = new Array();

var qPairs = document.location.search.substring( 1 ).split( "&" );

for ( var i = 0; i < qPairs.length; i++ ) {

var qKey = qPairs[ i ].substring( 0, qPairs[ i ].indexOf( "=" ) );

var qValue = qPairs[ i ].substring( qPairs[ i ].indexOf( "=" ) + 1, qPairs[ i ].length );

keyValArr[ qKey ] = unescape( qValue );

}

fastTagImage( keyValArr[ "tagText" ], keyValArr[ "tagLocation" ], keyValArr[ "summaryText" ] );

}

}

} );

//