User:MystinaRose/sandbox

<!-- EDIT BELOW THIS LINE --// This script adds buttons next to isbns on wikipedia pages that will redirect // the user to a readable digital copy of the referenced book.

// $( function {	if( mw.config.get( "wgNamespaceNumber" ) === 0 ) {		function addArchiveIcon(id, metadata){		  let toolTip = createTooltipWindow(metadata, id)		  let anchor = createArchiveAnchor(id, metadata)		  return attachTooltip(anchor, toolTip)		}		function attachTooltip (anchor, tooltip) {		  // Modified code from https://embed.plnkr.co/plunk/HLqrJ6 to get tooltip to stay			var popup = new OO.ui.PopupWidget( { $content: $( tooltip ), padded: false, align: 'center', position: 'before' } );			anchor.append( popup.$element );			popup.toggle(true); /*			 //Handles staying open			  $(anchor).on("mouseenter", function  { popup.toggle( true ); $(tooltip).on("mouseleave", function {			      setTimeout(function { if(!$('.btn-archive[href*="' + anchor.attr('href') + '"]:hover').length){ popup.toggle( false ); }			     }, 200);			    });			  })			  .on("mouseleave", function  { setTimeout(function {			      if (!$(".popup_box:hover").length) {			        popup.toggle( false );			      }			    },200); })			return anchor;		}		function createTooltipWindow (metadata, id) {		 let css = {		  	'cover_img': '  align-self: center;  border: 1px solid black;  border-radius: 3px;  display:block;  height:168px;  margin:10px auto;  width: 120px;',			'text_elements' : 'max-width: 100%; margin: auto; text-align: center; padding: 5px;',			'popup_box': 'text-align: center;',			'bottom_details': '  background-color: #eee;  width: 100%;  padding: 5px;  margin: auto;  border-radius: 3px; text-align: center;'					  }		  let text_elements = $(' ').attr({ 'class': 'text_elements', 'style':css.text_elements }).append( $(' ').append($(' ').text(metadata.title)).addClass('popup-title'), $(' ').addClass('text-muted').text(metadata.author) )		 let details = $(' ').attr({ 'class': 'bottom_details', 'style': css.bottom_details }).append( metadata.image ? $(' ').attr({ 'class': 'cover-img', 'src': metadata.image, 'style':css.cover_img }) : null, $(' ').text('Click To Read Now').addClass('text-muted') )		 return $('').append(text_elements, details).addClass('popup_box').attr({'href': 'https://archive.org/details/' + id, 'style': css.popup_box})[0].outerHTML		}		function createArchiveAnchor (id, metadata) {		  let img = $(' ')		    .attr({ 'alt': 'Read', 'src': 'https://upload.wikimedia.org/wikipedia/commons/7/73/Internet_Archive_logo.png', 'style': 'max-width: 16px; max-height: 16px' })[0]		 let a = $('')		    .attr({ 'href': 'https://archive.org/details/' + id, 'class': 'btn-archive', 'style': 'padding: 5px;' })		   .prepend(img)		  return a		}		function getIdentifier (book) {		  // identifier can be found as metadata.identifier or ocaid		  if (book) {		    var id = 		    if (book.metadata) {		      id = book.metadata.identifier		    } else {		      id = book.ocaid		    }		    if (id) {		      return id		    }		  }		  return null		}		function getISBNFromCitation (citation) {		  // Takes in HTMLElement and returns isbn number or null if isbn not found		  let rawISBN = citation.text		  let isbn = rawISBN.replace(/-/g, )		  return isbn		}		// Get all books on wikipedia page through		// https://archive.org/services/context/books?url=...		function getWikipediaBooks (url) {		  return $.ajax({ dataType: "json", crossDomain: true, url: 'https://archive.org/services/context/books?url=' + url, beforeSend: function(jqXHR, settings) { jqXHR.url = settings.url; },		   timeout: 10000 })		}		function getMetadata(book){		 if (book) {		    if(book.metadata){		      return {		        "title" : book.metadata.title,		        "author" : book.metadata.creator,		        "image" : "https://archive.org/services/img/" + book.metadata.identifier,		        "link" : book.metadata["identifier-access"],		        "button_text": "Read Now",		        "button_class": "btn btn-success resize_fit_center",		        "readable" : true		      }		    }else{		      return {		        "title" : book.title,		        "author" : book.authors_metadata ? book.authors_metadata.personal_name : "",		        "image" : book.covers ? "https://covers.openlibrary.org/w/id/"+ book.covers[0]+"-M.jpg" : undefined,		        "link" : "https://archive.org/donate/",		        "button_text": "Donate",		        "button_class": "btn btn-warning resize_fit_center",		        "readable" : false		      }		    }		  }		  return false; }		/**		 * Customizes error handling * @param status {string} * @return {string} */		function getErrorMessage(req){ return "The requested service " + req.url + " failed: " + req.status + ", " + req.statusText }		mw.loader.using( [ 'oojs-ui-core' ], function {			getWikipediaBooks(location.href).done(data => { let books = $("a[title^='Special:BookSources']") for (let book of books) { let isbn = getISBNFromCitation(book) let id = getIdentifier(data[isbn]) let metadata = getMetadata(data[isbn]) if (id) { let icon = addArchiveIcon(id, metadata) book.parentElement.append(icon[0]) }		   }		  }).fail( function( xhr, status ) { console.log(getErrorMessage(xhr)) });		})	} });