Skip to content

Instantly share code, notes, and snippets.

@d3m3vilurr
Created January 17, 2009 08:13
Show Gist options
  • Save d3m3vilurr/48288 to your computer and use it in GitHub Desktop.
Save d3m3vilurr/48288 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: "yes24-search",
icon: "http://image.yes24.com/sysimage/top/favicon16.ico",
homepage: "http://d3m3vilurr.tistory.com/",
author: { name: "d3m3vilurr", email: "[email protected]"},
license: "GPL/MIT",
description: "Searches <a href=\"http://www.yes24.com\">Yes24</a> for books matching your words.",
takes: {"search term": noun_arb_text},
help: "Searches for Book on Yes24",
preview: function( pblock, input ) {
if (!input.text || input.text.length < 1) {
pblock.innerHTML = "Searches for Book on Yes24.";
return;
}
pblock.innerHTML = "Searching for books on Yes24 ...";
var parseTotalMatches = function(html) {
var frontTrimmed
= html.substr(html.search('<b>전체</b>'));
var rearTrimmed
= frontTrimmed.substr(0,
frontTrimmed.search('</td>')
);
var frontParsed = rearTrimmed.substr(rearTrimmed.search('[0-9]')).replace(/,/, '');
return parseInt(frontParsed.match(/\d+/));
}
var parseContentList = function(html) {
var frontTrimmed
= html.substr(html.search('<table id="tblProductList"'));
var rearTrimmed
= frontTrimmed.substr(0,
frontTrimmed.search('<table width="100%"')
);
var $ = jQuery;
var items = $($(rearTrimmed)[0]).children() // tbody
.children() // tr
.filter(function(i) {
return !$(this).children()
.attr('colspan');
});
var result = new Array();
items.each(function(i) {
var thumbUrl = $(items[i]).find('img:first').attr('src');
var links = $(items[i]).find('a');
var bookUrl = $(links[0]).attr('href');
var bookTitle = $(links.not("a:has(img)")[0]).text();
var authorUrl = $(links.filter(".m")[0]).attr('href');
var author = $(links.filter(".m")[0]).text();
var publisherUrl = $(links.filter(".m")[1]).attr('href');
var publisher = $(links.filter(".m")[1]).text();
if (links.filter(".m").length < 2) {
var publisherUrl = authorUrl;
var publisher = author;
var authorUrl = '';
var author = '';
}
result[i] = {
thumb : thumbUrl,
bookUrl : bookUrl,
bookTitle : bookTitle,
authorUrl : authorUrl,
author : author,
publisherUrl : publisherUrl,
publisher : publisher
};
});
return result;
}
var query = input.text;
var searchItem = function(page) {
var searchUrl = "http://www.yes24.com/searchCenter/searchResult.aspx?query="+escape(query)+"&Page="+page;
CmdUtils.previewGet(pblock, searchUrl, null, function(responseData) {
var total = parseTotalMatches(responseData);
var itemList = parseContentList(responseData);
var previewData = {
rows: 4,
nummatches: total,
books: itemList
};
var template = "<b>${nummatches} books were found on Yes24.</b><br /><br />"
+ "<div id='yes24-search'>"
+ "{for book in books}"
+ " {if book_index % rows == 0}"
+ " <table style=\"border-width: 0px;\">"
+ " {/if}"
+ " <tr height=\"100\">"
+ " <td><img src=\"${book.thumb}\" alt=\"${book.bookTitle|escape}\" /></td>"
+ " <td>"
+ " <div>Title : <a href=\"${book.bookUrl}\" title=\"${book.book|escape}\">"
+ " ${book.bookTitle}"
+ " </a></div>"
+ " <div>Author : <a href=\"http://www.yes24.com${book.authorUrl}\" title=\"${book.author|escape}\">"
+ " ${book.author}"
+ " </a></div>"
+ " <div>Publisher : <a href=\"http://www.yes24.com/searchCenter/${book.publisherUrl}\" title=\"${book.publisher|escape}\">"
+ " ${book.publisher|escape}"
+ " </a></div>"
+ " </td>"
+ " </tr>"
+ " {if book_index % rows == (rows - 1) || book_index == (books.length - 1) }"
+ " <tr class='buttons'>"
+ " <td colspan='2'><div class='left'>PREV</div><div class='right'>NEXT</div></td>"
+ " </tr>"
+ " </table>"
+ " {/if}"
+ "{/for}"
+ "</div>"
pblock.innerHTML = CmdUtils.renderTemplate(
template,
previewData);
var $ = jQuery;
$(pblock).find("#yes24-search img").css('width', '60px');
$(pblock).find("#yes24-search table").hide()
.find('.buttons div').css('cursor', 'pointer')
.css('border', '1px solid white')
.css('width', '50px')
.css('text-align', 'center')
.css('float', 'left')
.css('position', 'absolute')
.click(function() {
$(pblock).find("#yes24-search table")
.hide();
});
$(pblock).find('.buttons div.left').css('left', '100px');
$(pblock).find('.buttons div.right').css('left', '200px');
if (page == 1) {
$(pblock).find("#yes24-search .buttons .left:first").hide();
}
if ($(pblock).find("#yes24-search table:last tr").length - 1 < previewData.rows
|| page * 20 >= total) {
$(pblock).find("#yes24-search .buttons .right:last").hide();
}
$(pblock).find("#yes24-search .buttons").each(function(i) {
$(this).find('.left').click(function() {
$($(pblock).find("#yes24-search table")[i - 1]).show();
});
$(this).find('.right').click(function() {
$($(pblock).find("#yes24-search table")[i + 1]).show();
});
});
$(pblock).find("#yes24-search .buttons .left:first")
.unbind('click')
.click(function() {
pblock.innerHTML = "Load prev pages...";
searchItem(page-1);
});
$(pblock).find("#yes24-search .buttons .right:last")
.unbind('click')
.click(function() {
pblock.innerHTML = "Load next pages...";
searchItem(page+1);
});
$(pblock).find("#yes24-search table:first").show();
});
}
searchItem(1);
},
execute: function( input ) {
var query = escape(input.text);
Utils.openUrlInBrowser("http://www.yes24.com/searchCenter/searchResult.aspx?query="+query);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment