| 1 | // PLUGIN_INFO//{{{ |
|---|
| 2 | var PLUGIN_INFO = |
|---|
| 3 | <VimperatorPlugin> |
|---|
| 4 | <name>retweet</name> |
|---|
| 5 | <description>ReTweet This Page.</description> |
|---|
| 6 | <description lang="ja">開いているTweetをReTweetします。</description> |
|---|
| 7 | <author mail="from.kyushu.island@gmail.com" homepage="http://iddy.jp/profile/from_kyushu">from_kyushu</author> |
|---|
| 8 | <version>0.2</version> |
|---|
| 9 | <license>GPL</license> |
|---|
| 10 | <minVersion>1.2</minVersion> |
|---|
| 11 | <maxVersion>2.1</maxVersion> |
|---|
| 12 | <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/retweet.js</updateURL> |
|---|
| 13 | <require type="plugin">_libly.js</require> |
|---|
| 14 | <detail><![CDATA[ |
|---|
| 15 | |
|---|
| 16 | == Command == |
|---|
| 17 | Usage: |
|---|
| 18 | :rtt |
|---|
| 19 | ReTweet This Post. |
|---|
| 20 | |
|---|
| 21 | ]]></detail> |
|---|
| 22 | </VimperatorPlugin>; |
|---|
| 23 | //}}} |
|---|
| 24 | // |
|---|
| 25 | ( |
|---|
| 26 | function() |
|---|
| 27 | { |
|---|
| 28 | var password; |
|---|
| 29 | var username; |
|---|
| 30 | var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); |
|---|
| 31 | var $U = liberator.plugins.libly.$U; |
|---|
| 32 | |
|---|
| 33 | function getBody() |
|---|
| 34 | { |
|---|
| 35 | var body = $U.getFirstNodeFromXPath("//span[@class='entry-content']").innerHTML; |
|---|
| 36 | //return body.replace(/<[^>]*>/g, ""); |
|---|
| 37 | var tags = body.match(/<[^>]*>/g); |
|---|
| 38 | for(var tag in tags) |
|---|
| 39 | { |
|---|
| 40 | body = body.replace(tags[tag],""); |
|---|
| 41 | } |
|---|
| 42 | return body; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | function getUserName() |
|---|
| 46 | { |
|---|
| 47 | return $U.getFirstNodeFromXPath("//div[@class='screen-name']/a").innerHTML; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | function getShortenUrl(longUrl) |
|---|
| 51 | { |
|---|
| 52 | var xhr = new XMLHttpRequest(); |
|---|
| 53 | var req = "http://bit.ly/api?url=" + encodeURIComponent(longUrl); |
|---|
| 54 | xhr.open('GET',req, false); |
|---|
| 55 | xhr.send(null); |
|---|
| 56 | if(xhr.status != 200) |
|---|
| 57 | { |
|---|
| 58 | return longUrl; |
|---|
| 59 | } |
|---|
| 60 | return xhr.responseText; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | function sendTwitter(url,name,body) |
|---|
| 64 | { |
|---|
| 65 | var xhr = new XMLHttpRequest(); |
|---|
| 66 | var statusText = "RT @" + name + " [" + url +"]: " + body; |
|---|
| 67 | xhr.open("POST", "https://twitter.com/statuses/update.json", false, username, password); |
|---|
| 68 | xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
|---|
| 69 | xhr.send("status=" + encodeURIComponent(statusText) + "&source=Vimperator"); |
|---|
| 70 | |
|---|
| 71 | liberator.echo("[RT] Your post was sent."); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | commands.addUserCommand( |
|---|
| 75 | ['retweet[This]','rtt'], |
|---|
| 76 | 'ReTweet This.', |
|---|
| 77 | function() |
|---|
| 78 | { |
|---|
| 79 | try |
|---|
| 80 | { |
|---|
| 81 | var logins = passwordManager.findLogins({}, "http://twitter.com","https://twitter.com",null); |
|---|
| 82 | var body = getBody(); |
|---|
| 83 | var name = getUserName(); |
|---|
| 84 | var url = getShortenUrl(buffer.URL); |
|---|
| 85 | if(logins.length) |
|---|
| 86 | { |
|---|
| 87 | username = logins[0].username; |
|---|
| 88 | password = logins[0].password; |
|---|
| 89 | sendTwitter(url,name,body); |
|---|
| 90 | } |
|---|
| 91 | else if(liberator.globalVariables.twitter_username && liberator.globalVariables.twitter_password) |
|---|
| 92 | { |
|---|
| 93 | username = liberator.globalVariables.twitter_username; |
|---|
| 94 | password = liberator.globalVariables.twitter_password; |
|---|
| 95 | sendTwitter(url,name,body); |
|---|
| 96 | } |
|---|
| 97 | else |
|---|
| 98 | { |
|---|
| 99 | throw "Accont not found"; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | catch(e) |
|---|
| 103 | { |
|---|
| 104 | liberator.echoerr(e); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | ); |
|---|
| 108 | } |
|---|
| 109 | )(); |
|---|