@@ -34,6 +34,8 @@ const Methods = enum {
3434 enable ,
3535 getDocument ,
3636 performSearch ,
37+ getSearchResults ,
38+ discardSearchResults ,
3739};
3840
3941pub fn dom (
@@ -49,6 +51,8 @@ pub fn dom(
4951 .enable = > enable (alloc , msg , ctx ),
5052 .getDocument = > getDocument (alloc , msg , ctx ),
5153 .performSearch = > performSearch (alloc , msg , ctx ),
54+ .getSearchResults = > getSearchResults (alloc , msg , ctx ),
55+ .discardSearchResults = > discardSearchResults (alloc , msg , ctx ),
5256 };
5357}
5458
@@ -238,3 +242,75 @@ fn performSearch(
238242
239243 return result (alloc , input .id , Resp , resp , input .sessionId );
240244}
245+
246+ // https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-discardSearchResults
247+ fn discardSearchResults (
248+ alloc : std.mem.Allocator ,
249+ msg : * IncomingMessage ,
250+ ctx : * Ctx ,
251+ ) ! []const u8 {
252+ // input
253+ const Params = struct {
254+ searchId : []const u8 ,
255+ };
256+ const input = try Input (Params ).get (alloc , msg );
257+ defer input .deinit ();
258+ std .debug .assert (input .sessionId != null );
259+ log .debug ("Req > id {d}, method {s}" , .{ input .id , "DOM.discardSearchResults" });
260+
261+ // retrieve the search from context
262+ for (ctx .state .nodesearchlist .items , 0.. ) | * s , i | {
263+ if (! std .mem .eql (u8 , s .name , input .params .searchId )) continue ;
264+
265+ s .deinit ();
266+ _ = ctx .state .nodesearchlist .swapRemove (i );
267+ break ;
268+ }
269+
270+ return result (alloc , input .id , null , null , input .sessionId );
271+ }
272+
273+ // https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getSearchResults
274+ fn getSearchResults (
275+ alloc : std.mem.Allocator ,
276+ msg : * IncomingMessage ,
277+ ctx : * Ctx ,
278+ ) ! []const u8 {
279+ // input
280+ const Params = struct {
281+ searchId : []const u8 ,
282+ fromIndex : u32 ,
283+ toIndex : u32 ,
284+ };
285+ const input = try Input (Params ).get (alloc , msg );
286+ defer input .deinit ();
287+ std .debug .assert (input .sessionId != null );
288+ log .debug ("Req > id {d}, method {s}" , .{ input .id , "DOM.getSearchResults" });
289+
290+ if (input .params .fromIndex >= input .params .toIndex ) return error .BadIndices ;
291+
292+ // retrieve the search from context
293+ var ns : ? * const NodeSearch = undefined ;
294+ for (ctx .state .nodesearchlist .items ) | s | {
295+ if (! std .mem .eql (u8 , s .name , input .params .searchId )) continue ;
296+
297+ ns = & s ;
298+ break ;
299+ }
300+
301+ if (ns == null ) return error .searchResultNotFound ;
302+ const items = ns .? .coll .items ;
303+
304+ if (input .params .fromIndex >= items .len ) return error .BadFromIndex ;
305+ if (input .params .toIndex > items .len ) return error .BadToIndex ;
306+
307+ // output
308+ const Resp = struct {
309+ nodeIds : []NodeId ,
310+ };
311+ const resp : Resp = .{
312+ .nodeIds = ns .? .coll .items [input .params .fromIndex .. input .params .toIndex ],
313+ };
314+
315+ return result (alloc , input .id , Resp , resp , input .sessionId );
316+ }
0 commit comments