0

Is there any way to load background js even on error pages (such as net::ERR_NETWORK_CHANGED)? I need to keep persistence connection with WS server from the extension, but error pages don't load background js. So I lose the connection and possibility to restart it (due this is automated tool without access to browser ui).

The only solution I found is to use proxy server to customize error pages and load background js inside of them.

sunki
  • 672
  • 11
  • 20
  • I think that you're mixing [Content scripts](https://developer.chrome.com/extensions/content_scripts.html) with [Background pages](https://developer.chrome.com/extensions/background_pages.html). You're using a content script, while you should really be implementing a background page. – Rob W May 07 '13 at 10:10
  • @RobW I'm using both backround and content scripts and no one works on error pages. – sunki May 07 '13 at 12:32
  • Define what you mean by "no one works on error pages". Background pages are only closed when Chrome is closed. Do you want to detect network connectivity loss in order to trigger a reconnect routine? – Rob W May 07 '13 at 13:07
  • @RobW You are right, this is my fault. Please post the answer to give me possibility to accept it :) And yes, Im now trying to trigger lost connection. – sunki May 07 '13 at 16:15

1 Answers1

2

The assertion "Background js doesn't work on error pages" does not make any sense, because there's only one background page per extension two if you use split incognito mode.

So I assume that you want to detect a network connectivity loss in order to restore the web socket. Chrome offers two reliable global events for this: online and offline.

I have published the source code of Real-time desktop notifications for Stack Exchange inbox, which also accounts for network connectivity loss/regain. The relevant Web Socket part of the Chrome extension is found in stackexchange-notifications/Chrome/using-websocket.js on Github.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • I'm a little bit confused you, sorry. I thought that I lose possibility to restart connection with WS server when background script goes down (what actually never happened as you described). So there is no problem to restart server. Also I need possibility to reload tabs when any errors occurs. `navigator.onLine` not solve last problem because there are may be a lot of different problems with site loading. I think I need some kind of ping/pong mechanizm here between tabs and background script. – sunki May 07 '13 at 20:25
  • @skrafi If all you want is to detect a request error, have a look at the [`chrome.webRequest.onErrorOccurred`](https://developer.chrome.com/extensions/webRequest.html#event-onErrorOccurred) event of the `webRequest` API – Rob W May 07 '13 at 20:28
  • I've already tried this, but getting stuck. Can't see this event being triggered. – sunki May 07 '13 at 20:38
  • 1
    @skrafi Did you request the permissions to access this host? Did you look in [the error console of the background page](http://stackoverflow.com/a/10258029) for any errors? – Rob W May 07 '13 at 20:39
  • Have to RTFM 3 times next time. Thanks! – sunki May 07 '13 at 22:52