Adding text to textarea when a context menu is clicked

1,594 views
Skip to first unread message

Pratik K Anand

unread,
Sep 24, 2010, 10:30:49 AM9/24/10
to Chromium-extensions
Hi, I'm currently working on an extension that adds text to the
textarea depending on the context menu item that is clicked.
(U can say trying to make "clippings" extension for chrome)
But whenever I click on the context menu it's not adding any text.

function novio() {
var x=document.getElementsByClassName("pratik");
x.value="Hey";
}
// code removed
var parent = chrome.contextMenus.create({"title": "Clippings",
"contexts":[context]});

var novio = chrome.contextMenus.create(
{"title": "No violation", "contexts":[context],"parentId":parent,
"onclick": novio});


Can I get any pointers?


--
Thanks for your time,
Pratik K Anand

PhistucK

unread,
Sep 25, 2010, 1:55:56 PM9/25/10
to Pratik K Anand, Chromium-extensions
Unfortunately, you currently cannot get the DOM of the element from which the context menu was opened.
What you can do, is inject a content script into the page that are supposed to have your context menu prior to the context menu selection and monitor all of the elements for oncontextmenu and track it. Then send a message from the background page once your context menu command was selected (and the data you want to transfer to that content script) and the content script will add the text to the textarea.

PhistucK




--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Jesselnz

unread,
Sep 26, 2010, 11:41:38 AM9/26/10
to Chromium-extensions
I just made an extension that does exactly this. I found that after
the user right clicks in a textarea, document.activeElement will point
to that textarea. So you just need a content script injected into all
relevant pages containing something along the lines of:

chrome.extension.onRequest.addListener(function(request) {
if (request.text)
document.activeElement.value += request.text;
});

Of course, if you want to get fancy you can use node.selectionStart
and node.selectionEnd to insert the text at the cursor, but you get
the idea.

Bazinga

unread,
Sep 27, 2010, 11:26:10 AM9/27/10
to Chromium-extensions
Mind sharing your extension's link?

Jesselnz

unread,
Sep 27, 2010, 11:26:36 PM9/27/10
to Chromium-extensions

Bazinga

unread,
Sep 28, 2010, 11:17:06 AM9/28/10
to Chromium-extensions
Nope, the code you're using isn't working. In the background file, if
you put in the following code.

<script>
chrome.contextMenus.create({"title": "Alert", "contexts":
["editable"], "onclick": al});

function al(){
alert(document.activeElement.tagName);
}
</script>

It doesn't return the tagName that you expect in your extension. It
returns BODY for me.

On Sep 28, 8:26 am, Jesselnz <jesse...@gmail.com> wrote:
> Sure.
>
> https://chrome.google.com/extensions/detail/bihhahlakgpcmadfnekbjmnfk...

PhistucK

unread,
Sep 28, 2010, 11:48:35 AM9/28/10
to Bazinga, Chromium-extensions
You have to send a message to your content script\executed script and call al() there. The background page is not the web page, so, activeElement will not be the activeElement of the web page on which you clicked on the context menu command.

PhistucK



PhistucK

unread,
Sep 28, 2010, 1:17:42 PM9/28/10
to Amaan Cheval, Chromium-extensions
Mmm. Not sure where is the problem.
If you send a zipped unpacked sample extension, I will take a look.
PhistucK



On Tue, Sep 28, 2010 at 18:03, Amaan Cheval <amaan...@gmail.com> wrote:
Errmmm. Yeah, I tried changing that. Added a Content Script, with the function in the Content Script this time. Still not working man. It still pops up with BODY. Doesn't work with ID, or value either. They all say something different, but it certainly isn't working.
Reply all
Reply to author
Forward
0 new messages