0

Possible duplicate question

Prior to asking this, I did some searching and found this question that is similar by name, but I don't understand if it's what I need:

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

Especially Mohamed Mansour's answer looked promising, but after spending over an hour researching, experimenting and testing, I still don't understand it. So I hope this possibly duplicate question will at least help other people with the same confusion that I have.


Question

Besides the title of this question, I'll include some details of what I want to do:

  1. Select text on any website
  2. Right-click to open the context menu of my chrome extension
  3. Send that text to my website's database.

But for testing purposes, I thought I would replace the third point with sending a basic post-request to a site like hurl.it

So how do I send such a post-request from my chrome extension to a specific external website?

I already know how to get the selected text:

manifest.json

{
    "manifest_version": 2,

    "name": "My Plugin",
    "description": "Test extension",
    "version": "0.1.20150917",
    "permissions": [
        "contextMenus"
    ],
    "background": {
        "scripts": ["script.js"]
    }
}

script.js

function handleSelectedText(info,tab) {
    var selectedText = info.selectionText;
    console.log("Selected text: " + selectedText;
    //This where I thought I'd send the data to my domain

    //And some pseudo-code to show how I thought it would work:
    /*
    chrome.extension.postRequest(selectedText, function(response) {
        if(response == success)
            displayNotification("Yay, it worked!");
        else
            displayNotification("Error: " + response.errorMessage);
    });
    */
}


chrome.contextMenus.create({
    title: "Mark error: '%s'", 
    contexts:["selection"],
    id: "cc-mark",
    onclick: handleSelectedText,
});

If I insert Mohamed Mansour's code into mine, I just get an error saying that response is undefined, so I'm assuming I'm doing that so terribly wrong that I shouldn't even include it.

If it isn't obvious already, I should mention that I'm new to Chrome extensions.

Community
  • 1
  • 1
Aske B.
  • 6,419
  • 8
  • 35
  • 62
  • If I understand the question correctly you can simply use the standard XMLHttpRequest with `method: 'POST'` in your background page. – wOxxOm Sep 17 '15 at 20:30
  • @wOxxOm I don't understand what you're saying. When you say "background page", you mean the `script.js`, right? Or does it have to be an html page? I'm gonna try something like this when I get home: http://stackoverflow.com/a/9713078/1380710 I'll let you know if it works, then you can post an answer. Sorry for not being clear. – Aske B. Sep 18 '15 at 07:27
  • Make sure you've read the extension overview, yes it's script.js – wOxxOm Sep 18 '15 at 07:30
  • @wOxxOm I haven't, but you mean [this](https://developer.chrome.com/extensions/overview) right? I'm gonna read it when I get home as well. But usually I don't learn much by reading. I learn by doing. I see the background page is mentioned there, so we'll get a reference point at least. Thanks, and I'll respond later. – Aske B. Sep 18 '15 at 07:34
  • Well, the overview is simple and gives you the terminology, along with answers to the basic questions, so that you don't have to reinvent the wheel. – wOxxOm Sep 18 '15 at 08:09
  • @wOxxOm I read it, and it honestly gives me more questions than answers. Most of the stuff I knew already, just from fiddling around with extensions. Maybe that tells me that it won't be easy for me to learn things as I need them, on-demand, for this project. Maybe I have to spend days on reading stuff I won't need, to find the missing pieces that'll help me out. Generally, I find that most webpages use FAR too many words to explain what's obvious. I prefer succinct examples that explains everything on its own. Sadly, I won't have time to look at your suggestion today. Thanks though. – Aske B. Sep 18 '15 at 19:29
  • As usual, reading should be interspersed with examining a real working and correct code from the official samples for example, or trying some stuff in the devtools console. – wOxxOm Sep 18 '15 at 23:34

0 Answers0