Skip to content

The headless browser for AI agents and web scraping

fix(js): report unsupported editing commands truthfully

SGavrl373bc8b158c9
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 2d47383f..a1b7d763 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -5687,7 +5687,12 @@ function _throwDocumentDomainSecurityError() {
throw new DOMException("Failed to set the 'domain' property on 'Document'", "SecurityError");
}
+const _documentInstances = new WeakSet();
class Document extends Node {
+ constructor(nid) {
+ super(nid);
+ _documentInstances.add(this);
+ }
get timeline() {
if (!this._timeline) {
this._timeline = new DocumentTimeline();
@@ -6369,6 +6374,16 @@ class Document extends Node {
setTimeout(finishParsing, 0);
}
hasFocus() { return true; }
+ queryCommandSupported(commandId) {
+ const method = _documentRealmMember(this, 'queryCommandSupported');
+ if (method) return Reflect.apply(method, this, arguments);
+ if (!_documentInstances.has(this)) throw new TypeError('Illegal invocation');
+ if (arguments.length < 1) throw new TypeError('queryCommandSupported requires 1 argument');
+ commandId = `${commandId}`;
+ // execCommand currently implements no editing commands. Feature detection
+ // must not advertise clipboard or formatting actions that do nothing.
+ return false;
+ }
execCommand() { return false; }
}
@@ -11270,6 +11285,7 @@ globalThis.DOMParser = class DOMParser {
_root: root,
nodeName: "#document",
nodeType: 9,
+ queryCommandSupported: Document.prototype.queryCommandSupported,
contentType: isXml ? (mimeType || "application/xml") : "text/html",
get documentElement() {
// For XML parsererror docs, return the <parsererror> child, not the
@@ -11371,6 +11387,7 @@ globalThis.DOMParser = class DOMParser {
contains(n) { return root.contains ? root.contains(n) : false; },
addEventListener() {}, removeEventListener() {}, dispatchEvent() { return true; },
};
+ _documentInstances.add(docNode);
return docNode;
}
};
@@ -16754,7 +16771,7 @@ if (typeof ShadowRoot !== 'undefined' && !ShadowRoot.prototype.elementFromPoint)
// Capture late-defined document members too, before page code can replace them.
const _documentMembers = Object.freeze(Object.fromEntries(
- ['URL', 'defaultView', 'readyState', 'getElementById', 'querySelector', 'querySelectorAll', 'open', 'close', 'elementFromPoint'].map(name => {
+ ['URL', 'defaultView', 'readyState', 'getElementById', 'querySelector', 'querySelectorAll', 'open', 'close', 'elementFromPoint', 'queryCommandSupported'].map(name => {
const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, name);
return [name, descriptor.value || descriptor.get];
})));
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 63b62b18..c6a378c5 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -7722,6 +7722,45 @@ mod tests {
assert_eq!(result, serde_json::json!([1, 0, true]));
}
+ #[test]
+ fn editing_command_detection_reports_unsupported_commands_without_mutation() {
+ let mut rt = setup_runtime("<body><p>unchanged</p><iframe></iframe></body>");
+ assert_eq!(rt.evaluate(r#"(() => {
+ const docs = [document, document.implementation.createHTMLDocument('detached'),
+ new DOMParser().parseFromString('<p>parsed</p>', 'text/html')];
+ const commands = ['copy', 'cut', 'paste', 'bold', 'obscura-unknown-command'];
+ const support = docs.map(doc => commands.map(command => doc.queryCommandSupported(command)));
+ const child = document.querySelector('iframe').contentDocument;
+ const borrowed = Document.prototype.queryCommandSupported;
+ Object.setPrototypeOf(child, Document.prototype);
+ Object.defineProperty(child, 'nodeType', { get() { throw new Error('author getter'); } });
+ return [support, borrowed.call(child, 'copy'), document.execCommand('copy'),
+ document.querySelector('p').textContent];
+ })()"#).unwrap(), serde_json::json!([
+ [[false, false, false, false, false], [false, false, false, false, false],
+ [false, false, false, false, false]], false, false, "unchanged"
+ ]));
+ }
+
+ #[test]
+ fn editing_command_detection_checks_receiver_and_converts_domstring() {
+ let mut rt = setup_runtime("<body></body>");
+ assert_eq!(rt.evaluate(r#"(() => {
+ const hints = [];
+ const command = { [Symbol.toPrimitive](hint) { hints.push(hint); return 'unknown'; } };
+ const supported = document.queryCommandSupported(command);
+ const errorName = action => { try { action(); return null; } catch (e) { return e.name; } };
+ const errors = [errorName(() => document.queryCommandSupported()),
+ errorName(() => document.queryCommandSupported(Symbol('command'))),
+ errorName(() => Document.prototype.queryCommandSupported.call({}, command)),
+ errorName(() => Document.prototype.queryCommandSupported.call(Object.create(Document.prototype), command)),
+ errorName(() => document.queryCommandSupported({ toString() { throw new RangeError(); } }))];
+ return [supported, hints, errors];
+ })()"#).unwrap(), serde_json::json!([
+ false, ["string"], ["TypeError", "TypeError", "TypeError", "TypeError", "RangeError"]
+ ]));
+ }
+
#[test]
fn test_document_title() {
let mut rt = setup_runtime("<html><head><title>Test</title></head><body></body></html>");
diff --git a/docs/Use-with-Playwright.md b/docs/Use-with-Playwright.md
index b482ebf4..63252375 100644
--- a/docs/Use-with-Playwright.md
+++ b/docs/Use-with-Playwright.md
@@ -196,6 +196,10 @@ await browser.close(); // closes the CDP connection, leaves obscura serve runni
## Current limits
+- `document.queryCommandSupported()` reports editing commands as unsupported.
+ `execCommand()` does not perform clipboard or rich-text editing operations.
+ Capability detection allows editor libraries to initialize; it is not full
+ editor interaction support.
- Playwright `page.video()` and tracing artifacts that require desktop capture
are not implemented. Use the raw CDP flow above for page frames.
- `BrowserContext` storage-state save/restore remains limited; use