From 3a34888a72345a90f17659994977ada30c6c2387 Mon Sep 17 00:00:00 2001 From: typeling1578 Date: Fri, 22 Nov 2024 02:30:38 +0900 Subject: [PATCH] Improve post to lens processing --- background.mjs | 8 +++++--- content_script.js | 32 -------------------------------- post_to_lens.html | 21 +++++++++++++++++++++ post_to_lens.mjs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 post_to_lens.html create mode 100644 post_to_lens.mjs diff --git a/background.mjs b/background.mjs index 52c4420..355b4f2 100644 --- a/background.mjs +++ b/background.mjs @@ -120,9 +120,11 @@ async function search_on_google_lens(image_url, tab) { reader.readAsDataURL(image_data_processed); }); - browser.tabs.sendMessage(tab.id, { - type: "open-new-tab", - data_url: image_data_processed_dataurl, + browser.tabs.create({ + url: `${browser.runtime.getURL("/post_to_lens.html")}?image_data_url=${encodeURIComponent(image_data_processed_dataurl)}`, + windowId: tab.windowId ?? undefined, + openerTabId: tab.id ?? undefined, + active: !settings.get("local", "newTabsLoadInBackground"), }); browser.tabs.sendMessage(tab.id, { type: "google-post-end" }); diff --git a/content_script.js b/content_script.js index b7cbc3b..86d3334 100644 --- a/content_script.js +++ b/content_script.js @@ -39,38 +39,6 @@ browser.runtime.onMessage.addListener(async function (message) { elem.remove(); alert(browser.i18n.getMessage("sendingImageError")); break; - case "open-new-tab": - const container = document.createElement("div"); - container.style.display = "none"; - document.body.appendChild(container); - - const form = document.createElement("form"); - form.action = `https://lens.google.com/v3/upload?ep=ccm&s=&st=${Date.now()}`; - form.method = "POST"; - form.enctype = "multipart/form-data"; - form.target = "_blank"; - container.appendChild(form); - - const file_input = document.createElement("input"); - file_input.type = "file"; - file_input.name = "encoded_image"; - form.appendChild(file_input); - - const pid_input = document.createElement("input"); - pid_input.type = "text"; - pid_input.name = "processed_image_dimensions"; - pid_input.value = "1000,1000"; - - const result = await fetch(message.data_url); - const file = new Blob([await result.arrayBuffer()]); - const data_transfer = new DataTransfer(); - const file_obj = new File([file], "image.jpg", { type: "image/jpeg" }); - data_transfer.items.add(file_obj); - file_input.files = data_transfer.files; - - form.submit(); - - container.remove(); default: break; } diff --git a/post_to_lens.html b/post_to_lens.html new file mode 100644 index 0000000..43e9d45 --- /dev/null +++ b/post_to_lens.html @@ -0,0 +1,21 @@ + + + + + + + +
+
+ +
+
+
+ + + + diff --git a/post_to_lens.mjs b/post_to_lens.mjs new file mode 100644 index 0000000..72619a2 --- /dev/null +++ b/post_to_lens.mjs @@ -0,0 +1,44 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +(async () => { + const params = new URLSearchParams(window.location.search); + const image_data_url = decodeURIComponent(params.get("image_data_url") ?? ""); + if (!image_data_url) { + return; + } + + const container = document.createElement("div"); + container.style.display = "none"; + document.body.appendChild(container); + + const form = document.createElement("form"); + form.action = `https://lens.google.com/v3/upload?ep=ccm&s=&st=${Date.now()}`; + form.method = "POST"; + form.enctype = "multipart/form-data"; + // form.target = "_blank"; + container.appendChild(form); + + const file_input = document.createElement("input"); + file_input.type = "file"; + file_input.name = "encoded_image"; + form.appendChild(file_input); + + const pid_input = document.createElement("input"); + pid_input.type = "text"; + pid_input.name = "processed_image_dimensions"; + pid_input.value = "1000,1000"; + + const result = await fetch(image_data_url); + const file = new Blob([await result.arrayBuffer()]); + const data_transfer = new DataTransfer(); + const file_obj = new File([file], "image.jpg", { type: "image/jpeg" }); + data_transfer.items.add(file_obj); + file_input.files = data_transfer.files; + + form.submit(); + + container.remove(); +})(); \ No newline at end of file