| 1 | /** |
|---|
| 2 | * Multi login for Tumblr |
|---|
| 3 | * cho45 <cho45@lowreal.net> |
|---|
| 4 | * 2007-09-24 |
|---|
| 5 | * |
|---|
| 6 | * License: |
|---|
| 7 | * Creative Commons by |
|---|
| 8 | * http://creativecommons.org/licenses/by/3.0/ |
|---|
| 9 | * |
|---|
| 10 | * Using information in your password manager, |
|---|
| 11 | * you have to logged in tumblr previously. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | if (typeof MultiUserOnTumblrService != "function") { |
|---|
| 16 | var MultiUserOnTumblrService = function () { |
|---|
| 17 | if (arguments.callee.instance) { |
|---|
| 18 | // some complex process for reloading |
|---|
| 19 | for (let prop in arguments.callee.prototype) { |
|---|
| 20 | if (arguments.callee.prototype.hasOwnProperty(prop)) { |
|---|
| 21 | arguments.callee.instance[prop] = arguments.callee.prototype[prop]; |
|---|
| 22 | } |
|---|
| 23 | } |
|---|
| 24 | return arguments.callee.instance; |
|---|
| 25 | } else { |
|---|
| 26 | this.initialize.apply(this, arguments); |
|---|
| 27 | arguments.callee.instance = this; |
|---|
| 28 | } |
|---|
| 29 | }; |
|---|
| 30 | } |
|---|
| 31 | MultiUserOnTumblrService.prototype = { |
|---|
| 32 | ID : "status-bar-multi-user-tumblr-uc", |
|---|
| 33 | |
|---|
| 34 | initialize : function () { |
|---|
| 35 | var self = this; |
|---|
| 36 | this.manager = Components.classes["@mozilla.org/passwordmanager;1"] |
|---|
| 37 | .getService(Components.interfaces.nsIPasswordManager); |
|---|
| 38 | this.manageri = Components.classes["@mozilla.org/passwordmanager;1"] |
|---|
| 39 | .getService(Components.interfaces.nsIPasswordManagerInternal); |
|---|
| 40 | this.cookies = Components.classes["@mozilla.org/cookieService;1"] |
|---|
| 41 | .getService(Components.interfaces.nsICookieService); |
|---|
| 42 | this.IOService = Components.classes["@mozilla.org/network/io-service;1"] |
|---|
| 43 | .getService(Components.interfaces.nsIIOService); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | var statusbar = document.getElementById("status-bar"); |
|---|
| 47 | this.panel = document.createElementNS(kXULNS, "statusbarpanel"); |
|---|
| 48 | |
|---|
| 49 | this.img = document.createElementNS(kXULNS, "image"); |
|---|
| 50 | this.iconimg = <><![CDATA[ |
|---|
| 51 | data:image/gif;base64, |
|---|
| 52 | R0lGODlhDgAOAKIBAEx/GP///6PWcIe6VHyuSoG0To/CXP7+/iH5BAEAAAEA |
|---|
| 53 | LAAAAAAOAA4AAAM7GLoMsC5KIR6gOBdCnBhgGHLeIEVkcBUPo1pr6wbmNStX |
|---|
| 54 | XcmQ0KmUk6dzMRgyGFJRVNhwfgCQMALhzRIAOw== |
|---|
| 55 | ]]></>.replace(/\s+/g, ""); |
|---|
| 56 | this.img.setAttribute("src", this.iconimg); |
|---|
| 57 | this.panel.appendChild(this.img); |
|---|
| 58 | |
|---|
| 59 | this.lbl = document.createElementNS(kXULNS, "label"); |
|---|
| 60 | this.panel.appendChild(this.lbl); |
|---|
| 61 | |
|---|
| 62 | this.menu = document.createElementNS(kXULNS, "menupopup"); |
|---|
| 63 | |
|---|
| 64 | this.panel.appendChild(this.menu); |
|---|
| 65 | this.panel.addEventListener("click", function () { |
|---|
| 66 | self.onPanelClick.apply(self, arguments); |
|---|
| 67 | }, false); |
|---|
| 68 | |
|---|
| 69 | var t = document.getElementById(this.ID) |
|---|
| 70 | if (t) t.parentNode.removeChild(t); |
|---|
| 71 | this.panel.id = this.ID; |
|---|
| 72 | statusbar.appendChild(this.panel); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | this.setStatus("Not logged in"); |
|---|
| 76 | }, |
|---|
| 77 | |
|---|
| 78 | onPanelClick : function (e) { |
|---|
| 79 | // var foundHost = {value:""}, foundUser = {value:""}, foundPass = {value:""}; |
|---|
| 80 | // this.manageri.findPasswordEntry("http://www.tumblr.com", null, null, foundHost, foundUser, foundPass); |
|---|
| 81 | // alert([foundUser.value, foundPass.value]); |
|---|
| 82 | |
|---|
| 83 | var matched = []; |
|---|
| 84 | var passwords = this.manager.enumerator; |
|---|
| 85 | var pass; |
|---|
| 86 | while (passwords.hasMoreElements()) { |
|---|
| 87 | var pass = passwords.getNext().QueryInterface(Components.interfaces.nsIPassword); |
|---|
| 88 | if (pass.host == "http://www.tumblr.com") matched.push(pass); |
|---|
| 89 | } |
|---|
| 90 | while (this.menu.firstChild) this.menu.removeChild(this.menu.firstChild); |
|---|
| 91 | matched.forEach(function (e) { |
|---|
| 92 | var mi = document.createElementNS(kXULNS, "menuitem"); |
|---|
| 93 | mi.setAttribute("label", e.user); |
|---|
| 94 | var self = this; |
|---|
| 95 | mi.addEventListener("command", function () { self.switchUser(e) } , false); |
|---|
| 96 | this.menu.appendChild(mi); |
|---|
| 97 | }, this); |
|---|
| 98 | this.menu.showPopup(this.panel, -1, -1, "popup", "bottomleft", "topleft"); |
|---|
| 99 | |
|---|
| 100 | }, |
|---|
| 101 | |
|---|
| 102 | switchUser : function (password) { |
|---|
| 103 | var self = this; |
|---|
| 104 | this.sessions = this.sessions || {}; |
|---|
| 105 | |
|---|
| 106 | // var cookie = this.cookies.getCookieString(this.IOService.newURI("http://www.tumblr.com", null, null), null) |
|---|
| 107 | self.setStatus("Logging in..."); |
|---|
| 108 | var req = new XMLHttpRequest; |
|---|
| 109 | req.open("POST", "http://www.tumblr.com/login", true); |
|---|
| 110 | req.onload = function (e) { try { |
|---|
| 111 | if (req.responseText.match(/Logging in/)) { |
|---|
| 112 | self.setStatus(password.user); |
|---|
| 113 | content.location = "javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://www.tumblr.com/share',l=d.location,e=encodeURIComponent,p='?v=3&u='+e(l.href) +'&t='+e(d.title) +'&s='+e(s),u=f+p;try{if(!/^(.*\\.)?tumblr[^.]*$/.test(l.host))throw(0);tstbklt();}catch(z){a =function(){if(!w.open(u,'t','toolbar=0,resizable=0,status=1,width=450,height=430'))l.href=u;};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();}void(0)"; |
|---|
| 114 | } else { |
|---|
| 115 | self.setStatus("Failed"); |
|---|
| 116 | } |
|---|
| 117 | } catch (e) { alert(e) } }; |
|---|
| 118 | req.onerror = function (e) { |
|---|
| 119 | alert(e); |
|---|
| 120 | }; |
|---|
| 121 | req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
|---|
| 122 | req.send([ |
|---|
| 123 | "email=", encodeURIComponent(password.user), |
|---|
| 124 | "&password=", encodeURIComponent(password.password), |
|---|
| 125 | ].join("")); |
|---|
| 126 | |
|---|
| 127 | this.currentUser = password; |
|---|
| 128 | }, |
|---|
| 129 | |
|---|
| 130 | setStatus : function (msg) { |
|---|
| 131 | // this.lbl.setAttribute("value", msg); |
|---|
| 132 | this.panel.setAttribute("tooltiptext", msg); |
|---|
| 133 | }, |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | new MultiUserOnTumblrService(); |
|---|