viewof embed_code = {
var Observable = await script_to_data_uri(
"https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js"
);
var notebook = await script_to_data_uri(notebook_url);
const embed = `async function ${func_name}(target, injections) {
const Observable = await import("${Observable}");
const notebook = await import("${notebook}");
return new Promise((yes,no) => {
const main = (new Observable.Runtime).module(notebook.default, name => {
if (name === "${required_cell}") {
return new Observable.Inspector(target);
}else{
return ${JSON.stringify(
side_effect_cells
)}.includes(name) || null;
}
});
for (const name in injections) {
main.redefine(name, [], () => injections[name]);
}
yes(main);
})
}`;
const doc =
"data:text/html;charset=utf-8;base64," +
btoa(
unescape(
encodeURIComponent(`<!DOCTYPE html><body>
<div id="test"></div>
<script>
${embed}
${func_name}(document.getElementById("test"),{width:${width},height:320,color:'firebrick'}).then(later=>{
/*if you want to redefine stuff later...*/
later.redefine('color','#FF5859')
})
</script>`)
)
);
var dom = html`<iframe style="width:100%;height:350px;border:none;" sandbox="allow-scripts" src="${doc}">`;
dom.value = embed;
return dom;
}