@ -39,15 +39,14 @@ function handleMessage(ev) {
const sendOk = ( ) => sendReply ( { action : "cmdOk" } ) ;
const sendError = ( message , stack ) => sendReply ( { action : "cmdError" , message , stack } )
parent . postMessage ( { action : "test" } , ev . origin ) ;
if ( action == "eval" ) {
let { script } = ev . data . args ;
try {
eval ( script ) ;
sendOk ( ) ;
} catch ( e ) {
sendError ( e . message , e . stack )
sendError ( e . message , e . stack ) ;
}
}
@ -56,54 +55,71 @@ function handleMessage(ev) {
if ( ! window . component ) {
// TODO can this happen?
console . error ( ` no component to bind to ` ) ;
console . warn ( 'no component to bind to' ) ;
sendOk ( ) ;
return ;
}
props . forEach ( prop => {
// TODO should there be a public API for binding?
// e.g. `component.$watch(prop, handler)`?
// (answer: probably)
window . component . $$ . bound [ prop ] = value => {
sendMessage ( { action : "prop_update" , args : { prop , value } } )
} ;
} ) ;
try {
props . forEach ( prop => {
// TODO should there be a public API for binding?
// e.g. `component.$watch(prop, handler)`?
// (answer: probably)
window . component . $$ . bound [ prop ] = value => {
sendMessage ( { action : "prop_update" , args : { prop , value } } )
} ;
} ) ;
sendOk ( ) ;
} catch ( e ) {
sendError ( e . message , e . stack ) ;
}
}
if ( action == "set_prop" ) {
if ( ! window . component ) {
return ;
try {
if ( ! window . component ) {
return ;
}
let { prop , value } = ev . data . args ;
component [ prop ] = value ;
sendOk ( ) ;
} catch ( e ) {
sendError ( e . message , e . stack ) ;
}
let { prop , value } = ev . data . args ;
component [ prop ] = value ;
}
if ( action == "catch_clicks" ) {
let topOrigin = ev . origin ;
document . body . addEventListener ( 'click' , event => {
if ( event . which !== 1 ) return ;
if ( event . metaKey || event . ctrlKey || event . shiftKey ) return ;
if ( event . defaultPrevented ) return ;
// ensure target is a link
let el = event . target ;
while ( el && el . nodeName !== 'A' ) el = el . parentNode ;
if ( ! el || el . nodeName !== 'A' ) return ;
if ( el . hasAttribute ( 'download' ) || el . getAttribute ( 'rel' ) === 'external' || el . target ) return ;
event . preventDefault ( ) ;
if ( el . href . startsWith ( topOrigin ) ) {
const url = new URL ( el . href ) ;
if ( url . hash [ 0 ] === '#' ) {
window . location . hash = url . hash ;
return ;
try {
let topOrigin = ev . origin ;
document . body . addEventListener ( 'click' , event => {
if ( event . which !== 1 ) return ;
if ( event . metaKey || event . ctrlKey || event . shiftKey ) return ;
if ( event . defaultPrevented ) return ;
// ensure target is a link
let el = event . target ;
while ( el && el . nodeName !== 'A' ) el = el . parentNode ;
if ( ! el || el . nodeName !== 'A' ) return ;
if ( el . hasAttribute ( 'download' ) || el . getAttribute ( 'rel' ) === 'external' || el . target ) return ;
event . preventDefault ( ) ;
if ( el . href . startsWith ( topOrigin ) ) {
const url = new URL ( el . href ) ;
if ( url . hash [ 0 ] === '#' ) {
window . location . hash = url . hash ;
return ;
}
}
}
window . open ( el . href , '_blank' ) ;
} ) ;
window . open ( el . href , '_blank' ) ;
} ) ;
sendOk ( ) ;
} catch ( e ) {
sendError ( e . message , e . stack ) ;
}
}