User:Vbanos/IABooks.js

// 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)		  return createArchiveAnchor(id, metadata, toolTip)		}

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; padding: 5px;', 'popup_box': 'text-align: center;', 'bottom_details': ' background-color: #eee;  max-width: 100%;  padding: 5px;  margin: auto;' }		 let text_elements = $(' ').attr({'style':css.text_elements }).append(		    $(' ').append($(' ').text(metadata.title)),		    $(' ').text(metadata.author)		  ) let details = $(' ').attr({'style': css.bottom_details }).append(		   metadata.image ? $(' ').attr({'src': metadata.image, 'style':css.cover_img }) : null,		    $(' ').text('Click To Read Now')		  ) return $('').append(text_elements, details).attr({'href': 'https://archive.org/details/' + id, 'style': css.popup_box})[0].outerHTML }		function createArchiveAnchor (id, metadata, tt) { // var readIcon = new OO.ui.IconWidget({			// 	'icon':'alert',			// 	'iconTitle':'Alert'			// }); // var iconLabel = new OO.ui.LabelWidget( {			// 	label: 'Read Now'			// } ); var popupButton = new OO.ui.PopupButtonWidget( { 				label: '📖 Read Now',				href: 'https://archive.org/details/' + id,				target: '_blank',				framed: false,				flags: 'progressive',				classes: ['btn-archive-book'],				// icon: 'bookmark',				popup: {					$content: $(tt),					padded: false,					align: 'forwards',					width:180				}			} ); return popupButton }

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.$element[0]) }		   }		    $('.btn-archive-book').css({		    	'background-color': 'lightgrey',		    	'border-radius': '3px',		    	'box-shadow': '2px 2px 1px darkgrey',		    	'margin': '0 4px 1px',		    	'padding': '0 2px 0'		    }) }).fail( function( xhr, status ) { console.log(getErrorMessage(xhr)) });		})	} });