1

I am making Google Chrome Extension which gets the text from ACE Editor. But as soon as I create ACE object, formatting is lost and I am not able to get the Java code with indentation. The result also contains many unwanted characters.

I have used following code.

var editor = ace.edit('editor');
var code = editor.getValue();

Before my code is executed

1

After my code is executed

2

But when I run same code from developer console, it works fine.

Please suggest what is wrong or any other way to get full code with formatting using DOM element?

Vineet Palan
  • 341
  • 2
  • 9
  • do you call edit on an editor created by the page? maybe extension doesn't have access to objects created on the page, so when you call edit old editor is destroyed and new one is created – a user Jan 09 '17 at 08:15
  • @auser : That could be the case, but that's the only way I know to get the Ace Editor's object. – Vineet Palan Jan 09 '17 at 13:08

1 Answers1

0

This happens because code in chrome extension doesn't have access to the code running in the page. When you call ace.edit on existing editor env property of the element and creates a new editor instead. As a workaround you can create a content script which will run in the page context, and communicate with the rest of extensions using events or postMessage. See https://stackoverflow.com/a/13292994/1743328 for more details

Community
  • 1
  • 1
a user
  • 23,300
  • 6
  • 58
  • 90
  • 1
    I upvoted the answer but I don't have 15 reputations, so I was not able to do it. But now I marked it as Solution. Thanks. – Vineet Palan Jan 10 '17 at 21:04