5

I'm trying to get something similar to a typo correction for the location bar on mozilla sdk. The user inputs a url on the locationbar and it gets changed before it is loaded. I've tried:

sdk/pagemod as seen here. The problem is it runs when the page starts rendering, not before it starts downloading it.

sdk/tabs => It doesn't have a event that gets called before it starts loading a website.

Above is what I've found that would catch websites the user is trying to enter into. I have also found sdk/system/events, but the only event I found I could use is http-on-modify-request, this does catch every http request. The problem is, you can't modify the url of that request.

To solve this I've seen 2 options:

This addon's way => Get the current tab and load there the new url. The problem is clear, the request may not be from a url the user is trying to load, but from a url inside a page's html. I tried comparing the request url to tabs.activeTab.url, but it gets set after the http-on-modify-request event is sent.

or the solution explained here that I am not sure could work (it is not for mozilla sdk).

So basically my problem right now could be solved with:

-a way to capture the url from the location bar before it starts loading & changing it (I haven't seen anything for this on the sdk) or -a way to know when a request corresponds to a website the user is trying to load in the location bar or -a way to modify the request (url)

Kara
  • 6,115
  • 16
  • 50
  • 57
dyeray
  • 1,056
  • 6
  • 17
  • 2
    if you end up not using sdk use this: http://stackoverflow.com/questions/21947483/security-error-when-trying-to-load-content-from-resource-in-a-firefox-addon-sdk – Noitidart Mar 30 '14 at 00:50
  • 1
    Thanks, that way to redirect shown in the question is what I was looking for. – dyeray Mar 30 '14 at 02:41
  • 1
    Cool man, yeah use that channel.redirectTo function, just note its FF20+ only, for support prior to FF20 you will have to abort the channel and replace it – Noitidart Mar 30 '14 at 02:54

2 Answers2

5

I do that in my add-on Google Redirects Fixer which source code you can see here.

Basically, what you're looking for is listening to the http-on-modify-request event. If you follow that code you'll see how to intercept the request and, under certain conditions, abort it and replace it with a new request.

Hope it helps.

matagus
  • 6,136
  • 2
  • 26
  • 39
  • I was doing it that way. I looked at your code to do this and I've been using my own addon for a couple of weeks but I discovered a problem with this approach: or maybe it's a bug in my code and I didn't understand it. The problem is: The request can be from a file (favicon, image, etc.) linked from the html. In that case it is not correct to change the content of the current tab. – dyeray Mar 30 '14 at 02:32
1

"sdk/pagemod as seen here. The problem is it runs when the page starts rendering, not before it starts downloading it."

You can tell it to attach before any content is loaded with contentScriptWhen: start.

Load content scripts immediately after the document element is inserted into the DOM, but before the DOM content itself has been loaded

willlma
  • 7,353
  • 2
  • 30
  • 45
  • 1
    I don't have right now my development environment to recheck this, but as I said in the question, I tried something very similar to this: https://stackoverflow.com/questions/21258449/how-to-get-url-of-a-tab-before-the-page-gets-loaded-in-mozilla-addon-sdk so, I did use contentScriptWhen: start and the page would show up for a moment before chaning the domain. And I think it makes sense, because the pagemod attaches itself to the webpage, so this probably means not after finishing loading, but after starting it (the document has to exist). – dyeray Mar 31 '14 at 20:22