irmorteza
irmorteza

Reputation: 1654

Access to website data from chrome extension

I have an extension, and i want it do followings:

  1. when user double clik and select a word, detect word
  2. Do somthings on word, and show result on small interactive page like tooltip

I looking for something like Google Dictionary extension

May anyone help me? what should i do ?

Thanks in advance. Morteza

Upvotes: 1

Views: 184

Answers (1)

Chris
Chris

Reputation: 5605

When text is selected, the selected text will be shown in an alert window. A good starting point....

manifest.json

{
  "name": "Selecty thingy",
  "version": "1.0.1",
  "manifest_version": 2,
  "description": "Selecty thingy",  
    "browser_action": {
  },
  "permissions": [
    "tabs", "*://*/*"
  ],
  "content_scripts": [
    {
      "matches": ["*://*/*"],
      "js": ["jquery-1.7.2.min.js","content_script.js"],
      "run_at": "document_end"
    }
  ]
}

content_script.js

$(document).ready(function(){
    $('html').mouseup(function() {
        var selectedText = getSelectedText();
        if(selectedText > ''){
            alert(selectedText);
        }
    });

    function getSelectedText() {
        if (window.getSelection) {
            var selection = window.getSelection().toString();
            if(selection.trim() > ''){
                return selection;
            }
        } else if (document.selection) {
            var selection = document.selection.createRange().text;
            if(selection.trim() > ''){
                return selection;
            }
        }
        return '';
    } });

Here is a jsfiddle showing the functionality outside of a chrome extension...

Upvotes: 1

Related Questions