The following extension adds a listener to webNavigation.onCompleted , which is used to remember, index by tabId, both the most recent URL in frameId==0 , for which the event is fired, and the previous URL.
A webNavigation.onBeforeNavigate added to webNavigation.onBeforeNavigate , which monitors the mapping of URLs, in this case stackexchange.com . If the URL matches, the tab URL is updated through tabs.update to return to the last URL for which the webNavigation.onCompleted event was webNavigation.onCompleted .
If the onBeforeNavigate event for frameId is other than 0 , then the tab moves to the previous URL for which the onCompleted event was executed for frameId==0 . If the previous URL was not used, we could get into a cycle in which the current URL is re-loaded due to the URL in one of its frames corresponding to the address we are blocking. The best way to handle this would be to add content script to change the src attribute for the frame. Then we will need to process the frames inside the frames.
blockNavigation.js:
manifest.json:
{ "description": "Watch webNavigation events and block matching URLs", "manifest_version": 2, "name": "webNavigation based block navigation to matched URLs", "version": "0.1", "permissions": [ "notifications", "webNavigation", "tabs" ], "background": { "scripts": ["blockNavigation.js"] } }
source share