Need help trying to modify an add-on

Talk about add-ons and extension development.
Post Reply
Orangutan100
Posts: 54
Joined: July 9th, 2015, 11:56 am

Need help trying to modify an add-on

Post by Orangutan100 »

Hello,

I am using this add-on Image Search Options. This add-on shows a submenu made up of reverse image search engines in the context menu upon right clicking an image. What I would like is for me to:
1) open the result page in a background tab upon (left) clicking a context menu entry
2) open the page in the same tab upon right clicking a context menu entry.

The add-on already has an option to set the former (action for left click) and I have configured it accordingly, but I am still trying to find a way to do the latter.

I don't know much Javascript, and although I was able to modify the previous such add-on that I used because all I had to do was change the part that checks if it is a middle click to check if it is a right click (by detecting event 2), I can't do that here because the code is much too different. I have looked at the relevant MDN pages (1, 2), but the event listener functions that the add-on has (in imagesearchoptions.js) are all inline and don't have any arguments where the event could be passed. I can't even tell where to begin with this stuff.

I would really appreciate if somebody could tell me what would be the simplest way to do the above, even if it's the crudest way possible. I totally don't mind hard-coding this option for the right click, without any UI to change it and I don't need to sign the add-on either.

Thanks in advance.

Edit: reworded to hopefully be clearer. Sorry for the confusion.
Last edited by Orangutan100 on October 26th, 2016, 1:28 pm, edited 2 times in total.
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Howto modify addon to open link in same tab upon right c

Post by lithopsian »

This addon insinuates itself into the process of displaying a context menu using the oncontextmenu handler. You can see this in ff-overlay.xul. It simply sets a flag at this point and then lets the context menu be displayed. Then later it does stuff in the popupshowing event handler.

You can insinuate your code into the same place if you want. I'm guessing you already know how to get the link, if you're actually on a link, and open it? The extra thing here is that you will want to prevent the default behaviour of the event (at least if you are on a link) which would be to open the context menu. You do this by calling event.preventDefault(). Obviously you'll need to pass the event into rightClick() which isn't currently done. Should be quite simple.

There are probably addons that cause this behaviour already, although they might not cooperate with this addon.
Orangutan100
Posts: 54
Joined: July 9th, 2015, 11:56 am

Re: Howto modify addon to open link in same tab upon right c

Post by Orangutan100 »

Thanks a lot for responding.

I am sorry, I feel like I didn't explain myself very well (English isn't my first language). I am gonna try it again. What I want the add-on to be able to do is:
1) Show me a submenu containing entries for websites that help reverse search images when I right click on an image, like this:

Image
2) When I (left) click an entry in this submenu (say, 'SauceNao Search'), open the result page in a new background tab.
3) But if I instead right click on an entry in this submenu, open the result page in the same tab.

Currently, the extension does (1), gives you an option to pick (2) as per your choice but doesn't do (3). I am trying to add an ability to do (3) as well, without taking away the ability to do (2).

I believe that I can hard-code a crude version of (3), by editing the line

Code: Select all

menuItem.addEventListener("command", function(){imagesearchoptions.multiCommand(1);}, false);
at line 83 in imagesearchoptions.js to something like:

Code: Select all

menuItem.addEventListener("command", function(e){if(e.button == 1) where = "new" else if(e.button == 2) where = "same"; imagesearchoptions.multiCommand(1,where);}, false);
and then further modify the functions `multiCommand` and `searchCommand` in imagesearchoptions.js itself to take 1 argument extra each which will finally be used in the function `searchCommand` in the following manner: replace line numbers 432-436 with

Code: Select all

 if(where == "new") {
--<copy-paste line numbers 432-436 here>
}
else if(where == "same")
loadLocation = 0;
which should work alright when the `switch(loadLocation)` part at line 447 runs, as `case = 0` for `loadlocation` makes the search page open in the same tab.

Am I somewhat close? Is something like this gonna work?
lithopsian
Posts: 3664
Joined: September 15th, 2010, 9:03 am

Re: Need help trying to modify an add-on

Post by lithopsian »

So you want to do this by right-clicking on the menu? You kind of have the right idea, but what you suggest won't actually work. The command event doesn't have a button property, because it is not a mouse event. I don't think a right click in a menu even activates the command event. You need to monitor the click event, so add an extra event listener, probably activating a brand new function. Looks like you might need to add a line in four different places, not sure if you need it both for searchCommand and multiCommand. In the new function you can call the existing searchCommand or multiCommand functions with a flag, or just do everything yourself in your function.

If you want to make it configurable, you can always add a preference and not add any UI. For your purposes, about:config might be a good enough way to set the preference. You can always add some UI later if you want to learn a a bit more.

Also, javascript requires declarations of local variables (eg. "var where"), otherwise they are globals. Your variable "where" would be a global (strictly, a property of window: where === window.where) which could clash with other addons or Firefox itself. Or you can avoid the use of a flag variable completely using the trinary ?: operator.
Post Reply