Created
December 12, 2013 16:05
-
-
Save jrunning/7930452 to your computer and use it in GitHub Desktop.
Some very rough code to automatically submit an item from Amazon to Iowa City Public Library's "Suggestions For Our Collection" page: http://www.icpl.org/catalog/suggestions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var formAction = "http://www.icpl.org/catalog/suggestions.php", | |
| formTmpl; | |
| formTmpl = | |
| '<form method="post" action="' + formAction + '" id="icplInjectForm" target="_blank">' + | |
| '<input name="author" id="icpl-author"/>' + | |
| '<input name="title" id="icpl-title"/>' + | |
| '<input name="publisher" id="icpl-publisher"/>' + | |
| '<input name="copyright" id="icpl-copyright"/>' + | |
| '<input name="cost" id="icpl-cost"/>' + | |
| '<input name="isbn" id="icpl-isbn"/>' + | |
| '<input name="where" id="icpl-where"/>' + | |
| '<input type="submit" value="SUBMIT THIS SUGGESTION"/>' + | |
| '</form>' | |
| ; | |
| /* | |
| author | |
| span.author | |
| a | |
| "Ylvis" | |
| span.contribution | |
| "(Author)" | |
| */ | |
| function getAuthor() { | |
| return $('.author a').text() | |
| } | |
| /* | |
| title | |
| #title | |
| Constellation Games | |
| <span ...>...</span> | |
| */ | |
| function getTitle() { | |
| var $titleEl = $("h1#title"), | |
| contents = $titleEl.contents(), | |
| title; | |
| contents.each(function() { | |
| if(this.nodeType == 3) { | |
| title = this.textContent.trim() | |
| if(title.length) { return false } // break | |
| } | |
| }) | |
| return title | |
| } | |
| /* | |
| publisher, copyright | |
| #productDetailsTable li | |
| <b>Publisher:</b> | |
| Candlemark & Gleam (April 17, 2012) | |
| */ | |
| function getPublisherAndDate() { | |
| var $publisherLiEl = $("#productDetailsTable li").has("b:contains('Publisher:')"), | |
| contents = $publisherLiEl.contents(), | |
| matchExp = /(.+?)(?:\s\(([^(]+)\))?$/, | |
| publisherAndDate, matches; | |
| contents.each(function() { | |
| if(this.nodeType == 3) { | |
| publisherAndDate = this.textContent.trim() | |
| if(publisherAndDate.length) { return false } // break | |
| } | |
| }) | |
| if(publisherAndDate.length === 0) { return } | |
| matches = matchExp.exec(publisherAndDate) | |
| return { publisher: matches[1], date: matches[2] } | |
| } | |
| /* | |
| cost | |
| #buybox span.offer-price | |
| "$10.92" | |
| */ | |
| function getPrice() { | |
| return $("#buybox span.offer-price").text().trim() | |
| } | |
| /* | |
| isbn | |
| #productDetailsTable | |
| li | |
| <b>ISBN-13:</b> | |
| "978-1936460236" | |
| li | |
| <b>ISBN-10:</b> | |
| "9999999999" | |
| */ | |
| function getISBN() { | |
| var $isbnLiEl = $("#productDetailsTable li").has("b:contains('ISBN-10'), b:contains('ISBN-13')"), | |
| contents = $isbnLiEl.contents(), | |
| isbn; | |
| contents.each(function() { | |
| if(this.nodeType == 3) { | |
| isbn = this.textContent.trim() | |
| if(title.length) { return false } // break | |
| } | |
| }) | |
| return isbn | |
| } | |
| function createForm() { | |
| var $form = $("#icplInjectForm"); | |
| if($form.length === 0) { | |
| $form = $(formTmpl).appendTo('body'); | |
| } | |
| return $form | |
| } | |
| function populateForm($form, values) { | |
| $form = $($form) | |
| Object.keys(values).forEach(function(key) { | |
| $form.find("#icpl-" + key).val( values[key] ) | |
| }) | |
| } | |
| function getValues() { | |
| var publisherAndDate = getPublisherAndDate(); | |
| return { | |
| author: getAuthor(), | |
| title: getTitle(), | |
| publisher: publisherAndDate.publisher, | |
| copyright: publisherAndDate.date, | |
| cost: getPrice(), | |
| isbn: getISBN(), | |
| where: window.location.href, | |
| format: "no", | |
| placehold: "yes" | |
| } | |
| } | |
| function goIcpl() { | |
| var $form = createForm(), | |
| values = getValues(); | |
| populateForm($form, values); | |
| $form.submit() | |
| } | |
| /* | |
| name | |
| addressStreet | |
| addressCity | |
| addressState: IA | |
| addressZip | |
| phone | |
| barcode // library card number | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment