I'm attempting to add buttons to a CKEditor using a Chrome extension. Specifically, I'm trying to modify the CKEditor in MailChimp.com's template editor. And to be clear, my issue less about how to do it via a Chrome extension, and more about how to do it after the CKEditor has already been instanced.
When I'm on MailChimp's editor page and I type editor
into the console I can see that it's been used to create the CKeditor on the page. Unfortunately, I'm having trouble using the code in the CKEditor documentation to add buttons after the fact. Some things like editor.destroy()
work, but that doesn't really help me. I'm not interested in destroying MailChimp's toolbar and creating one from scratch, I just want to add to it.
My code is being run in the page context, not as a content script. Here is the code from my content script that I used to enable this:
injectScript( chrome.extension.getURL('/js/mailchimp_injected_template-editor.js'), 'body');
It injects a script file into the body
of the page so that I can access the variables set by MailChimp's code. Here is the code from the injected script. I tried using this code below that I got from this answer: https://stackoverflow.com/a/37432348/556079
CKEDITOR.on('instanceCreated', function(event) {
var editor = event.editor;
editor.config.toolbar = [
{ name: 'basicstyles', groups: [ 'basicstyles'], items: [ 'Bold', 'Italic','Subscript', 'Superscript' ] },
]; // could be from synchronous!!! XHR as well
});
CKEDITOR.on('instanceReady', function(event) {
var editor = event.editor;
editor.config.toolbar = [
{ name: 'basicstyles', groups: [ 'basicstyles'], items: [ 'Bold', 'Italic','Subscript', 'Superscript' ] },
];
});
Both of these functions fire exactly when I expect them too. Sadly, the code within them has no effect on the page. I've swapped it out for code I've found on CKeditors documentation page as well as from other answers in the question I linked above. It all either throws an error or fails to do anything at all.
Here's an example of some other code I tried, which is more relevant than the stuff above. This would should actually do what I want. Create a command and then add a button to the toolbar that fires that command.
editor.addCommand("mySimpleCommand", {
exec: function(edt) {
alert(edt.getData());
}
});
editor.ui.addButton('SuperButton', {
label: "Click me",
command: 'mySimpleCommand',
toolbar: 'insert',
icon: 'https://avatars1.githubusercontent.com/u/5500999?v=2&s=16'
});
Any tips would be greatly appreciated!