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:
- Select text on any website
- Right-click to open the context menu of my chrome extension
- 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.