1

I'm used to updating input, textarea etc. elements with text content from my Chrome Extension.

But adding/updating text to a draft.js text editor is a new beast.

Since a content script exists ON the page, it can use whatever frameworks are already in use - right? So if I know a specific URL I'm interacting with uses jQuery, my Extension can use jQuery. Otherwise I need to use plain vanilla Javascript.

So if I know I'm on a react/draft.js page can I use the draft.js APIs to update the contents of the editor (https://draftjs.org/docs/quickstart-api-basics.html)? How would I access/initiate the editor? Or even know what it's ID is?

Any help/direction appreciated. I just want to append text programmatically to the text editor and update (event handler) React/Draft.js so it understands that the contents of the editor have changed.

11teenth
  • 1,853
  • 1
  • 15
  • 28
  • I'm not familiar with Draft.js, but it's not the case that a contents script "can use whatever frameworks are already in use" on the host page. While a content script [shares the DOM of the page that hosts it](https://developer.chrome.com/extensions/content_scripts#host-page-communication), its JavaScript environment [is separate](https://developer.chrome.com/extensions/content_scripts#isolated_world) from that of the host page. See https://stackoverflow.com/a/9517879 for approaches to injecting JS onto the host page. – jgaul Aug 11 '18 at 23:10
  • Many thanks for taking a look...and for correcting my mental model. Having the right mental model is half the battle. I'm not sure where I picked up the "you can use jQuery if the host page uses jQuery" concept... Draft.js is for React, and you can build Chrome Extensions with React, so I'm optimistic that there will be a way for me to get React running and access the existing (on the host page) draft.js via its APIs. – 11teenth Aug 11 '18 at 23:17
  • You're very welcome. Good luck! – jgaul Aug 12 '18 at 00:05
  • 1
    Possible duplicate of [Could you inject a script into a React page (with Draft.js editor) to append HTML to the existing contents of the editor?](https://stackoverflow.com/questions/51845654/could-you-inject-a-script-into-a-react-page-with-draft-js-editor-to-append-htm) – mzedeler Sep 12 '18 at 19:27

0 Answers0