Search code examples
javascriptgoogle-chromeckeditoruserscripts

CKEditor variable is available in console, but not from a Chrome userscript?


I'm writing a Chrome userscript to locally auto-save content in a CKEditor. I'm using this CKEditor auto-save plugin as inspiration.

I have written a function that fires every half second (via an interval) to register the CKEditor event handler:

var intervalId = window.setInterval(function() {
    if (CKEDITOR) {
        window.clearInterval(intervalId);
        CKEDITOR.plugins.add("user-script-auto-save", {
            init : function(editor) {
                editor.on('key', startTimer);
            }
        });
    }
}, 500);

However, it never properly completes, and complains that "CKEDITOR is undefined" on the if (CKEDITOR) statement.

Meanwhile, if I drop into Chrome's console and type CKEDITOR, the console prints out the expected object.

What am I missing? The editor is embedded within an iframe; might that have an impact on scoping? Or am I fighting against Chrome's sandboxing here? And if so, is there some other way I can dig into CKEditor to pull out the content every second or something to do the auto-saves?

I have not yet tried the script in Firefox; that's next on my list.

Worth noting: I'm a long-time JavaScript novice. So I could easily be doing something dumb with scoping or something like that.


Solution

  • According to this little tutorial video on YouTube, all the 3 "devices" are separated from each other in order to prevent XSS attacks from the user script to the browser / website and vice versa. Although the user scripts / content scripts are running in the website's context, they are still kept separated from the actual website script context. You can easily acknowledge this by simply trying to access for example jQuery from a content script. Just as the CKEditor, it will not be available.

    So what I've come up with in order to deal with this is using the content script to include external JavaScripts in the head tag. AFAIK, this is not possible for files directly in the extension's root directory, so I've taken a remote server to host my files.

    I'm not sure if this is the best approach and I think it is an ugly bypass, possibly way to powerfull and disabled by the Chromium Project some time.


    (Edited by OP, so I can select this answer and route karma appropriately)

    This answer, combined with some of the suggestions and links in the comments, ended up getting me to where I needed to be.

    I ended up with the following function:

    var insertScriptIntoDocument = function(scriptUrl, doc) {
        // inspired by http://blog.afterthedeadline.com/2010/05/14/how-to-jump-through-hoops-and-make-a-chrome-extension/
        var scriptText = doc.createTextNode(
            '(function(loc) {                                                    \
        var embeddedScript = document.createElement("script");                   \
        embeddedScript.type = "text/javascript";                                 \
        embeddedScript.src = loc;                                                \
        document.getElementsByTagName("head")[0].appendChild(embeddedScript);    \
    })("' + scriptUrl + '");');
    
        var injectorElement = doc.createElement('script');
        injectorElement.appendChild(scriptText);
        doc.body.appendChild(injectorElement);
    };
    

    Usage looks like so:

    var embeddedScriptUrl = chrome.extension.getURL("embedded-script.js");
    insertScriptIntoDocument(embeddedScriptUrl, document);

    For now, I'm executing this from within a Chrome extension, but I suspect that the pattern might work in a GreaseMonkey script deployed via the Chrome TamperMonkey extension provided that the URL of the script to be embedded was hosted somewhere reachable.

    FTR, as it turns out, I did not actually need to get to the iframe -- the CKEDITOR variable was defined in the top-level document, but was simply not visible because of the rules of the Chrome sandbox