示例#1
0
        private async void ScriptCallback(IntPtr manager, IntPtr jsResult, IntPtr userdata)
        {
            if (EnableScriptInterface)
            {
                IntPtr value = WebKit.JavaScript.GetValue(jsResult);
                if (WebKit.JavaScript.IsValueString(value))
                {
                    IntPtr bytes = IntPtr.Zero;
                    try
                    {
                        bytes = WebKit.JavaScript.GetStringBytes(value);
                        IntPtr bytesPtr = GLib.GetBytesDataPointer(bytes, out UIntPtr length);

                        string result;
                        unsafe { result = Encoding.UTF8.GetString((byte *)bytesPtr, (int)length); }

                        await bridge.HandleScriptCall(result);
                    }
                    finally { if (bytes != IntPtr.Zero)
                              {
                                  GLib.UnrefBytes(bytes);
                              }
                    }
                }
            }
        }
示例#2
0
        public Task <string> ExecuteScriptAsync(string script)
        {
            var taskResult = new TaskCompletionSource <string>();

            unsafe void Callback(IntPtr webview, IntPtr asyncResult, IntPtr userdata)
            {
                IntPtr jsResult = IntPtr.Zero;

                try
                {
                    jsResult = WebKit.JavaScript.EndExecute(webview, asyncResult, out IntPtr errorPtr);
                    if (jsResult != IntPtr.Zero)
                    {
                        IntPtr value = WebKit.JavaScript.GetValue(jsResult);
                        if (WebKit.JavaScript.IsValueString(value))
                        {
                            IntPtr bytes    = WebKit.JavaScript.GetStringBytes(value);
                            IntPtr bytesPtr = GLib.GetBytesDataPointer(bytes, out UIntPtr length);

                            string result = Encoding.UTF8.GetString((byte *)bytesPtr, (int)length);
                            taskResult.TrySetResult(result);

                            GLib.UnrefBytes(bytes);
                        }
                        else
                        {
                            taskResult.TrySetResult(null);
                        }
                    }
                    else
                    {
                        try
                        {
                            var    error        = Marshal.PtrToStructure <GError>(errorPtr);
                            string errorMessage = GLibString.FromPointer(error.Message);
                            taskResult.TrySetException(new Exception($"Script execution failed with: \"{errorMessage}\""));
                        }
                        catch (Exception ex) { taskResult.TrySetException(ex); }
                        finally { GLib.FreeError(errorPtr); }
                    }
                }
                catch (Exception ex) { taskResult.TrySetException(ex); }
                finally
                {
                    if (jsResult != IntPtr.Zero)
                    {
                        WebKit.JavaScript.ReleaseJsResult(jsResult);
                    }
                }
            }

            using (GLibString gfunction = script)
            {
                WebKit.JavaScript.BeginExecute(Handle, gfunction, IntPtr.Zero, Callback, IntPtr.Zero);
            }

            return(taskResult.Task);
        }
示例#3
0
        private unsafe void ScriptExecuteCallback(IntPtr webview, IntPtr asyncResult, IntPtr userdata)
        {
            var handle = GCHandle.FromIntPtr(userdata);
            var state  = (ScriptExecuteState)handle.Target !;

            IntPtr jsResult = IntPtr.Zero;

            try
            {
                jsResult = WebKit.JavaScript.EndExecute(webview, asyncResult, out IntPtr errorPtr);
                if (jsResult != IntPtr.Zero)
                {
                    IntPtr value = WebKit.JavaScript.GetValue(jsResult);
                    if (WebKit.JavaScript.IsValueString(value))
                    {
                        IntPtr bytes    = WebKit.JavaScript.GetStringBytes(value);
                        IntPtr bytesPtr = GLib.GetBytesDataPointer(bytes, out UIntPtr length);

                        string result = Encoding.UTF8.GetString((byte *)bytesPtr, (int)length);
                        state.TaskResult.TrySetResult(result);

                        GLib.UnrefBytes(bytes);
                    }
                    else
                    {
                        state.TaskResult.TrySetResult(null);
                    }
                }
                else
                {
                    try
                    {
                        var    error        = Marshal.PtrToStructure <GError>(errorPtr);
                        string?errorMessage = GLibString.FromPointer(error.Message);
                        state.TaskResult.TrySetException(new ScriptException($"Script execution failed with: \"{errorMessage}\""));
                    }
                    catch (Exception ex) { state.TaskResult.TrySetException(ex); }
                    finally { GLib.FreeError(errorPtr); }
                }
            }
            catch (Exception ex) { state.TaskResult.TrySetException(ex); }
            finally
            {
                handle.Free();

                if (jsResult != IntPtr.Zero)
                {
                    WebKit.JavaScript.ReleaseJsResult(jsResult);
                }
            }
        }
示例#4
0
        private string ExecuteScriptCallback(IntPtr webview, IntPtr asyncResult, IntPtr userdata)
        {
            IntPtr jsResult = IntPtr.Zero;

            try
            {
                jsResult = WebKit.JavaScript.EndExecute(webview, asyncResult, out IntPtr errorPtr);
                if (jsResult != IntPtr.Zero)
                {
                    IntPtr value = WebKit.JavaScript.GetValue(jsResult);
                    if (WebKit.JavaScript.IsValueString(value))
                    {
                        IntPtr bytes    = WebKit.JavaScript.GetStringBytes(value);
                        IntPtr bytesPtr = GLib.GetBytesDataPointer(bytes, out UIntPtr length);

                        unsafe
                        {
                            string result = Encoding.UTF8.GetString((byte *)bytesPtr, (int)length);
                            GLib.UnrefBytes(bytes);
                            return(result);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    try
                    {
                        var    error        = Marshal.PtrToStructure <GError>(errorPtr);
                        string errorMessage = GLibString.FromPointer(error.Message);
                        throw new Exception($"Script execution failed with: \"{errorMessage}\"");
                    }
                    finally { GLib.FreeError(errorPtr); }
                }
            }
            finally
            {
                if (jsResult != IntPtr.Zero)
                {
                    WebKit.JavaScript.ReleaseJsResult(jsResult);
                }
            }
        }