Permalink
Browse files

User profile button, icon touch-ups, and extremely fragile authentica…

…tion refresh upon site load
  • Loading branch information...
1 parent 3fce188 commit 75e4624c5e4112028055ef370ee72239252e49f4 @chromakode committed Sep 28, 2008
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -24,6 +24,7 @@
<xul:button anonid="buttonSave" label="&save.label;" accesskey="&save.accesskey;"/>
<xul:button anonid="buttonHide" label="&hide.label;" accesskey="&hide.accesskey;"/>
<xul:button anonid="buttonRandom" label="&random.label;" accesskey="&random.accesskey;"/>
+ <xul:button anonid="buttonProfile" label="&profile.label;" accesskey="&profile.accesskey;"/>
<children/>
</xul:hbox>
</content>
@@ -50,6 +51,7 @@
this.buttonSave = document.getAnonymousElementByAttribute(this, "anonid", "buttonSave");
this.buttonHide = document.getAnonymousElementByAttribute(this, "anonid", "buttonHide");
this.buttonRandom = document.getAnonymousElementByAttribute(this, "anonid", "buttonRandom");
+ this.buttonProfile = document.getAnonymousElementByAttribute(this, "anonid", "buttonProfile");
// Display preferences
this.labelScore.hidden = !this.sitePreferences.getBoolPref("showScore");
@@ -58,6 +60,7 @@
this.buttonSave.hidden = !this.sitePreferences.getBoolPref("showSave");
this.buttonHide.hidden = !this.sitePreferences.getBoolPref("showHide");
this.buttonRandom.hidden = !this.sitePreferences.getBoolPref("showRandom");
+ this.buttonProfile.hidden = !this.sitePreferences.getBoolPref("showProfile");
this.compactDisplay = this.sitePreferences.getBoolPref("compactDisplay");
@@ -84,6 +87,9 @@
case "showRandom":
self.buttonRandom.hidden = !self.sitePreferences.getBoolPref("showRandom");
break;
+ case "showProfile":
+ self.buttonProfile.hidden = !self.sitePreferences.getBoolPref("showProfile");
+ break;
case "compactDisplay":
self.compactDisplay = self.sitePreferences.getBoolPref("compactDisplay");
self.update();
@@ -206,6 +212,9 @@
// Random button
updateButton(this.buttonRandom, "chrome://socialite/content/reddit/8ball.png", "&random.label;");
+
+ // Profile button
+ updateButton(this.buttonProfile, "chrome://socialite/content/reddit/profile.png", "&profile.label;");
</body>
</method>
</implementation>
View
@@ -168,7 +168,7 @@ var SocialiteWindow =
let originalURL = channel.originalURI.spec;
if ((channel.loadFlags & Components.interfaces.nsIChannel.LOAD_REPLACE)
- && Socialite.watchedURLs.isWatched(originalURL)) {
+ && (Socialite.watchedURLs.isWatched(originalURL) || Socialite.watchedURLs.isWatched(URL))) {
logger.log("linkStartLoad", "Detected redirect: "+ originalURL +" -> "+ URL);
Socialite.watchedURLs.addRedirect(originalURL, URL);
}
View
@@ -9,9 +9,11 @@ The following images in the directory "content/reddit" are (C) 2008 Mark James,
comments.png
saved.png
savedgray.png
+ 8ball.png
The following images are derivative works and thus (C) 2008 Chromakode, licensed under the Creative Commons Attribution 2.5 License:
hidden.png
hiddengray.png
+ profile.png
More silk icons by Mark James can be found at http://www.famfamfam.com/lab/icons/silk/.
The icon in "skin/general-preferences.png" uses a wrench icon from the Tango Project (http://tango.freedesktop.org) licensed under Creative Commons Attribution Share-Alike 2.5. The icon is thus also Creative Commons Attribution Share-Alike 2.5.
View
@@ -15,6 +15,8 @@
<!ENTITY unhide.accesskey "n">
<!ENTITY random.label "Serendipity!">
<!ENTITY random.accesskey "r">
+<!ENTITY profile.label "User Profile">
+<!ENTITY profile.accesskey "p">
<!ENTITY subreddit.tooltip "Subreddit">
<!ENTITY subreddit.loadingtext "Loading...">
@@ -14,4 +14,6 @@ showSavePreference.accesskey=s
showHidePreference.label=Show hide/unhide button
showHidePreference.accesskey=h
showRandomPreference.label=Show random link button
-showRandomPreference.accesskey=r
+showRandomPreference.accesskey=r
+showProfilePreference.label=Show profile page button
+showProfilePreference.accesskey=p
@@ -1,37 +1,22 @@
-var loginManager = Components.classes["@mozilla.org/login-manager;1"]
- .getService(Components.interfaces.nsILoginManager)
-
-var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
- Components.interfaces.nsILoginInfo,
- "init");
-
logger = Components.utils.import("resource://socialite/utils/log.jsm");
Components.utils.import("resource://socialite/utils/action/action.jsm");
http = Components.utils.import("resource://socialite/utils/action/httpRequest.jsm");
+Components.utils.import("resource://socialite/utils/hitch.jsm");
-var EXPORTED_SYMBOLS = ["getAuthHash", "refreshAuthHash", "RedditAuth"];
-
-STATUS_SUCCESS = 200;
-PASSWORD_HOSTNAME = "chrome://socialite";
-PASSWORD_REALM_PRE = "Socialite authentication for ";
+var EXPORTED_SYMBOLS = ["getAuthInfo", "RedditAuth"];
// ---
function RedditAuth(siteURL, username, userHash) {
this.siteURL = siteURL;
this.username = username;
- this.userHash = userHash;
this.modHash = null;
}
-RedditAuth.prototype.authParams = function(params) {
- params["uh"] = this.userHash;
- return params;
-}
-
-RedditAuth.prototype.snarfModHash = function(modHash) {
- this.modHash = modHash;
+RedditAuth.prototype.snarfAuthInfo = function(doc, win) {
+ this.modHash = extractModHash(doc);
+ this.username = extractUsername(doc);
}
RedditAuth.prototype.authModHash = function(params) {
@@ -41,99 +26,48 @@ RedditAuth.prototype.authModHash = function(params) {
return params;
}
-// ---
-
-var getAuthHash = Action("reddit_auth.getAuthHash", function(siteURL, action) {
- logger.log("reddit_auth", "Making authenticate call");
-
- var logins = loginManager.findLogins({}, PASSWORD_HOSTNAME, null, PASSWORD_REALM_PRE + siteURL);
- if (logins.length == 0) {
- // No stored hash... time to get one
- var act = refreshAuthHash();
- act.chainTo(action);
- act.perform(siteURL);
- } else {
- var login = logins[0];
- var rAuth = new RedditAuth(siteURL, login.username, login.password);
- action.success(rAuth);
- }
-});
-
-var refreshAuthHash = Action("reddit_auth.refreshAuthHash", function(siteURL, action) {
- logger.log("reddit_auth", "Getting new user hash");
+RedditAuth.prototype.refreshAuthInfo = Action("reddit_auth.refreshAuthInfo", function(action) {
+ logger.log("reddit_auth", this.siteURL, "Getting new authentication info");
var act = http.GetAction(
- "http://" + siteURL + "/bookmarklets/",
+ this.siteURL + "login/",
null,
- function success(r) {
- var uh = extractUserHash(r.responseXML);
- var username = extractUsername(r.responseXML);
-
- // Save the userHash we retrieved
- var uhLoginInfo = new nsLoginInfo(
- PASSWORD_HOSTNAME,
- null,
- PASSWORD_REALM_PRE + siteURL,
- username, uh,
- "", ""
- );
-
- // Remove any existing logins
- var logins = loginManager.findLogins({}, PASSWORD_HOSTNAME, null, PASSWORD_REALM_PRE + siteURL);
- for (var i = 0; i < logins.length; i++) {
- if (logins[1].username == username) {
- loginManager.removeLogin(logins[i]);
- }
- }
-
- // Add the new login
- loginManager.addLogin(uhLoginInfo);
-
- var rAuth = new RedditAuth(siteURL, uh);
- action.success(rAuth);
- },
+ hitchThis(this, function success(r) {
+ this.modHash = extractModHash(r.responseXML);
+ this.username = extractUsername(r.responseXML);
+ logger.log("reddit_auth", this.siteURL, "Stored authentication info");
+
+ action.success();
+ }),
function failure(r) { action.failure(); }
);
- act.request.overrideMimeType('text/xml');
+ act.request.overrideMimeType("text/xml");
act.perform();
});
-function evalXHTML_XPath(document, xpath) {
- var XPathResult = Components.interfaces.nsIDOMXPathResult;
- const XHTMLResolver = function(prefix) {
- if (prefix=="xhtml") {return "http://www.w3.org/1999/xhtml"}
- }
-
- return document.evaluate(xpath, document, XHTMLResolver, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
-}
-
-function extractUserHash(document) {
- // A bit elaborate, but this is currently the best way to do things.
- // By getting the information from the bookmarklets page itself, we are hopefully more safe from unassociated page changes as reddit may alter their hashing system...
-
- // Get the first "like" button
+function extractModHash(document) {
try {
- var results = evalXHTML_XPath(document, "//xhtml:a/xhtml:img[@alt='like']/..");
- var likeButton = results.iterateNext();
- const getUserHash = /uh=(\w*)/;
+ let globalsCode = document.getElementsByTagName("script")[0].textContent;
+ const getModHash = /modhash\s+=\s+'(\w+)'/;
- return likeButton.href.match(getUserHash)[1];
+ return globalsCode.match(getModHash)[1];
} catch (e) {
- logger.log("reddit_auth", "Unable to parse bookmarklets page for user hash: " + e.toString());
+ logger.log("reddit_auth", this.siteURL, "Unable to parse page for user hash: " + e.toString());
return null;
}
}
function extractUsername(document) {
// Get the username
try {
- var results = evalXHTML_XPath(document, "//xhtml:span[@class='user']/xhtml:a");
- var usernameLink = results.iterateNext();
- return usernameLink.textContent;
+ let globalsCode = document.getElementsByTagName("script")[0].textContent;
+ const getUsername = /logged\s+=\s+'(\w+)'/;
+
+ return globalsCode.match(getUsername)[1];
} catch (e) {
- logger.log("reddit_auth", "Unable to parse bookmarklets page for username: " + e.toString());
+ logger.log("reddit_auth", this.siteURL, "Unable to parse page for username: " + e.toString());
return null;
}
}
@@ -40,6 +40,7 @@ RedditSite.prototype.onLoad = function() {
SocialiteSite.prototype.onLoad.apply(this, arguments);
this.API = new RedditAPI();
this.API.auth = new RedditAuth(this.siteURL);
+ this.API.auth.refreshAuthInfo().perform();
}
RedditSite.prototype.onSitePageLoad = function(doc, win) {
@@ -67,7 +68,7 @@ RedditSite.prototype.onSitePageLoad = function(doc, win) {
// Snarf the authentication hash using wrappedJSObject
// This should be safe, since Firefox 3 uses a XPCSafeJSObjectWrapper
// See http://developer.mozilla.org/en/docs/XPConnect_wrappers#XPCSafeJSObjectWrapper
- this.API.auth.snarfModHash(win.wrappedJSObject.modhash);
+ this.API.auth.snarfAuthInfo(doc, win);
}
RedditSite.prototype.linkClicked = function(event) {
@@ -288,6 +289,10 @@ RedditSite.prototype.createBarContentUI = function(document, linkInfo) {
).perform();
}, false);
+ this.buttonProfile.addEventListener("click", function(e) {
+ Socialite.openUILink(subredditURL()+"user/"+barContent.linkInfo.redditAPI.auth.username+"/", e);
+ }, false);
+
};
barContent.style.MozBinding = "url(chrome://socialite/content/reddit/redditBar.xml#reddit-content-ui)";
@@ -394,6 +399,7 @@ RedditSite.prototype.createPreferencesUI = function(document, propertiesWindow)
addBooleanPreferenceUI(displayGroup, "showSave", true);
addBooleanPreferenceUI(displayGroup, "showHide", false);
addBooleanPreferenceUI(displayGroup, "showRandom", false);
+ addBooleanPreferenceUI(displayGroup, "showProfile", false);
return propertiesBox;
}
Oops, something went wrong.

0 comments on commit 75e4624

Please sign in to comment.