0

I'm trying to cancel a web-request whenever a new tab is opened. I wanted to check this via webNavigation.onCreatedNavigationTarget, but that event seems to be activated "long" after the webRequest.onBeforeRequest.

So I use property Tab.openerTabId to find out if a tab has been opened by another tab. The problem is, I need to get the tab instance (details) by doing a Tag.get(details.tabId), but this is a callback and I cannot return the main function from a callback. Therefor the return {cancel: true} is not recognized and the web-request goes through.

I'm using Tab.onCreated to store the tab, but it's a 50 % chance of being executed after onBeforeRequest.

I've been looking online but in no way I'm able to fix or get around this issue. Is there anything I could do?

Qlii256
  • 460
  • 6
  • 11
  • There's no undocumented methods in the webRequest/tabs API so they won't help you. Maybe you can make the decision in your content script instead. For example you can override `window.open` in the [page context](/a/9517879), also listen to the mousedown event and see if it's a link with `target` attribute. – wOxxOm Sep 25 '19 at 19:40
  • I've been looking into that, but will overriding window.open functionality stop opening a web page when a link is clicked in Chrome? Does Chrome itself use a JS function to open tabs? – Qlii256 Sep 25 '19 at 19:50
  • Overriding means you keep the old function reference and call it when your decision is to allow opening. This is also called hooking and spoofing. – wOxxOm Sep 25 '19 at 19:54
  • Thanks, but does this override all new tabs/windows whenever I click a link, or is this only when specific window.open function is called? – Qlii256 Sep 25 '19 at 19:55
  • My comment mentions both cases. `window.open()` is a javascript function, not related to the standard opening of links. – wOxxOm Sep 25 '19 at 20:03
  • So I should use the click-event? I've been trying to do it via a script in the page context, but "object" is not defined. object.addEventListener("click", myScript); This is what I use. Also, how do I get the target and/or the object that was clicked? – Qlii256 Sep 25 '19 at 20:30
  • This is basic DOM and it's covered in various tutorials and existing answers. In short it's something like `window.addEventListener('mousedown', yourFunction, true)` - use `true` to process the event in the earlier capturing phase. – wOxxOm Sep 25 '19 at 21:06

0 Answers0