If you think a bug might affect users in the 57 release, please set the correct tracking and status flags for Release Management.

TabClose event should be cancelable

NEW
Unassigned

Status

()

Firefox
Tabbed Browser
--
enhancement
8 years ago
8 years ago

People

(Reporter: Samuel, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

8 years ago
User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10
Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10

Well, "should". The fact is I wish "TabClose" event to be cancelable to make a Firefox extension that warns from tab closing depending on the URI.

I have seen the addon Tab Mix Plus allows tab close warning but by mean of 10K+ hardcore javascript lines that I am not able to understand.

So, if it does not have any major drawback, I would like TabClose event were cancelable.

Thank you.

Reproducible: Always

Actual Results:  
TabClose event does not react to preventDefault method.

Expected Results:  
TabClose event does react preventDefault method.

Comment 1

8 years ago
I second this.
I think this could be easily done - just move the code slightly earlier in the method, check the return value of dispatchEvent and return null, right?
Status: UNCONFIRMED → NEW
Ever confirmed: true
OS: Linux → All
Hardware: x86 → All

Updated

8 years ago
Whiteboard: [good first bug]
Dispatching TabClose without removing the tab would actually break most TabClose consumers out there. So I guess you would need a separate event.
(In reply to comment #0)
> [...] to make a
> Firefox extension that warns from tab closing depending on the URI.
> 
> I have seen the addon Tab Mix Plus allows tab close warning but by mean of 10K+
> hardcore javascript lines that I am not able to understand.

Here's an easy way:

var removeTab = gBrowser.removeTab;
gBrowser.removeTab = function (tab) {
  // check URI, warn and return if necessary

  removeTab.apply(gBrowser, arguments);
};
Whiteboard: [good first bug]
(Reporter)

Comment 5

8 years ago
I meant, "warns and allows to cancel".

How could I cancel the tab closing using this way?

I think you just wrote it but I don't understand well what did you mean with "return if necessary".

Thank you.
var removeTab = gBrowser.removeTab;
gBrowser.removeTab = function (tab) {
  try {
    var host = tab.linkedBrowser.currentURI.host;
  } catch (e) {}

  if (host == "bugzilla.mozilla.org" &&
      !confirm("really?"))
    return;

  removeTab.apply(gBrowser, arguments);
};
(Reporter)

Comment 7

8 years ago
OK. I'll try. Thank you very much!
(Reporter)

Comment 8

8 years ago
Hello Dão.

It works :_D

But I don't know why. I mean, the code is intuitive but I ignored completely the possibility of making extensions with this structure and intercepting browser events in this way. What should I read to know what are the fundamentals behind your code?

Going back to the point, when you said
> So I guess you would need a separate event.
you meant a new and previous event such as "TabCloseRequest"?
Eventually, do you think this event would be a better alternative than the code you posted?

Thank you.
The idea is to save a reference to the original function A and to make gBrowser.removeTab point a custom function B instead. B runs your code and also calls A. This is plain Javascript, so you probably want a guide like <https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide>.

The downside of overriding functions is that it needs to be done carefully, otherwise it leads to conflicts with other extensions or when we change the original Firefox code.

The downside of the event approach is that there are too many possible hooks, and introducing events for all of them isn't practical.
You need to log in before you can comment on or make changes to this bug.