Public
Edited
Jun 9, 2024
Paused
Comments locked
1 star
Insert cell
Insert cell
Insert cell
trap()
Insert cell
trap("Error: ")
Insert cell
trap("TypeError: ")
Insert cell
trap("SyntaxError: ")
Insert cell
trap("ReferenceError: ")
Insert cell
trap("RuntimeError: ")
Insert cell
viewof qbo = (odp, Inputs.bind(Inputs.input(), viewof odp))
Insert cell
viewof odp = {
const attribute = "data-odp";

const hooks = Array(3)
.fill("observablehq")
.map((x, i) => x + ["-root", "", "--error"][i]);

const poll = (function () {
let count = 0;

const callback = (records) => {
for (const record of records) {
if (
record.type === "attributes" &&
record.oldValue.indexOf(hooks[2]) !== -1
) {
stash(record.target, { action: "pop" });
}
}
};

const config = {
attributes: true,
attributeFilter: ["class"],
attributeOldValue: true
};

const observer = new MutationObserver(callback);

return (node) => {
observer.observe(node, config);
node.addEventListener("error", snitch);
node.setAttribute(attribute, ++count);
if (node.classList.contains(hooks[2])) {
stash(node);
}
};
})();

const root = Array.from(document.getElementsByClassName(hooks[0])).pop();

const register = Inputs.input({});

const snitch = (event) => stash(event.target);

const stash = (
node,
{ action = "push", key = node.getAttribute(attribute) } = {}
) => {
if (action === "pop") {
delete register.value[key];
} else {
Object.assign(register.value, {
[key]: node.textContent
});
}
register.dispatchEvent(new Event("input", { bubbles: true }));
};

if (root) {
Array.from(
/* existing cells */ root.getElementsByClassName(hooks[1])
).forEach(poll);

const observer = new MutationObserver((records) => {
for (const record of records) {
Array.from(/* new cells */ record.addedNodes).forEach(poll);
}
});
observer.observe(root, {
childList: true
});
}

/* thank you, Tom Larkworthy! - mg */

return register;
}
Insert cell
Insert cell
val = foo
Insert cell
null.valueOf()
Insert cell
[0x33, 0x66, 0#99, 0xcc, 0xff]
Insert cell
1 +
Insert cell
((x, y) => (1, eval)("x + y")).apply(null, [2, 2])
Insert cell
foo
Insert cell
bar
Insert cell
{
throw Error("#" + Math.random().toString(16).slice(2));
}
Insert cell
Insert cell
trap = (needle = "", { register = Object.values(qbo) } = {}) =>
Object.freeze(
needle.length && needle.match(/^\w+: /)
? Object.defineProperty(
[
...new Set(
register
.filter((item) => item.match(new RegExp("\\b" + needle)))
.map((item) => item.slice(item.indexOf(needle) + needle.length))
)
],
"instanceof",
{ value: needle.replace(/^(\w+).*$/, "$1") }
)
: register
)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more