Skip to content

Instantly share code, notes, and snippets.

/gist:354281

Created Apr 3, 2010
Embed
What would you like to do?
jetpack.future.import("slideBar");
var html = <>
<html>
<head>
<style type="text/css">
<![CDATA[
body{
font-size:12px;
}
]]>
</style>
</head>
<body>
<form id="searchForm">
<input type="text" name="word" id="word" />
<input type="submit" value="搜尋" id="search" />
</form>
<div id="ans"></div>
</body></html></>.toXMLString();
var _doc;
function ready(slider) {
_doc = slider.contentDocument;
$("#searchForm", _doc).submit(function(event) {
search($("#word", _doc).val());
event.preventDefault();
event.stopPropagation();
});
}
function search(word){
var h5 = _doc.createElement("h5");
$("body", _doc).append($(h5).text(word));
$.ajax({
url: "http://tw.dictionary.yahoo.com/search",
dataType: "html",
data: {'ei': 'UTF-8', 'p': word},
success: searched,
error: function(xhr, stat, err) { console.log("XHR Error:" + stat); }
});
}
function searched(data, stat, xhr){
var matches = data.match(/<div class\=pexplain>[^<]+<\/div>/g);
if (!matches) {
$('body', _doc).append("No Found!");
return;
}
var list = _doc.createElement("ul");
for (var i = 0; i < matches.length; i++) {
var explain = matches[i].match(/<div class\=pexplain>([^<]+)<\/div>/)[1];
var item = _doc.createElement("li");
$(item).text(explain);
$(list, _doc).append(item);
}
$("body", _doc).append(list);
}
jetpack.slideBar.append( {
icon: "https://jetpack.mozillalabs.com/images/jetpack.png",
html: html,
onReady: ready
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment