Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text selection and bubble overlay as Chrome extension

I am looking for a way to select text on a website in Chrome and have a overlay/tooltip pop with content depending on the text selection.

Has anyone done this before or knows from the top of their heads how to make the toolip pop-up?

Much appreciated.

like image 822
baik Avatar asked Dec 10 '10 13:12

baik


People also ask

How do I add an overlay to Chrome?

When you go to the Chrome Web Store you just search for 'Color Overlay'. Select the 'Add to Chrome' Button. (At some stage you may be asked to sign into your Chrome Browser using your Gmail address).

What is the Chrome extension that reads text?

SpeakIt. SpeakIt is a simple TTS tool. Kids highlight text in Chrome, click a button, and listen as the words are read aloud. This extension can read in more than 50 languages.

How do you select multiple texts in Chrome?

- When selecting separate words throughout a web page, you can double-click while holding ctrl instead of dragging to select. - When making overlapping selections, ctrl+z will remove the combined selection.


Video Answer


2 Answers

All you need to do is listen for mouse events:

  • mousedown: to hide the bubble.
  • mouseup: to show the bubble.

For example, this might help you get started. More tweaks are needed, to figure out if you initiated the selection from down->up, right->left, etc (all directions). You can use the following code as a startup:

contentscript.js

// Add bubble to the top of the page.
var bubbleDOM = document.createElement('div');
bubbleDOM.setAttribute('class', 'selection_bubble');
document.body.appendChild(bubbleDOM);

// Lets listen to mouseup DOM events.
document.addEventListener('mouseup', function (e) {
  var selection = window.getSelection().toString();
  if (selection.length > 0) {
    renderBubble(e.clientX, e.clientY, selection);
  }
}, false);


// Close the bubble when we click on the screen.
document.addEventListener('mousedown', function (e) {
  bubbleDOM.style.visibility = 'hidden';
}, false);

// Move that bubble to the appropriate location.
function renderBubble(mouseX, mouseY, selection) {
  bubbleDOM.innerHTML = selection;
  bubbleDOM.style.top = mouseY + 'px';
  bubbleDOM.style.left = mouseX + 'px';
  bubbleDOM.style.visibility = 'visible';
}

contentscript.css

.selection_bubble {
  visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
  background:-webkit-gradient(linear, left top, left bottom, from(#2e88c4), to(#075698));
}

manifest.json

Change the matches portion to the domain you want to inject these content scripts.

...
...
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "css": ["main.css"],
      "js": ["main.js"],
      "run_at": "document_end",
      "all_frames": true
    }
...
...

If you want to style it to look like a bubble, Nicolas Gallagher did some awesome CSS3 demos for bubbles!

like image 54
Mohamed Mansour Avatar answered Oct 13 '22 02:10

Mohamed Mansour


I wrote something similar to what you're asking about. I listened for the user to select text and when there was a selection, I displayed a list of links for things like "Note on Facebook", "Define", etc.

A day or two after I started to write it, I found that google was going to add context menu support in a future release so I didn't touch this until recently (when I converted to context menus).

Take a look at the code:

http://code.google.com/p/select-actions/source/browse/trunk/select_actions.js?r=4

like image 2
Jim Schubert Avatar answered Oct 13 '22 00:10

Jim Schubert