Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 8 of 8

This is a discussion on Suppressing right-click context menu within the Plugins Development section, part of the Chrome Plugins category: Hi, I have an extension which requires me to conditionally suppress the right click context menu. I have tried lots ...


  1. #1
    Hemmer is offline Junior Member
    Join Date
    Feb 2010
    Posts
    5

    Default Suppressing right-click context menu

    Hi, I have an extension which requires me to conditionally suppress the right click context menu. I have tried lots of ways, but I can't seem to get it to stop.

    Code:
    // fire function if clicked
    document.onmousedown = checkPos;
    
    function checkPos(e){
    		
    		// if RMB, perform action
    		if(e.button == 2){
    			// do action (without context menu)
    			return false;    
    			
    	         }
    	}
    
    	return true;
    }

  2. #2
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    Try this. I believe that's the only way on Chrome.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  3. #3
    Hemmer is offline Junior Member
    Join Date
    Feb 2010
    Posts
    5

    Default

    Mm I've seen that around, but it doesn't seem to work either Am I using it wrong? I only want to suppress it a condition is satisfied.

    Code:
    ...
    else if(e.button == 2 && condition == blah){
    		window.oncontextmenu = function () {return false;}
    			history.go(1);
    			return false;
    			
    		// MMB closes tab
    		}...

    Note that using the following works (in so much that the menu is suppressed, but there is now an annoying popup box...)

    Code:
    window.oncontextmenu = alert('Hi');

  4. #4
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    Well keep in mind that once you register the event, it's going to work regardless of if the condition is still met or not.
    Firstly, that second one does NOT work. It will simply alert when the line is executed (not when the user right-clicks), and sets window.oncontextmenu to undefined.

    With the first, the anonymous function you're declaring is:
    Code:
    window.oncontextmenu = function () {return false;}
    and the history.go and stuff is not part of the function called when the user right-clicks, only the conditional. Which, after it does back, the event is unregistered since it's a new page now.

    You can also try registering the even to the body, which is probably the better way of doing it. As much as I hate just giving code to people, I can't really think of a good way to just help you figure it out right now.
    Code:
    document.body.addListener("contextmenu",function(){
    	if(condition == blah){
    		history.go(1);
    		return false;
    	}
    	else return true;
    });
    That should probably work.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  5. #5
    Hemmer is offline Junior Member
    Join Date
    Feb 2010
    Posts
    5

    Default

    Thanks for you help. I tried your method with no luck. However, it did inspire me to check the order of an earlier piece of code:

    Code:
    	// RMB goes forward
    		}else if(e.button == 2){
    
    			history.forward();
    			e.preventDefault();
    			return false;
                    }
    I had preventDefault() before the history statement, which was being ignored. Strangely, the context menu still fires if its not possible to go forward (i.e. current page), but that could probably be classified as a feature!. Anyway, I've pestered you enough for now I think! Thanks for your help! Hemmer

    BTW: if you're interested, the "finished" extension is here: https://chrome.google.com/extensions...cnlcondflhgajh

  6. #6
    PAEz's Avatar
    PAEz is offline Moderator
    Join Date
    Aug 2009
    Location
    Australia
    Posts
    656

    Default

    Here's another way to do it.....
    Code:
    window.addEventListener("contextmenu",function(event){
           history.go(1);
           event.preventDefault();
    });
    ....and it will still suppressed even if there is no foward page.

  7. #7
    Hemmer is offline Junior Member
    Join Date
    Feb 2010
    Posts
    5

    Default

    Yeah it turns out that the method I mentioned above was only working due to another extension affecting the results! Final code is:

    Code:
    // suppress right click menu on page edge
    var doContextMenu = function(t) {
       if(t.x == 0) t.preventDefault();
    }
    window.addEventListener('contextmenu', doContextMenu, true);
    then elsewhere:

    Code:
    //  RMB goes forward
    if(e.button == 2){
    			history.go(1);
    		
    		}
    Importantly, I also needed the following in the manifest to get it to work:

    Code:
       "content_scripts": [ {
          "all_frames": true,
          "js": [ "bigback.js" ],
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_start"
       }, {
          "all_frames": true,
          "js": [ "bigback.js" ],
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_end"
       } ]

  8. #8
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    Oh right..return false only works if you overwrite the rest of the events (ie. use it inline.)
    Whoops. :S
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

Similar Threads

  1. Adding item to context menu on right-click on link
    By afin in forum Plugins Development
    Replies: 5
    Last Post: 11-20-2010, 05:05 AM
  2. [New Ext] Context Search - Searching for selected text
    By andreyp123 in forum Chrome Plugins
    Replies: 5
    Last Post: 12-21-2009, 08:22 AM
  3. Context Search And Search Engine Drop-down Box
    By chrome567 in forum Plugins Development
    Replies: 12
    Last Post: 11-06-2009, 07:59 PM
  4. My right-click menu has been changed by Ask.com
    By Demonic Pothead in forum Chrome Tips & Tricks
    Replies: 3
    Last Post: 08-10-2009, 05:51 PM
  5. Taskbar Right-click menu rearrangement ?
    By Achilles97 in forum Chrome Tips & Tricks
    Replies: 4
    Last Post: 08-05-2009, 06:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •