

Easier browsing of Hacker News...for me - nirmal

http://nirmalpatel.com/#hnsplitview<p>HN SplitView is a bookmarklet that adds a link labelled "SPLIT VIEW" to each post on the Hacker News website. This link allows the user to open the post and the discussion page side by side in frames. Simply drag the link for HN SplitView to your toolbar or add it as a favorite.<p>I've been using this and it makes it easier for me to browse. I hope that other members of the HN community find it useful. I've tested it under Safari and FF. Let me know if you guys have any problems.<p>EDIT: It doesn't add the link for posts like this one or those of the "Ask HN:" variety.
======
Xichekolas
So I went to add this splitview thing to my Greasemonkey script for Hacker
News, only I did the same thing without hitting your site for the frameset
(completely clientside js). It works quite nicely, but for some reason I had
to resort to something I consider quite ugly.

Can someone please look at the code below (and read my comments in it) and
tell me why the three addEventListeners that I have commented out won't work?
I'd really really like to know why I am being retarded and how to fix it, as
the solution I ended up with seems unnecessarily ugly.

View code here: <http://userscripts.org/scripts/review/25039>

Or if you don't care about it being ugly and just want to use it:
<http://userscripts.org/scripts/show/25039>

But I'd really appreciate help with figuring out the issue with the
addEventListeners.

~~~
thaumaturgy
Disclaimer: I've never used Greasemonkey (now an Opera user) and have only
spent a few minutes trying to figure out how it interfaces with the browser. I
know my way around JS though.

That said, after doing some testing I think your issue is that the page isn't
completely loaded before the addEventListener is called. Changing the
addEventListener line to a simple element.onclick = function(alert) triggers
an error in the browser; so does trying to ask about the element's onclick
function. Putting in an alert box in place of the addEventListener line pretty
clearly demonstrates that the page is still loading when the code is being
called on the links. Firefox is a little touchy about that.

I'm still working on it, lemme see what happens if I wrap up your script's
initialization section into a document.onload, or something similar.

Edit: Also, elements are being returned as XPCNativeWrapper objects
[<http://developer.mozilla.org/en/docs/XPCNativeWrapper>]. I'm totally
unfamiliar with those; maybe that's interfering with the event attachment
somehow?

Edit: Yeah, looks like it. I wasn't sure, so I had to test, but references to
elements from directly inside the code body get the XPCNativeWrapper objects,
which provide protection for some properties. But, code called from a
setTimeout doesn't get those objects, and gets direct access to the elements.
Not sure how to fix that right since I've never encountered that before.

~~~
Xichekolas
Yeah I learned about the XPCNativeWrapper objects last night too. Basically GM
has access to every object (from window/document on down) as one of these for
security purposes (so the target page can't redefine standard methods). Due to
the vagaries of the way they are implemented, you can't call the
onclick/onkeypress/etc methods directly. It actually states in the
documentation that addEventListener is the preferred (and only working)
method.

You can supposedly get access to the underlying JS object from the
XPCNativeWrapper object with the wrappedJSObject property and then act on it,
but it's a big security no-no. I tried it anyway to no avail.

Your thought on the page not being loaded is kind of where I ended up too.
However, my understanding was that GM ran user scripts _after_ the
window.onload finished, but I could be completely wrong here. If I assign all
the links id attributes in the GM script and then manually add event listeners
in my Firebug console, they work fine. Also, you'll notice that later in my
script I add click listeners on other items (buttons and links) that work
swimmingly, so it's either entirely arbitrary or I'm missing something _very_
obvious.

Thanks for looking at this, I'll look forward to any other thoughts you have.

EDIT: Looks like I was very wrong about the GM script executing after the
window load event. I have been playing with wrapping code in a load event
listener and have indeed got it working that way. Thanks for the suggestion!
I'll hopefully have something polished and posted soon.

~~~
thaumaturgy
Yay! Glad you got it sorted out. I was stumped, and finally ran out of time to
keep tinkering with it. Even with the code firing after the page was done
loading, I still couldn't get it working right.

| You can supposedly get access to the underlying JS object from the
XPCNativeWrapper object with the wrappedJSObject property and then act on it,
but it's a big security no-no. I tried it anyway to no avail.

The other wrong way that I found out about was the unsafeWindow object; same
kind of deal, gives you direct access to the page.

------
nirmal
UPDATES: For greasemonkey/kit users. Clicking Split View links in the right
frame will now not cause internal splitting.

UPDATES: I can't edit the main post anymore so I thought I would try and
summarize the updates here.

Oompa posted a Greasemonkey script here:
<http://userscripts.org/scripts/show/28371>

I modified to work on multiple pages under hacker news, you can find it here:
<http://userscripts.org/scripts/show/28372>

The bookmarklet still works and is located at the URL in the post.

The bookmarklet will not continue adding SPLIT VIEW links after it has been
used once.

------
richesh
very nice, and very useful...can you make it a one click thing? right now I
have to click on the bookmarklet and then click on split view. If it was just
one link that would be awesome. I wonder if this can be tweaked to work from
within Google Reader, hmmm..I'll tinker with that a bit.

thanks again!

~~~
Oompa
I turned this into a Greasemonkey script for any Firefox users.

<http://userscripts.org/scripts/show/28371>

If the author doesn't like it, PM me, and I'll delete this post and the
Userscripts.org post.

~~~
nirmal
<http://userscripts.org/scripts/show/28372>

I just modified it so that it would prompt for install when you clicked
Install Script. GreaseKit for Safari doesn't have an Install User Script
option...work in progress.

~~~
Oompa
I don't understand what you changed? Can you please elaborate?

~~~
nirmal
I just wrapped your code in a closure so it would run immediately. This makes
GreaseKit or Greasemonkey prompt you to install. This way you don't have to go
and install it manually.

Thanks for turning me back on to userscripts!

------
christefano
This script assumes that I visit news.ycombinator.com to read news. I'd prefer
a Firefox extension that compares the addresses of web pages I visit with
links in the news.ycombinator.com RSS feed. That way I'd be notified that
there's a discussion thread about the page I'm currently viewing.

------
vipulsolanki
Wah NirmalBhai, wah.

Btw, There s one minor bug. If I click the bookmarklet more than once, it adds
"|SPLIT VIEW |" more than once.

| SPLIT VIEW | SPLIT VIEW | SPLIT VIEW | SPLIT VIEW | SPLIT VIEW

~~~
nirmal
Fixed.

------
tokipin
i was thinking about proposing this as a feature. because when dealing with
submissions, the constant is that i look at both the article and the comments

------
listic
On Opera, it says "Unsupported browser". Why not?

~~~
nirmal
I had modified the bookmarklet to allow opera but it doesn't like the code on
my splitting page. I'm not sure why though. It's pretty tame JS. Opera also
doesn't show me any errors in the Error Console.

------
mwmanning
This is brilliant.

------
xccx
i'm too retarded to install this but not too proud to admit it.. it sounds
like it scrolls 2+ windows with one control:

xcroll

------
btw0
Simple, but effective. Awesome.

------
kajecounterhack
wow this is so useful. kudos =]

My first bookmarklet too, really.

------
ScottWhigham
I love you, nirmal.

------
agentbleu
10 /10

