feat(webview): populate element content fields
Some checks failed
Test / test (push) Successful in 55s
Security Scan / security (push) Failing after 12m32s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 04:47:46 +00:00
parent e35de439e1
commit b993fc18f3
2 changed files with 73 additions and 0 deletions

View file

@ -679,6 +679,8 @@ func (wv *Webview) getElementInfo(ctx context.Context, nodeID int) (*ElementInfo
} }
} }
innerHTML, innerText := wv.getElementContent(ctx, nodeID)
// Get bounding box // Get bounding box
var box *BoundingBox var box *BoundingBox
if boxResult, err := wv.client.Call(ctx, "DOM.getBoxModel", map[string]any{ if boxResult, err := wv.client.Call(ctx, "DOM.getBoxModel", map[string]any{
@ -704,10 +706,61 @@ func (wv *Webview) getElementInfo(ctx context.Context, nodeID int) (*ElementInfo
NodeID: nodeID, NodeID: nodeID,
TagName: tagName, TagName: tagName,
Attributes: attrs, Attributes: attrs,
InnerHTML: innerHTML,
InnerText: innerText,
BoundingBox: box, BoundingBox: box,
}, nil }, nil
} }
// getElementContent retrieves the element's inner HTML and inner text.
func (wv *Webview) getElementContent(ctx context.Context, nodeID int) (string, string) {
resolveResult, err := wv.client.Call(ctx, "DOM.resolveNode", map[string]any{
"nodeId": nodeID,
})
if err != nil {
return "", ""
}
object, ok := resolveResult["object"].(map[string]any)
if !ok {
return "", ""
}
objectID, ok := object["objectId"].(string)
if !ok || objectID == "" {
return "", ""
}
callResult, err := wv.client.Call(ctx, "Runtime.callFunctionOn", map[string]any{
"objectId": objectID,
"functionDeclaration": "function() { return { innerHTML: this.innerHTML || '', innerText: this.innerText || '' }; }",
"returnByValue": true,
"awaitPromise": true,
})
if err != nil {
return "", ""
}
return parseElementContent(callResult)
}
// parseElementContent extracts inner HTML and inner text from a CDP response.
func parseElementContent(result map[string]any) (string, string) {
resultObj, ok := result["result"].(map[string]any)
if !ok {
return "", ""
}
value, ok := resultObj["value"].(map[string]any)
if !ok {
return "", ""
}
innerHTML, _ := value["innerHTML"].(string)
innerText, _ := value["innerText"].(string)
return innerHTML, innerText
}
// click performs a click on an element. // click performs a click on an element.
func (wv *Webview) click(ctx context.Context, selector string) error { func (wv *Webview) click(ctx context.Context, selector string) error {
// Find element and get its center coordinates // Find element and get its center coordinates

View file

@ -458,6 +458,26 @@ func TestGetString_Good(t *testing.T) {
} }
} }
// TestParseElementContent_Good verifies inner content extraction from CDP output.
func TestParseElementContent_Good(t *testing.T) {
result := map[string]any{
"result": map[string]any{
"value": map[string]any{
"innerHTML": "<span>Hello</span>",
"innerText": "Hello",
},
},
}
innerHTML, innerText := parseElementContent(result)
if innerHTML != "<span>Hello</span>" {
t.Fatalf("parseElementContent innerHTML = %q, want %q", innerHTML, "<span>Hello</span>")
}
if innerText != "Hello" {
t.Fatalf("parseElementContent innerText = %q, want %q", innerText, "Hello")
}
}
// TestWaitAction_Good_ContextCancelled verifies WaitAction respects context cancellation. // TestWaitAction_Good_ContextCancelled verifies WaitAction respects context cancellation.
func TestWaitAction_Good_ContextCancelled(t *testing.T) { func TestWaitAction_Good_ContextCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())