-module(butterbee@commands@script). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/butterbee/commands/script.gleam"). -export([call_function/2]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " ## [Script commands](https://w3c.github.io/webdriver-bidi/#module-script-commands)\n" "\n" " The script commands module contains commands found in the script section of the\n" " webdriver bidi protocol. Butterbee uses these internally to create the high level\n" " API. But you can use these commands directly if you want something specific.\n" ). -file("src/butterbee/commands/script.gleam", 31). ?DOC( " Calls a function on the page with the given arguments.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " let function_params =\n" " call_function.new(target)\n" " |> call_function.with_function(function)\n" " |> call_function.with_arguments(arguments)\n" " ```\n" "\n" " [w3c](https://w3c.github.io/webdriver-bidi/#command-script-callFunction)\n" ). -spec call_function( butterbee@webdriver:web_driver(any()), butterbidi@script@commands@call_function:call_function_parameters() ) -> {ok, butterbidi@script@types@evaluate_result:evaluate_result()} | {error, butterbee@internal@error:butterbee_error()}. call_function(Driver, Params) -> Command = {script_command, call_function}, Request = butterbidi@definition:command_to_json( {command, butterbee@internal@id:from_unix(), Command, [{<<"params"/utf8>>, butterbidi@script@commands@call_function:call_function_parameters_to_json( Params )}]} ), gleam@result:'try'( (butterbee@webdriver:get_socket(Driver)), fun(Socket) -> case butterbee@internal@socket:send_request( Socket, Request, Command ) of {error, Error} -> {error, Error}; {ok, Response} -> case erlang:element(4, Response) of {script_result, Result} -> {ok, erlang:element(2, Result)}; _ -> {error, unexpected_script_result_type} end end end ).