Ram
Ram

Reputation: 21

How to get url of a tab before the page gets loaded in Mozilla Addon SDK?

I am creating an addon for blocking sites on user request.I have done- getting user input and storing in simple-storage.Now i want to access the url of the tab(s) before the page gets loaded so that i can process the url and gets it hostname to block the site.

Upvotes: 2

Views: 449

Answers (2)

willlma
willlma

Reputation: 7543

You do this using the PageMod module with onAttach.

pageMod.PageMod({
    contentScriptWhen: 'start', //This says not to wait until the page is ready
    include: ['*'],
    //Forget about contentScript(File), we're not attaching a script
    onAttach: function(worker) {
        var tabUrl = worker.tab.url;
        if(tabUrl==myString) worker.tab.url = 'http://arabcrunch.com/wp-content/uploads/2013/05/block-website.jpeg'
    }
});

But I recommend doing it differently. Instead of checking every URL yourself then doing something , you create an array of URLs or partial URLs and set include: myArrayOfUrls. Then you don't need the if clause in onAttach, you already know that it's one of the websites you want to block.

Upvotes: 2

Yolanda Ruiz
Yolanda Ruiz

Reputation: 696

You can register to the http-on-modify-request notification from the Observer Service. You'll get notified just before the request is made and you can get the URL.

See the following resources on the topic: Setting HTTP request headers for registration example and Observer Notifications for the list of notifications you can register to.

What you describe is essentially what the Adblock Plus extension is doing, maybe you could use it directly or look into its code.

Upvotes: 0

Related Questions