Thursday, April 7, 2016

Showing a notice when site fails to load completely

Leave a Comment

Sometimes during overload some sites fail to load.

I can detect this error using chrome.webRequest.onErrorOccurred api.

I guess the content script will not run at all in this case so sending message from background page to content script is of no use.

How can I paste a notice in the site body that it has failed to load?

Maybe using script execute from the background page? Will the page have a body content?

1 Answers

Answers 1

You've got a few options here. If you're using chrome.webRequest.onErrorOccurred() though I'd suggest you redirect the tab to an error page when an error occurs, using chrome.tabs.update().

For example:

chrome.webRequest.onErrorOccurred.addListener(function(details)  {         chrome.tabs.update(details.tabId, {url: "URL FOR AN ERROR PAGE"}); }, {types: ["main_frame"]}); 

This will redirect the tab the error occurred in when a web request with a resourceType of main_frame errors.

There are a few things you need to consider here. Do you only want to capture errors from requests with a type of main_frame? If not, just remove the 'types' filter from the event.

The other thing you need to consider is what page you're redirecting to. You can package a HTML file within your extension and then redirect to that. To generate the URL of your error page you can run chrome.extension.getURL('customerrorpage.html').

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment