10

I've made an extension who's purpose is to redirect urls. I.e: www.google.com becomes: www.mysite.com/?url=www.google.com

I came across this post: How to modify current url location in chrome via extensions

The problem I'm having is that the url's are both processed. The tab initially loads up google.com and only after it's finished my request is shown ( www.mysite.com/?url=www.google.com). Is there any way to stop the initial request from being processed?

Something like:

   chrome.tabs.onUpdated.addListener(function(tabId,obj,tab){
    update.stop() // ??????????? Here I'm missing...
    chrome.tabs.update(tabId,{url:....}, function callback); // My update stuff..
   });

Thoughts?

thank you all.

Community
  • 1
  • 1
Phoenix
  • 1,256
  • 2
  • 17
  • 25
  • Are you using a content script? – Some Guy Sep 12 '11 at 12:52
  • Well, not yet. I'm using background page. – Phoenix Sep 12 '11 at 14:15
  • Oh, yeah, well, of course. I don't know what had gotten into me. Yeah, I don't think doing that would be possible, unless you could control the omnibox as well, which as far as I know, you can't. – Some Guy Sep 12 '11 at 14:23
  • Can it be done using the content script? – Phoenix Sep 12 '11 at 14:32
  • What you're currently doing, yes. What you want to do, no, not as far as I know. I remember there being something like window.stop(). Try using that in a content script; which was injected as early as possible into the page. (Won't help a lot because content scripts usually get injected well after most of the content on the page has loaded.) – Some Guy Sep 12 '11 at 14:36

2 Answers2

10

You're looking for the webNavigation API.

You can register listeners to handle user navigation by modifying or blocking the request on the fly.

In the example below, when a user navigate to www.google.com, before the page even start loading onBeforeNavigate is fired and you can redirect the user to the CSS validation page for that URL:

chrome.webNavigation.onBeforeNavigate.addListener((details) => {
    if(details.url.indexOf("www.google.com") !== -1)) {
        chrome.tabs.update(details.tabId, {
            url: "https://jigsaw.w3.org/css-validator/validator?uri=" + details.url
        });
    }
});

Remember to add the "webNavigation" permission to your extension manifest to get this functionality enabled.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • Looks promising. I was afraid to use that API because it may change. What are the chances that they will? should I "depend" on them although they say not to? – Phoenix Sep 18 '11 at 15:12
  • Sometimes, some API's become no-longer-needed or even non-sense. For example, the sidebar API's, which's going to be removed in future versions since you can now make your own sidebars via content scripts. But I ensure to you, webNavigation is a must have and will get just some minor changes. Have fun. – cvsguimaraes Sep 18 '11 at 17:35
  • Rather then redirect? Is it possible to just block it? – Noitidart Nov 19 '16 at 19:53
  • 1
    @Noitidart Yes, its possible to block requests with webNavigation API. I think that many ad blockers use it. You may have a look on [uBlock Origin](https://github.com/gorhill/uBlock) which is open source and see how they do it. – cvsguimaraes Nov 19 '16 at 22:35
  • Thank you @snolflake I will take look – Noitidart Nov 19 '16 at 22:37
  • In my experience this does not necessarily always block the initial request. – Chris_F Dec 14 '16 at 05:24
0

chrome.tabs.onUpdated is fired two times per tab load - once a tab starts loading, and another time when it finishes loading. If you attach your update to the tab start loading event then it should work relatively quickly. You will still see original url being loaded for a brief moment, but it won't wait until it finishes, as you are describing.

chrome.tabs.onUpdated.addListener(function(tabId,obj,tab){
    if(obj.status == "loading") {
        chrome.tabs.update(tabId,{url:....}, function callback); 
    }
});

I don't think there is a more efficient solution at the moment.

serg
  • 109,619
  • 77
  • 317
  • 330