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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:07:15+00:00 2026-06-01T14:07:15+00:00

My extension adds a context menu whenever a user selects some text on the

  • 0

My extension adds a context menu whenever a user selects some text on the page.

Then, using info.selectionText, I use the selected text on a function executed whenever the user selects one of the items from my context menu. (from http://code.google.com/chrome/extensions/contextMenus.html)

So far, all works ok.

Now, I got this cool request from one of the extension users, to execute that same function once per line of the selected text.
A user would select, for example, 3 lines of text, and my function would be called 3 times, once per line, with the corresponding line of text.

I haven’t been able to split the info.selectionText so far, in order to recognize each line…
info.selectionText returns a single line of text, and could not find a way to split it.

Anyone knows if there’s a way to do so? is there any “hidden” character to use for the split?

Thanks in advance… in case you’re interested, here’s the link to the extension

https://chrome.google.com/webstore/detail/aagminaekdpcfimcbhknlgjmpnnnmooo

  • 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-01T14:07:16+00:00Added an answer on June 1, 2026 at 2:07 pm

    Ok, as OnClickData’s selectionText is only ever going to be text you’ll never be able to do it using this approach.

    What I would do then is inject a content script into each page and use something similar to the below example (as inspired by reading this SO post – get selected text's html in div)

    You could still use the context menu OnClickData hook like you do now but when you receive it instead of reading selectionText you use the event notification to then trigger your context script to read the selection using x.Selector.getSelected() instead. That should give you what you want. The text stays selected in your extension after using the context menu so you should have no problem reading the selected text.

    if (!window.x) {
        x = {};
    }
    
    // https://stackoverflow.com/questions/5669448/get-selected-texts-html-in-div
    x.Selector = {};
    x.Selector.getSelected = function() {
        var html = "";
        if (typeof window.getSelection != "undefined") {
            var sel = window.getSelection();
            if (sel.rangeCount) {
                var container = document.createElement("div");
                for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                    container.appendChild(sel.getRangeAt(i).cloneContents());
                }
                html = container.innerHTML;
            }
        } else if (typeof document.selection != "undefined") {
            if (document.selection.type == "Text") {
                html = document.selection.createRange().htmlText;
            }
        }
        return html;
    }
    
    $(document).ready(function() {
        $(document).bind("mouseup", function() {
            var mytext = x.Selector.getSelected();
            alert(mytext);
            console.log(mytext);
        });
    });​
    

    http://jsfiddle.net/richhollis/vfBGJ/4/

    See also: Chrome Extension: how to capture selected text and send to a web service

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

Sidebar

Related Questions

I'm writing a Safari Extension that adds some functionality to a web page that
I'm writing a Google Chrome extension which manipulates the current page (basically adds a
I have an extension which adds several links on a page dynamically, and all
I'm writing an extension to jQuery that adds data to DOM elements using el.data('lalala',
I'm trying to write a Firefox extension that adds elements to the loaded page.
I have written a Chrome extension that adds a char counter to some input
I would like to create an extension which adds a toolbar button in Thunderbird.
I made an extension for IE9 that adds a toolbar button. The registration looks
I'm working on a Refresh() extension method for ObservableCollection which adds, removes or replaces
Extension Link: https://www.dephormation.org.uk/?page=81 This plugin is great. It has one problem though, on pages

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.