Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6088819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:01:48+00:00 2026-05-23T12:01:48+00:00

I want to trigger the click, to which following code is listening: chrome.browserAction.onClicked.addListener(function(tab) {});

  • 0

I want to trigger the click, to which following code is listening:

chrome.browserAction.onClicked.addListener(function(tab) {});

The reason is that I have a working extension, which is listening in a background-script (the addListener above) and executes some scripts on click:

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {file: 'abc.js'});
chrome.tabs.executeScript(tab.id, {file: 'xxx.js'});
});

And now I want to trigger this “onClicked” from a context-menu:

var myContextPage = chrome.contextMenus.create({"title": myTitle, "contexts":["page"],
                                     "onclick": fctContext});

So, I thought, that the easiest way would be to have “click” in fctContext.
Maybe there is a better way, but I don’t how to solve my problem. I also tried to run “executeScript” but this isn’t working neither.

Thanks in advance!

//Update

Solution from answers:
This solution is working:

//Shared code. When the argument length is two, it is coming from the context
// menu, while a single argument is coming from the browser action.
function fctContext() {
   var tab = arguments.length == 2 ? arguments[1] : arguments[0];
   // Do whatever you want with the tab.
}

// Browser Action
chrome.browserAction.onClicked.addListener(fctContext);

// Context Menu
chrome.contextMenus.create({
   title: myTitle,
   contexts: ['page'],
   onclick: fctContext
});

Solution after testing some other things:

function fctStart() {
  chrome.tabs.getSelected(null, function(tab){ 
    chrome.tabs.executeScript(tab.id, {file: 'abc.js'});
    chrome.tabs.executeScript(tab.id, {file: 'xxx.js'});
  }); 
}

In this case, fctStart() is working from any point without having to pass the tab.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:01:49+00:00Added an answer on May 23, 2026 at 12:01 pm

    Remember that arguments are optional in JavaScript. Each function has an associated arguments object. That arguments acts as an array. In your case, one of them requires that while the other doesn’t. Having N arguments is the same in one function (Browser Action) than having M arguments in the other (Context Menu), the only difference between these two arguments is the arguments.callee which provides a way to reference the actual code within the function itself. You don’t have to worry about that if you want something basic.

    Your fctContext could be the share-able code between your browserAction click and your context menu. I did something similar in the Reload All Tabs extension.

    Search for this.reload in https://github.com/mohamedmansour/reload-all-tabs-extension/blob/master/js/reload_controller.js, you will notice that this.reload is being used for the Context Menu and the Browser Action. You just share the code.

    UPDATED with the arguments example baked in:

    In your case, you do the exact same thing.

    // Shared code. When the argument length is two, it is coming from the context
    // menu, while a single argument is coming from the browser action.
    function fctContext() {
       var tab = arguments.length == 2 ? arguments[1] : arguments[0];
       // Do whatever you want with the tab.
    }
    
    // Browser Action
    chrome.browserAction.onClicked.addListener(fctContext);
    
    // Context Menu
    chrome.contextMenus.create({
       title: myTitle,
       contexts: ['page'],
       onclick: fctContext
    });
    

    The problem with the above approach is maintainability, if the API changes it might break. Personally, I would rather explicitly name the arguments. So the user doesn’t have to do a lookup in the arguments array.

    function fctContext(tab) {
       // Do whatever you want with the tab.
    }
    
    // Browser Action
    chrome.browserAction.onClicked.addListener(fctContext);
    
    // Context Menu
    chrome.contextMenus.create({
       title: myTitle,
       contexts: ['page'],
       onclick: function (detail, tab) { fctContext(tab) }
    });
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the following code, which doesn't want to work in
On a click event, I want to add a fragment of html code which
I have the following JQuery code: jQuery(function($) { $j(.follow_btn) .click( function() { var element
I have the following code which does a function similar to the way the
I want to trigger on click of a button this: <asp:Button runat=server ID=submit Text=Submit
I want to trigger an event that causes the ScrollPane to start scrolling up
I often want to trigger a certain function just once, but I need to
If I want to trigger an error in my interpreter I call this function:
I have some javascript click handlers that don't do what I want in IE8.
I have a series of thumbs which I want a click on one to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.