Event prevent default in Firefox 45 Nightly

Talk about add-ons and extension development.
Post Reply
lastornot
Posts: 9
Joined: September 25th, 2015, 6:07 pm

Event prevent default in Firefox 45 Nightly

Post by lastornot »

The code doesn't prevent for e.g. opening link on middle click:

Code: Select all

var Extension= {

MidClick: function (e) {
    if (e.button === 1) {
        e.preventDefault();
        e.stopPropagation();
        console.log("prevented");
    }
},
...
}
window.addEventListener("click", Extension.MidClick, false);
so it doesn't work with multi-process enabled

it shows "prevented" in console only if I comment prevent functions

how it should be (the code) now?
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Event prevent default in Firefox 45 Nightly

Post by lithopsian »

What are you clicking on? Where is this code being run from? Content process or chrome process? Try setting the capture flag to true, since content events will not bubble up across the process boundary. You say that stopPorpagation() isn't working, but is it even being called?
lastornot
Posts: 9
Joined: September 25th, 2015, 6:07 pm

Re: Event prevent default in Firefox 45 Nightly

Post by lastornot »

it's just a standard FF addon (with chrome.manifest)
lithopsian wrote:What are you clicking on?
but I said about mouse middle clicking on link (url)
of course it's not a full code (but it doesn't matter), there is also a condition that the code works only for certain links

I am just asking how to properly prevent default clicking action on links (opening links) so it also works in FF with multi-process enabled
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Event prevent default in Firefox 45 Nightly

Post by lithopsian »

Frame scripts:
https://developer.mozilla.org/en-US/Fir ... ss_Firefox

I love how people don't give full information, then get snippy when people ask questions ...
lastornot
Posts: 9
Joined: September 25th, 2015, 6:07 pm

Re: Event prevent default in Firefox 45 Nightly

Post by lastornot »

I don't really want read all this, mb I'll wait for some real working extensions for 45 that have the functionality I need and then look at new changes..

Also it's easier with chromium extensions, and I can get the prevent working in FF 45 with that type of extension, but many other features still don't work... https://wiki.mozilla.org/WebExtensions# ... orted_APIs
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Event prevent default in Firefox 45 Nightly

Post by lithopsian »

That's a shame. Pretty easy to do with a frame script. Almost anything you want to do with content in multi-process needs to be done through a frame script. You can access content directly through compatibility shims, although it isn't recommended for performance reasons and may even cause crashes or lockups, but you simply can't listen to most content events from chrome.
lastornot
Posts: 9
Joined: September 25th, 2015, 6:07 pm

Re: Event prevent default in Firefox 45 Nightly

Post by lastornot »

Yeah... It's really easy - https://developer.mozilla.org/en-US/Add ... ss_Firefox ("Porting to the message manager" - an example)
lastornot
Posts: 9
Joined: September 25th, 2015, 6:07 pm

Re: Event prevent default in Firefox 45 Nightly

Post by lastornot »

But I don't recommend to use next line from the example:

Code: Select all

var globalMM = Cc["@mozilla.org/globalmessagemanager;1"]
  .getService(Ci.nsIMessageListenerManager);
Because when you use sendAsyncMessage/sendSyncMessage to communicate frame script with chrome script (content to chrome) it sends masses of the same messaging that it is supposed to be send only one time (for e.g. on one middle click) in the case where you open a few FF windows (for e.g. "shift + left mouse click" for links)

It looks like a bug and it's the same behavior in FF 42 stable with sending messages from frame-scripts, not only in 45
For example you have five opened FF windows, so it sends 5 the same messaging (to chrome-script) even if sendAsyncMessage was called only once and if you close 4 windows it is still sends 5 message until you restart FF

So it is better to use the next line:

Code: Select all

var mm = window.messageManager; // or just "messageManager"
and using that variable for loading frame script
Last edited by lastornot on December 7th, 2015, 4:10 am, edited 1 time in total.
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Event prevent default in Firefox 45 Nightly

Post by lithopsian »

It isn't a bug. It is doing exactly what it's supposed to. You're just loading your frame script five times in every window.
lastornot
Posts: 9
Joined: September 25th, 2015, 6:07 pm

Re: Event prevent default in Firefox 45 Nightly

Post by lastornot »

But with

Code: Select all

messageManager.loadFrameScript("chrome://____/content/frame-script.js", true);
click event listener works normally in all windows and all tabs of windows (so all of it uses frame-script). So what is the reason to use "Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager)" instead of "messageManager"?

for e.g. frame-script:

Code: Select all

addEventListener("mousedown", function(e) {
	if (e.button === 1) {
		sendAsyncMessage("id_extension@example.com:Name", { console : "you clicked on the page" });
	}
});
chrome:

Code: Select all

messageManager.addMessageListener(""id_extension@example.com:Name", listener);
function listener(message) {
	console.log(message.data.console);
}
and:
if you close 4 windows it is still sends 5 message until you restart FF
So I click (middle button) once on the page in my last remaining window and get 5 messages "you clicked on the page". I don't understand what is the point of it.
Last edited by lastornot on December 7th, 2015, 4:08 am, edited 1 time in total.
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Event prevent default in Firefox 45 Nightly

Post by lithopsian »

Again, you don't read my message. What is your problem? You called the global message manager five times, so you got the frame script five times in every window, so you get five messages on every click. That is exactly "the point of it". Luckily, someone with far more patience than me explained it all so you don't have to bother yourself looking at the documentation (although there is an excellent picture in the documentation that you really should look at anyway):
https://discourse.mozilla-community.org ... er/5521/10
Post Reply