Skip to content

Commit 3a34888

Browse files
committed
Improve post to lens processing
1 parent 1dabf0f commit 3a34888

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

Diff for: background.mjs

+5-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ async function search_on_google_lens(image_url, tab) {
120120
reader.readAsDataURL(image_data_processed);
121121
});
122122

123-
browser.tabs.sendMessage(tab.id, {
124-
type: "open-new-tab",
125-
data_url: image_data_processed_dataurl,
123+
browser.tabs.create({
124+
url: `${browser.runtime.getURL("/post_to_lens.html")}?image_data_url=${encodeURIComponent(image_data_processed_dataurl)}`,
125+
windowId: tab.windowId ?? undefined,
126+
openerTabId: tab.id ?? undefined,
127+
active: !settings.get("local", "newTabsLoadInBackground"),
126128
});
127129

128130
browser.tabs.sendMessage(tab.id, { type: "google-post-end" });

Diff for: content_script.js

-32
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,6 @@ browser.runtime.onMessage.addListener(async function (message) {
3939
elem.remove();
4040
alert(browser.i18n.getMessage("sendingImageError"));
4141
break;
42-
case "open-new-tab":
43-
const container = document.createElement("div");
44-
container.style.display = "none";
45-
document.body.appendChild(container);
46-
47-
const form = document.createElement("form");
48-
form.action = `https://lens.google.com/v3/upload?ep=ccm&s=&st=${Date.now()}`;
49-
form.method = "POST";
50-
form.enctype = "multipart/form-data";
51-
form.target = "_blank";
52-
container.appendChild(form);
53-
54-
const file_input = document.createElement("input");
55-
file_input.type = "file";
56-
file_input.name = "encoded_image";
57-
form.appendChild(file_input);
58-
59-
const pid_input = document.createElement("input");
60-
pid_input.type = "text";
61-
pid_input.name = "processed_image_dimensions";
62-
pid_input.value = "1000,1000";
63-
64-
const result = await fetch(message.data_url);
65-
const file = new Blob([await result.arrayBuffer()]);
66-
const data_transfer = new DataTransfer();
67-
const file_obj = new File([file], "image.jpg", { type: "image/jpeg" });
68-
data_transfer.items.add(file_obj);
69-
file_input.files = data_transfer.files;
70-
71-
form.submit();
72-
73-
container.remove();
7442
default:
7543
break;
7644
}

Diff for: post_to_lens.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
This Source Code Form is subject to the terms of the Mozilla Public
3+
License, v. 2.0. If a copy of the MPL was not distributed with this
4+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
-->
6+
7+
<html>
8+
<head>
9+
<link rel="stylesheet" href="color.css">
10+
</head>
11+
<body style="height: 100vh;font-family: sans-serif;">
12+
<div id="search_on_google_lens_elem" style="position: fixed; text-align: center; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.6); z-index: 1000000;">
13+
<div style="position: absolute; top: 50%; left: 50%; color: white; transform: translate(-50%, -50%); font-size: 50px; white-space: nowrap;">
14+
<img style="width: 60px; height: 60px;" src="/loading.svg"></img>
15+
<div class="i18n-text" data-i18n-id="sendingImage"></div>
16+
</div>
17+
</div>
18+
<script src="l10n.js"></script>
19+
<script type="module" src="./post_to_lens.mjs"></script>
20+
</body>
21+
</html>

Diff for: post_to_lens.mjs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
(async () => {
7+
const params = new URLSearchParams(window.location.search);
8+
const image_data_url = decodeURIComponent(params.get("image_data_url") ?? "");
9+
if (!image_data_url) {
10+
return;
11+
}
12+
13+
const container = document.createElement("div");
14+
container.style.display = "none";
15+
document.body.appendChild(container);
16+
17+
const form = document.createElement("form");
18+
form.action = `https://lens.google.com/v3/upload?ep=ccm&s=&st=${Date.now()}`;
19+
form.method = "POST";
20+
form.enctype = "multipart/form-data";
21+
// form.target = "_blank";
22+
container.appendChild(form);
23+
24+
const file_input = document.createElement("input");
25+
file_input.type = "file";
26+
file_input.name = "encoded_image";
27+
form.appendChild(file_input);
28+
29+
const pid_input = document.createElement("input");
30+
pid_input.type = "text";
31+
pid_input.name = "processed_image_dimensions";
32+
pid_input.value = "1000,1000";
33+
34+
const result = await fetch(image_data_url);
35+
const file = new Blob([await result.arrayBuffer()]);
36+
const data_transfer = new DataTransfer();
37+
const file_obj = new File([file], "image.jpg", { type: "image/jpeg" });
38+
data_transfer.items.add(file_obj);
39+
file_input.files = data_transfer.files;
40+
41+
form.submit();
42+
43+
container.remove();
44+
})();

0 commit comments

Comments
 (0)