reddit-companion icon indicating copy to clipboard operation
reddit-companion copied to clipboard

Idea for how to handle redirects

Open Kerrick opened this issue 13 years ago • 4 comments

  1. Inject a content script something like the following. (This uses jQuery because it's from my NSFW/Incognito extension, but it could easily be rewritten in native JS if need be)

    $('div.linklisting')
        .delegate('a.thumbnail', 'click', function() { return false; })
        .delegate('a.title', 'click', function() { return false; })
        .delegate('a.thumbnail', 'mouseup', function() {
            var thisUrl = $(this).attr('href');
            if (thisUrl.substr(0,1) == "/")
                thisUrl = "http://www.reddit.com"+thisUrl;
            if (event.button != 2)
                chrome.extension.sendRequest(thisUrl);
            return false;
        })
        .delegate('a.title', 'mouseup', function() {
            var thisUrl = $(this).attr('href');
            if (thisUrl.substr(0,1) == "/")
                thisUrl = "http://www.reddit.com"+thisUrl;
            if (event.button != 2)
                chrome.extension.sendRequest(thisUrl);
            return false;
        });
    
  2. Run a message passing listener on the background page.

    chrome.extension.onRequest.addListener(function(thisUrl, sender, sendResponse) {
                openTabAndForceThisUrl(thisUrl);
            });
    
  3. With openTabAndForceThisUrl(), you can open a new tab using chrome.tabs.create(), look up thisUrl on http://www.reddit.com/api/info.json, and force Companion to use that URL rather than the URL of the tab.

It's not the most graceful since you're interrupting clicks rather than actual navigation, and it's redirect agnostic and will do this logic on every click from reddit, but it should work. I'd fork and write this, but I'm going apartment hunting today and wanted to throw the idea out there.

Kerrick avatar Jul 21 '11 15:07 Kerrick

This is a cool idea, thank you Kerrick. The key idea here is to track the lifetime of the tab rather than redirects starting at a URL. I wonder if we can do this by tracking when tabs are created instead of overriding reddit.com link click functionality. The solution you provided is definitely something that could work, though it runs the risk of conflicting with the site and other extensions.

chromakode avatar Jul 21 '11 15:07 chromakode

track the lifetime of the tab

I really wish the Chrome extension APIs would give a parent item in the tab object when you listen for chrome.tabs.onCreated

though it runs the risk of conflicting with the site and other extensions.

Wow, I didn't even think of that. That's a huge issue since Reddit Companion (and MH) really don't affect the reddit.com pages whatsoever so they play nicely with things like RES.

Kerrick avatar Jul 21 '11 16:07 Kerrick

I spent some time thinking about it today. As far as I can tell, my changes seem to work without breaking anything else.

Unfortunately, since it tracks stuff with the tab id, it only works if you don't open a new tab, so it's only really halfway there (like Kerrick says, that lack of a good way to figure out where new tabs are coming from is really pretty aggravating). On the bright side, if we can figure out a good way to detect redirects in new tabs, it should just be a matter of calling Info.addURL(newTabsURL).

We could just check for new tabs after a link is clicked, but that seems a little too unreliable.

EDIT: It worked really well, until my internet got really slow and it started opening the bar on the wrong pages. Back to the drawing board :( At least the Info class seems to be working well, so it's still just a matter of identifying redirects and adding the new URLs to the existing Info objects

mhweaver avatar Jul 22 '11 14:07 mhweaver