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 8589261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:00:48+00:00 2026-06-11T23:00:48+00:00

I need to execute script once user clicked my context menu item. So for

  • 0

I need to execute script once user clicked my context menu item.

So for the purpose I created the context menu from my background js:

chrome.contextMenus.create({"title": title, "contexts": contexts,
                                       "onclick": genericOnClick});

It appears as expected. Later on from the genericOnClick I try to execute my script:

chrome.tabs.executeScript(null, {code: "console.log('test 1');"}, function() {
      console.log("test 2");
  });

I can see that the “test 2” is printed to console but “test 1” never gets printed. What am I doing wrong?
I’ve tried adding the console.log sentence to a separate js file but it failed to print it as well:

chrome.tabs.executeScript(null, {"file": 'content_script.js'}, function() {
      console.log("test 2");
  });

Note: my content_script.js is not defined in manifest. My manifest looks like follows:

{
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Sample extension",
  "page_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "http://*/*",
    "https://*/*",
    "tabs",
    "contextMenus"
  ],
  "background": {
    "scripts": ["sample.js"]
  },

  "icons": {
    "16": "icon16.png"
  }
}

Thank you in advance.

  • 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-06-11T23:00:50+00:00Added an answer on June 11, 2026 at 11:00 pm
    // addListener
    chrome.browserAction.onClicked.addListener(function() {
        chrome.tabs.executeScript(null, {file: "content_script.js"}, function() {
    
            console.log("test 2");
        });
    });
    
    
     // Context Menu
    chrome.contextMenus.create({
        title: myTitle,
        contexts: ['page'],
        onclick: function (detail, tab) { fn(tab) }
    });
    

    so;

    "permissions": [
    "tabs", "http://*/*", "https://*/*"
    ]
    
    chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});
    

    or:

    // Functional structure
    function hi() { alert("hi"); };
    
    // hi function toString after run  function (hi)()
    chrome.tabs.executeScript(null, { code: "(" + hi.toString() + ")()" });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute from Java a batch script, which does following 1) Once
I need to execute a script in the background through a service. The service
I need to execute a bash script on boot. To do so I created
I have a bash script which need to execute some php scripts and to
I need to be able to execute a PS1 script that resides on a
Sometimes (in customer's PCs) I need a python script to execute in the Windows
I'm writing a script and I need to create a loop that will execute
How to execute a php script from another ? I want to execute 3
I have a python script that, once executed from command line, performs the needed
I need a crontab syntax which should execute a specific PHP script /var/www/html/a.php every

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.