diff --git a/main.js b/main.js index 4054288..2d1bbfa 100644 --- a/main.js +++ b/main.js @@ -121,71 +121,33 @@ function generateAltText() { altText = () => alert(generateAltText()); if ('webkit' in window && 'messageHandlers' in window.webkit) { - let _promiseId = 0; - let _promises = new Map(); - - window.addEventListener('message', (event) => { - const [id] = event.detail; - const [resolve,] = _promises.get(id) ?? []; - if (!resolve) return; - _promises.delete(id); - resolve(); - }); - - window.addEventListener('message', (event) => { - const [id] = event.detail; - const [,reject] = _promises.get(id) ?? []; - if (!reject) return; - _promises.delete(id); - reject(); - }); - - altText = () => new Promise((resolve, reject) => { - const thisPromiseId = ++_promiseId; - - _promises.set(thisPromiseId, [resolve, reject]); - - window.webkit.messageHandlers.altText.postMessage([ - thisPromiseId, - generateAltText(), - ]); - }); - - copyClipboard = () => new Promise((resolve, reject) => { - const thisPromiseId = ++_promiseId; - - _promises.set(thisPromiseId, [resolve, reject]); + altText = () => window.webkit.messageHandlers.altText.postMessage([generateAltText()]); + copyClipboard = () => { const schedule = document.getElementById('schedule'); const boundingRect = schedule.getBoundingClientRect(); window.webkit.messageHandlers.copySnapshot.postMessage([ - thisPromiseId, 'clipboard', boundingRect.x + 1, boundingRect.y + 1, boundingRect.width - 2, boundingRect.height - 2, ]); - }); - - saveImage = () => new Promise((resolve, reject) => { - const thisPromiseId = ++_promiseId; - - _promises.set(thisPromiseId, [resolve, reject]); + }; + saveImage = () => { const schedule = document.getElementById('schedule'); const boundingRect = schedule.getBoundingClientRect(); window.webkit.messageHandlers.copySnapshot.postMessage([ - thisPromiseId, 'file', boundingRect.x + 1, boundingRect.y + 1, boundingRect.width - 2, boundingRect.height - 2, ]); - }); + }; document.getElementById('clipboard').removeAttribute('disabled'); document.getElementById('save-image').removeAttribute('disabled'); diff --git a/native/src/ls-web-view.c b/native/src/ls-web-view.c index bed5332..23ad7ee 100644 --- a/native/src/ls-web-view.c +++ b/native/src/ls-web-view.c @@ -80,7 +80,6 @@ static GdkTexture *ls_web_view_copy_snapshot_to_texture(GtkWidget *widget, struct _LSWebViewCopySnapshotUserData { LSWebView *self; GCancellable *cancellable; - guint callback_id; float xoffset; float yoffset; float width; @@ -92,18 +91,12 @@ static void ls_web_view_copy_snapshot_to_clipboard(void *user_data) { struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data; LSWebView *self = typed_user_data->self; GCancellable *cancellable = typed_user_data->cancellable; - guint callback_id = typed_user_data->callback_id; float xoffset = typed_user_data->xoffset; float yoffset = typed_user_data->yoffset; float width = typed_user_data->width; float height = typed_user_data->height; if (g_cancellable_is_cancelled(cancellable)) { - WebKitUserMessage *message = - webkit_user_message_new("_nativePromiseReject", g_variant_new("(i)", callback_id)); - - webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, cancellable, NULL, NULL); - goto ls_webview_copy_snapshot_to_clipboard_exit; } @@ -112,11 +105,6 @@ static void ls_web_view_copy_snapshot_to_clipboard(void *user_data) { GdkClipboard *clipboard = gdk_display_get_clipboard(gdk_display_get_default()); gdk_clipboard_set_texture(clipboard, texture); - WebKitUserMessage *message = - webkit_user_message_new("_nativePromiseResolve", g_variant_new("(i)", callback_id)); - - webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, NULL, NULL, NULL); - ls_webview_copy_snapshot_to_clipboard_exit: if (self->cancellable == cancellable) { g_object_unref(self->cancellable); @@ -135,15 +123,9 @@ static void ls_web_view_copy_snapshot_to_file_after_save_dialog(GObject *so struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data; LSWebView *self = typed_user_data->self; GCancellable *cancellable = typed_user_data->cancellable; - guint callback_id = typed_user_data->callback_id; GdkTexture *texture = typed_user_data->texture; if (g_cancellable_is_cancelled(cancellable)) { - WebKitUserMessage *message = - webkit_user_message_new("_nativePromiseReject", g_variant_new("(i)", callback_id)); - - webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, cancellable, NULL, NULL); - goto ls_web_view_copy_snapshot_to_file_after_save_dialog_exit; } @@ -158,11 +140,6 @@ static void ls_web_view_copy_snapshot_to_file_after_save_dialog(GObject *so char *path = g_file_get_path(file); gdk_texture_save_to_png(texture, path); - WebKitUserMessage *message = - webkit_user_message_new("_nativePromiseResolve", g_variant_new("(i)", callback_id)); - - webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, NULL, NULL, NULL); - ls_web_view_copy_snapshot_to_file_after_save_dialog_exit: if (self->cancellable == cancellable) { g_object_unref(self->cancellable); @@ -178,18 +155,12 @@ static void ls_web_view_copy_snapshot_to_file(void *user_data) { struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data; LSWebView *self = typed_user_data->self; GCancellable *cancellable = typed_user_data->cancellable; - guint callback_id = typed_user_data->callback_id; float xoffset = typed_user_data->xoffset; float yoffset = typed_user_data->yoffset; float width = typed_user_data->width; float height = typed_user_data->height; if (g_cancellable_is_cancelled(cancellable)) { - WebKitUserMessage *message = - webkit_user_message_new("_nativePromiseReject", g_variant_new("(i)", callback_id)); - - webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, cancellable, NULL, NULL); - if (self->cancellable == cancellable) { g_object_unref(self->cancellable); self->cancellable = NULL; @@ -214,9 +185,7 @@ static void ls_web_view_on_alt_text_message_received(WebKitUserContentManager *m JSCValue *value, gpointer user_data) { - JSCValue *callback_id_val = jsc_value_object_get_property_at_index(value, 0); - JSCValue *alt_text_val = jsc_value_object_get_property_at_index(value, 1); - int callback_id = jsc_value_to_int32(callback_id_val); + JSCValue *alt_text_val = jsc_value_object_get_property_at_index(value, 0); char *alt_text = jsc_value_to_string(alt_text_val); GdkClipboard *clipboard = gdk_display_get_clipboard(gdk_display_get_default()); @@ -224,12 +193,6 @@ static void ls_web_view_on_alt_text_message_received(WebKitUserContentManager *m LSWebView *self = LS_WEB_VIEW(user_data); - WebKitUserMessage *message = - webkit_user_message_new("_nativePromiseResolve", g_variant_new("(i)", callback_id)); - - webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, NULL, NULL, NULL); - - g_object_unref(callback_id_val); g_object_unref(alt_text_val); g_free(alt_text); } @@ -238,13 +201,11 @@ static void ls_web_view_on_copy_snapshot_message_received(WebKitUserContentManag JSCValue *value, gpointer user_data) { - JSCValue *callback_id_val = jsc_value_object_get_property_at_index(value, 0); - JSCValue *kind_val = jsc_value_object_get_property_at_index(value, 1); - JSCValue *xoffset_val = jsc_value_object_get_property_at_index(value, 2); - JSCValue *yoffset_val = jsc_value_object_get_property_at_index(value, 3); - JSCValue *width_val = jsc_value_object_get_property_at_index(value, 4); - JSCValue *height_val = jsc_value_object_get_property_at_index(value, 5); - int callback_id = jsc_value_to_int32(callback_id_val); + JSCValue *kind_val = jsc_value_object_get_property_at_index(value, 0); + JSCValue *xoffset_val = jsc_value_object_get_property_at_index(value, 1); + JSCValue *yoffset_val = jsc_value_object_get_property_at_index(value, 2); + JSCValue *width_val = jsc_value_object_get_property_at_index(value, 3); + JSCValue *height_val = jsc_value_object_get_property_at_index(value, 4); char *kind = jsc_value_to_string(kind_val); float xoffset = (float)jsc_value_to_double(xoffset_val); float yoffset = (float)jsc_value_to_double(yoffset_val); @@ -263,7 +224,6 @@ static void ls_web_view_on_copy_snapshot_message_received(WebKitUserContentManag (struct _LSWebViewCopySnapshotUserData *)g_malloc(sizeof(struct _LSWebViewCopySnapshotUserData)); task_user_data->self = g_object_ref(self); task_user_data->cancellable = g_object_ref(self->cancellable); - task_user_data->callback_id = callback_id; task_user_data->xoffset = xoffset; task_user_data->yoffset = yoffset; task_user_data->width = width; @@ -275,7 +235,6 @@ static void ls_web_view_on_copy_snapshot_message_received(WebKitUserContentManag g_idle_add_once(ls_web_view_copy_snapshot_to_file, task_user_data); } - g_object_unref(callback_id_val); g_object_unref(kind_val); g_object_unref(xoffset_val); g_object_unref(yoffset_val);