Created
April 3, 2010 08:53
-
-
Save anonymous/354281 to your computer and use it in GitHub Desktop.
This file contains 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
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