Skip to content

Instantly share code, notes, and snippets.

@5ec1cff
Last active August 3, 2023 13:23
Show Gist options
  • Save 5ec1cff/5f08e79e86edff5b0061f685d8042233 to your computer and use it in GitHub Desktop.
Save 5ec1cff/5f08e79e86edff5b0061f685d8042233 to your computer and use it in GitHub Desktop.
Yet Another Saucenao Search Extension
function injector(url) {
console.log("injected");
let im = document.querySelector("img");
function loaded() {
let canvas = document.createElement("canvas");
canvas.width = im.naturalWidth;
canvas.height = im.naturalHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(im, 0, 0);
canvas.toBlob((b) => {
console.log(b);
let form = document.createElement("form");
form.method = "POST";
form.action = url;
form.enctype = "multipart/form-data";
let input = document.createElement("input");
form.appendChild(input);
document.body.append(form);
input.type = "file";
input.name = "file";
let list = new DataTransfer();
let file = new File([b], "filename.png");
list.items.add(file);
input.files = list.files;
form.submit();
});
}
if (im.complete) loaded();
else im.onload = loaded;
}
function onTabOpen(id, tab) {
let url;
if (id == "saucenao") url = "https://saucenao.com/search.php";
else if (id == "ascii2d") url = "https://ascii2d.net/search/file";
chrome.scripting.executeScript({
func: injector,
target: { tabId: tab.id },
args: [url]
});
}
function sauce(info, tab) {
if (info.menuItemId == "ascii2d-url") {
chrome.tabs.create(
{
url: "https://ascii2d.net/search/url/" + encodeURIComponent(info.srcUrl) + "?type=color",
active: true,
}
);
return;
}
chrome.tabs.create(
{
url: info.srcUrl,
active: true,
},
onTabOpen.bind(null, info.menuItemId)
);
}
//chrome.contextMenus.remove("a");
chrome.contextMenus.create({
id: "saucenao",
title: "Search with saucenao",
contexts: ["image"],
});
chrome.contextMenus.create({
id: "ascii2d",
title: "Search with ascii2d",
contexts: ["image"],
});
chrome.contextMenus.create({
id: "ascii2d-url",
title: "Search with ascii2d by url",
contexts: ["image"],
});
chrome.contextMenus.onClicked.addListener(sauce);
{
"manifest_version": 3,
"name": "Yet Another Saucenao Search Extension",
"version": "1",
"permissions": ["activeTab", "scripting", "contextMenus"],
"host_permissions": ["\u003Call_urls>"],
"background": {
"service_worker": "background.js"
}
}
@5ec1cff
Copy link
Author

5ec1cff commented Aug 3, 2023

2023-08-03 Update: support ascii2d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment