aboutsummaryrefslogtreecommitdiffstats
path: root/direct_hb.js
diff options
context:
space:
mode:
authorshunirr2008-03-20 16:32:41 +0000
committershunirr2008-03-20 16:32:41 +0000
commitaad373b519fdaf2b04004db3e95c1571c5d9ddf8 (patch)
tree53b2ecdfea348e76a1b7180b76ead6252e02f228 /direct_hb.js
downloadvimperator-plugins-aad373b519fdaf2b04004db3e95c1571c5d9ddf8.tar.bz2
lang/javascript/vimperator-plugins/trunk
lang/javascript/vimperator-plugins/tags/0.5.3 - mkdir trunk, tags - mv some files git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@8221 d0d07461-0603-4401-acd4-de1884942a52
Diffstat (limited to 'direct_hb.js')
-rw-r--r--direct_hb.js215
1 files changed, 215 insertions, 0 deletions
diff --git a/direct_hb.js b/direct_hb.js
new file mode 100644
index 0000000..e734e39
--- /dev/null
+++ b/direct_hb.js
@@ -0,0 +1,215 @@
+// Vimperator plugin: 'Direct Hatena Bookmark'
+// Last Change: 14-Mar-2008. Jan 2008
+// License: Creative Commons
+// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid
+// Parts:
+// http://d.hatena.ne.jp/fls/20080309/p1
+// Pagerization (c) id:ofk
+// AutoPagerize(c) id:swdyh
+//
+// Hatena bookmark direct add script for vimperator0.6.*
+(function(){
+ var isNormalize = true;
+
+ function WSSEUtils(aUserName, aPassword){
+ this._init(aUserName, aPassword);
+ }
+
+ WSSEUtils.prototype = {
+
+ get userName(){
+ return this._userName;
+ },
+
+ get noce(){
+ return this._nonce;
+ },
+
+ get created(){
+ return this._created;
+ },
+
+ get passwordDigest(){
+ return this._passwordDigest;
+ },
+
+ getWSSEHeader: function(){
+ var result = [
+ 'UsernameToken Username="' + this._userName + '", ',
+ 'PasswordDigest="' + this._passwordDigest + '=", ',
+ 'Nonce="' + this._nonce + '", ',
+ 'Created="' + this._created + '"'
+ ].join("");
+
+ return result;
+ },
+
+ _init: function(aUserName, aPassword){
+ var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
+ var seed = (new Date()).toUTCString() + uuidGenerator.generateUUID().toString();
+
+ this._userName = aUserName;
+ this._nonce = this._getSha1Digest(seed, true);
+ this._created = this._getISO8601String((new Date()));
+ this._passwordDigest = this._getSha1Digest(this._getSha1Digest(seed, false) + this._created + aPassword, true);
+ },
+
+ _getSha1Digest: function(aString, aBase64){
+ var cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
+ cryptoHash.init(Ci.nsICryptoHash.SHA1);
+
+ var inputStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
+ inputStream.setData(aString, aString.length);
+ cryptoHash.updateFromStream(inputStream, -1);
+
+ return cryptoHash.finish(aBase64);
+ },
+
+ _getISO8601String: function(aDate){
+ function zeropad(s, l) {
+ while(s.length < l){
+ s = "0" + s;
+ }
+ return s;
+ }
+
+ var result = [
+ zeropad(aDate.getUTCFullYear(), 4), "-",
+ zeropad(aDate.getUTCMonth() + 1, 2), "-",
+ zeropad(aDate.getUTCDate(), 2), "T",
+ zeropad(aDate.getUTCHours(), 2), ":",
+ zeropad(aDate.getUTCMinutes(), 2), ":",
+ zeropad(aDate.getUTCSeconds(), 2), "Z"
+ ].join("");
+ return result;
+ }
+
+ };
+
+ // copied from AutoPagerize(c) id:swdyh
+ function getElementsByXPath(xpath, node) {
+ node = node || document;
+ var nodesSnapshot = (node.ownerDocument || node).evaluate(xpath, node, null,
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+ var data = [];
+ for (var i = 0, l = nodesSnapshot.snapshotLength; i < l;
+ data.push(nodesSnapshot.snapshotItem(i++)));
+ return (data.length > 0) ? data : null;
+ }
+
+ function getFirstElementByXPath(xpath, node) {
+ node = node || document;
+ var result = (node.ownerDocument || node).evaluate(xpath, node, null,
+ XPathResult.FIRST_ORDERED_NODE_TYPE, null);
+ return result.singleNodeValue ? result.singleNodeValue : null;
+ }
+
+ // copied from Pagerization (c) id:ofk
+ function parseHTML(str) {
+ str = str.replace(/^[\S\s]*?<html[^>]*>|<\/html\s*>[\S\s]*$/ig, '');
+ var res = document.implementation.createDocument(null, 'html', null);
+ var range = document.createRange();
+ range.setStartAfter(window.content.document.body);
+ res.documentElement.appendChild(
+ res.importNode(range.createContextualFragment(str), true)
+ );
+ return res;
+ }
+
+ //
+ //
+ //
+ //
+
+ function httpGET(uri,callback){
+ var xhr = new XMLHttpRequest();
+ xhr.onreadystatechange = function(){
+ if(xhr.readyState == 4){
+ if(xhr.status == 200)
+ callback.call(this,xhr.responseText);
+ else
+ throw new Error(xhr.statusText)
+ }
+ };
+ xhr.open("GET",uri,true);
+ xhr.send(null);
+ }
+
+ function getNormalizedPermalink(url){
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET","http://api.pathtraq.com/normalize_url?url=" + url,false);
+ xhr.send(null);
+ if(xhr.status != 200){
+ vimperator.echoerr("Pathtraq: URL normalize faild!!");
+ return undefined;
+ }
+ return xhr.responseText;
+ }
+
+ function getTags(arg){
+ vimperator.plugins.hatena_tags = [];
+ httpGET("http://b.hatena.ne.jp/my",
+ function(mypage_text){
+ var mypage_html = parseHTML(mypage_text);
+ var tags = getElementsByXPath("//ul[@id=\"taglist\"]/li/a",mypage_html);
+ for(var i in tags)
+ vimperator.plugins.hatena_tags.push(tags[i].innerHTML);
+ vimperator.echo("Hatena bookmark: Tag parsing is finished. taglist length: " + tags.length);
+ });
+ }
+ getTags();
+
+ function addHatenaBookmarks(user,password,url,comment,normalize){
+ var target = normalize ? getNormalizedPermalink(url) : url;
+ var request =
+ <entry xmlns="http://purl.org/atom/ns#">
+ <title>dummy</title>
+ <link rel="related" type="text/html" href={target}/>
+ <summary type="text/plain">{comment}</summary>
+ </entry>;
+ var xhr = new XMLHttpRequest();
+ xhr.onreadystatechange = function(){
+ if(xhr.readyState == 4){
+ if(xhr.status == 201)
+ vimperator.echo("HatenaBookmark: success");
+ else
+ vimperator.echoerr("HatenaBookmark:" + xhr.statusText);
+ }
+ };
+ var wsse = new WSSEUtils(user,password);
+ xhr.open("POST","http://b.hatena.ne.jp/atom/post", true);
+ xhr.setRequestHeader("X-WSSE",wsse.getWSSEHeader());
+ xhr.setRequestHeader("Content-Type","application/x.atom+xml");
+ xhr.send(request.toString());
+ }
+ vimperator.commands.addUserCommand(['hbtags'],"Update HatenaBookmark Tags",
+ getTags,
+ {}
+ );
+ vimperator.commands.addUserCommand(['hb'],"Post to HatenaBookmark",
+ function(arg){
+ try {
+ var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
+ var logins = passwordManager.findLogins({}, 'https://www.hatena.ne.jp', 'https://www.hatena.ne.jp', null);
+ if(logins.length)
+ [hatenaUser, hatenaPassword] = [logins[0].username, logins[0].password];
+ else
+ vimperator.echoerr("Hatena bookmark: account not found");
+ }
+ catch(ex) {
+ }
+ addHatenaBookmarks(hatenaUser,hatenaPassword,vimperator.buffer.URL,arg,isNormalize);
+ },{
+ completer: function(filter){
+ var match_result = filter.match(/(.*)\[(\w*)$/); //[all, commited , now inputting]
+ var m = new RegExp("^" + match_result[2]);
+ var completionList = [];
+ for(var i in vimperator.plugins.hatena_tags)
+ if(m.test(vimperator.plugins.hatena_tags[i])){
+ completionList.push([match_result[1] + "[" + vimperator.plugins.hatena_tags[i] + "]","Tag"]);
+ }
+ return [0, completionList];
+ }
+ }
+ );
+})();