Search code examples
krl

Open and control new window in KRL/Kynetx


I want to open a new window by clicking on a button (which is injected via Kynetx), but I want this new window to run in the Kynetx sandbox enviornment. This is because the new window will have a button which talks to a REST API, and I want to avoid browser same origin policy. I will also want to modify the DOM of this new window.

//code in Kynetx extension
ruleset a2031x3 {
meta {
    name "Open a new window (SO 12030281)"
    description << >>
    author "Steve Nay"
    logging off
}

dispatch { 

    domain "exampley.com"

    }

global { }

rule first_rule {
    select when pageview ".*" setting ()
    emit <|
        // Open a new window and write some content
        var newContent = 'some content';
        newWin = window.open();
        newWin.document.write(newContent);
    |>;        
 }
}

Please help.


Solution

  • You need to wrap that JavaScript code in an emit block, like this:

    ruleset a000x0 {
        meta {
            name "Open a new window (SO 12030281)"
            description << >>
            author "Steve Nay"
            logging off
        }
    
        dispatch { }
    
        global { }
    
        rule first_rule {
            select when pageview ".*" setting ()
            emit <|
                // Open a new window and write some content
                var newContent = 'some content';
                newWin = window.open();
                newWin.document.write(newContent);
            |>;        
        }
    }
    

    That will open a popup window like you want, and the write() call succeeds:

    Popup window, "some content"