2

I've just started learning to code last week so bear with me. I want to make an extension that does the following.

1)When you right click on a textbox a custom context menu is opened

2)When the context menu item is clicked, the title of that context menu item will be pasted into the text box.

So far I have created all the context menus. I just can't figure out how to copy/paste the title of the context menu item into the text box. I've read about the documents.execcommand but I have no idea how to use it. Thanks.

});

chrome.contextMenus.create({

title:"hi",
onclick:copy,
contexts:["editable"]

});

chrome.contextMenus.create({

title:"bye",
onclick:copy,
contexts:["editable"]

});

function copy(info) {

};
pilky04
  • 21
  • 4
  • Where is that code located? – Xan Dec 15 '15 at 23:52
  • The code is located in a JavaScript file background.js in the root folder of the extension. It's the only code in the file. I'm not sure if I answered your question. – pilky04 Dec 16 '15 at 01:40
  • [This question](http://stackoverflow.com/questions/28055887/is-there-a-flexible-way-to-modify-the-contents-of-an-editable-element/28198957#28198957) can be of value. I'm not slapping a duplicate, but it's very relevant. – Xan Dec 17 '15 at 10:32
  • Doing a simple search on stackoverflow will get you lots of answers to this question. – Steve Campbell Dec 23 '15 at 02:29

1 Answers1

0

There is an api called document.execCommand() like what you mentioned. Its as simple as the code below:

var copyText = document.execCommand('copy');  

Basically it will copy any text selection in the browser. Therefore it is not applicable with what you wanted to achieve.

Lastly, to change the value of any element in a webpage, you must have enough knowledge about content scripts. Basically these are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM).

goblin
  • 1,513
  • 13
  • 13