plugin
widget
ActionGetBookInfoWidget.prototype.invokeAction = function(triggeringWidget,event) {
function dateNow() {
var set = new Date();
var getDate = set.getDate().toString();
if (getDate.length == 1){ //example if 1 change to 01
getDate = "0"+getDate;
}
var getMonth = (set.getMonth()+1).toString();
if (getMonth.length == 1){
getMonth = "0"+getMonth;
}
var getYear = set.getFullYear().toString();
var dateNow = getYear +"-"+ getMonth +"-"+ getDate; //today
return dateNow;
}
function createBook(title, authors, publisher, isbn, cover) {
var author_line = "* \'\'Authors:\'\' ".concat(authors);
var publisher_line = "* \'\'Publisher:\'\' ".concat(publisher);
var isbn_line = "* \'\'ISBN:\'\' ".concat(isbn);
var date_line = "* \'\'Date Added:\'\' ".concat(dateNow());
var cover_line = "[img[".concat(cover,"]]\n");
var notes_line = "* \'\'Notes:\'\'";
var text = [author_line, publisher_line, isbn_line, date_line, cover_line, notes_line].join("\n");
var fields = { tags: "Books", text: text }
console.log(fields);
$tw.wiki.addTiddler(new $tw.Tiddler($tw.wiki.getCreationFields(), fields, {title: title}, $tw.wiki.getModificationFields()));
}
// Create URL
var base = "https://openlibrary.org/api/books?bibkeys=ISBN:";
var isbn = this.isbn;
var url = base.concat(isbn,"&format=json&jscmd=data");
console.log(url);
// Do HTTP request
window.fetch(url)
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
return;
}
response.json().then(function(data) {
console.log(JSON.stringify(data));
var key = "ISBN:".concat(isbn);
var title = data[key]["title"];
var publisher = data[key]["publishers"][0]["name"];
var authors = data[key]["authors"].map(a => a["name"]).join("|");
var cover = data[key]["cover"]["medium"];
createBook(title, authors, publisher, isbn, cover);
alert("Created page for: " + title);
});
}
)
.catch(function(err) {
});
return true; // Action was invoked
};
New book ISBN: <$edit-text tiddler="$:/state/isbn" tag="input" default=""/>
<$button>
<$action-getbookinfo $isbn={{$:/state/isbn}}/>
Add New Book
</$button>
There were a few improvements that i introduced as part of that packaging, namely:
<object width="100%" height="930" data="file:///C:\Data\batches\networkcheck.txt"></object>