Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify contents from browser extension #11974

Closed
octavianSandu opened this issue Jun 27, 2022 · 6 comments
Closed

Modify contents from browser extension #11974

octavianSandu opened this issue Jun 27, 2022 · 6 comments
Labels
resolution:invalid This issue is invalid (e.g. reports a non-existent bug or a by-design behavior). type:feature This issue reports a feature request (an idea for a new functionality or a missing option).

Comments

@octavianSandu
Copy link

octavianSandu commented Jun 27, 2022

馃摑 I need to be able to change the text contents of a ckEditor 5 instance via a browser extension

I know this is possible as it is done by Grammarly, but I have no idea how.
I guess that this is more of providing some documentation link and not adding new functionality.

For the demo site ( https://ckeditor.com/ckeditor-5/demo/ ) all works as I am using the ckEditor API that is available in the window.editors array. But, in real life, the API is not directly available.

So, is there any generic way to get access to the ckEditor API ?
Or how is it safe to make changes to the view without being blocked by the model?

Mostly, the API is used for making a selection (with the text to be removed), inserting the content, moving the cursor, refocusing the editor when focus is lost.

@octavianSandu octavianSandu added the type:feature This issue reports a feature request (an idea for a new functionality or a missing option). label Jun 27, 2022
@jswiderski jswiderski added the resolution:invalid This issue is invalid (e.g. reports a non-existent bug or a by-design behavior). label Jun 28, 2022
@jswiderski
Copy link

Or how is it safe to make changes to the view without being blocked by the model?

Any changes done to HTML or selection in CKEditor 5 have to be done through model and there is no way around that. When you create editor, you are most likely assigning it to window object.

 ...
 } )
    .then( editor => {
			console.log( 'Editor was initialized', editor );
			window.editor = editor;	
...

Having that you can use a lot (not all) of API in the browser console e.g.

// move selection to the start of editor.
editor.focus();
editor.model.change( (writer) => {
	const position = writer.createPositionFromPath( editor.model.document.getRoot(), [0,0] );
	const selection = writer.createSelection( position );
	writer.setSelection( selection.focus );
});

@octavianSandu
Copy link
Author

octavianSandu commented Jun 28, 2022

Thank you @jswiderski for your answer.

In the ckEditor demo, where the editor property is exposed (in window.editors array), I am, indeed, making the changes through the ckEditor model. It works flawlessly.

On the other hand, I am writing a browser extension for various clients.
They have no editor property exposed (and I looped through all the window properties hoping I would find it). But the Grammarly extension works. And it does play with the caret and it changes content, and it doesn't trigger ckEditor Errors.
So there must be a way to either get the API via the contenteditable htmlElement, or to make the model accept external manipulation.

There was a long discussion about it, but how it was eventually approached was not mentioned.

@octavianSandu
Copy link
Author

Ok, so, for anyone interested in this, it is answered on FAQ

@cosimo
Copy link

cosimo commented Apr 6, 2023

@octavianSandu I tried that approach described in the FAQ, but the ckeditorInstance attribute is undefined when the browser extension code is running. When I do the exact same thing from the js console, it works. Any idea why? Could you share your browser extension manifest and/or code? Thanks.

@tavi100
Copy link

tavi100 commented Apr 6, 2023

You need to run the code in the main world. See Chrome documentation.

@cosimo
Copy link

cosimo commented Apr 6, 2023

@tavi100 thanks, I didn't understand what that meant initially, but I got it to work. The only small problem I found is that Firefox does not support world: "MAIN", unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution:invalid This issue is invalid (e.g. reports a non-existent bug or a by-design behavior). type:feature This issue reports a feature request (an idea for a new functionality or a missing option).
Projects
None yet
Development

No branches or pull requests

4 participants