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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:00:23+00:00 2026-05-22T20:00:23+00:00

I am trying to create my first Chrome extension – a duplicate of the

  • 0

I am trying to create my first Chrome extension – a duplicate of the right-click google search option.

This is what I have so far

function searchGoogle() {
  var selectedText = window.getSelection().toString();
  var serviceCall = 'http://www.google.com/search?q=' + selectedText;
  chrome.tabs.create({url: serviceCall});
}

chrome.contextMenus.create({
  "title": "mySearch",
  "contexts":["selection"],
  "onclick": searchGoogle
});

The problem is that the selected text is not being captured, and the query that executes on any selected text is http://www.google.com/search?q=.

What am I doing wrong here? Why does window.getSelection().toString() not work?


After what Bryan suggested, I tried this

var query;
function getText(ocd) {
   query = ocd.selectionText;
}

function searchGoogle() {
  var serviceCall = 'http://www.google.com/search?q=' + query;
  chrome.tabs.create({url: serviceCall});
}

chrome.contextMenus.create({
  "title": "mySearch",
  "contexts":["selection"],
  "onclick": searchGoogle
});

but it searches for http://www.google.com/search?q=undefined

What did I do wrong?

  • 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-22T20:00:24+00:00Added an answer on May 22, 2026 at 8:00 pm

    See Google’s reference on the OnClickData object. When you click on your context menu item, it will send one of those as the first argument to the specified function. So, first of all, you probably want to add a variable to hold it. Once you have that variable (here ocd) you can access the properties listed by Google with ocd.*. In this case, we want ocd.selectionText.

    Most likely, you’ll need to make the following change:

    // Handle click event and store OnClickData object in first argument
    function searchGoogle(ocd) {
      var serviceCall = 'http://www.google.com/search?q=' + ocd.selectionText;
      chrome.tabs.create({url: serviceCall});
    }
    
    chrome.contextMenus.create({
      "title": "mySearch %s", // display "MySearch [selected text]" in menu
      "contexts":["selection"],
      "onclick": searchGoogle // pass an OnClickData object to searchGoogle on click
    });
    

    Since you’re just starting out, I’ll heavily recommend looking to Google’s Developer’s Guide. It usually has what you need in terms of which methods are available, what values they return, etc. for all of their custom chrome.* APIs.

    Also, window.getSelection().getString() is being called from your background script, right? It actually does work, but it works by returning whatever text you’ve selected on your background page. Since you usually can’t interact with your extension’s background page, that’s probably a blank string.

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

Sidebar

Related Questions

I am trying to attempt my first Google Chrome Extension and have a question.
I am trying to create an options page for a Chrome extension. The first
I am trying to create a Google Chrome extension where a user would highlight
I'm new to NHibernate and trying to create my first mapping. I have created
I'm trying to create a simple threading procedure (granted this is my first attempt
So I am trying to create an extension in Chrome (a prototype for a
I'm trying to write a simple google extension that will upon clicking ctrl+alt+x search
I am trying to create my first hello world described here: http://developer.chrome.com/trunk/apps/first_app.html but I
I'm trying to create my first server, a simple chat server. I have a
I am just trying to create my first repo from Ubuntu 11.10 with this

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.