-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
bg.js
79 lines (74 loc) · 1.92 KB
/
bg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
if (!self.browser && self.chrome) {
browser = chrome;
}
(async () => {
await browser.contextMenus.removeAll();
await browser.contextMenus.create({
id: "search-on-trace.moe",
title: "Search on trace.moe",
contexts: ["image", "video"],
});
})();
let imageDataURL = null;
let targetSrc = null;
let targetCurrentTime = null;
let tempTab = null;
let newTab = null;
browser.runtime.onMessage.addListener((request, { tab }, sendResponse) => {
if (newTab && tab.id === newTab.id && request.type === "getImageDataURL" && imageDataURL) {
sendResponse({ imageDataURL });
imageDataURL = null;
newTab = null;
}
if (tempTab && tab.id === tempTab.id && request.type === "getTargetSrc" && targetSrc) {
browser.tabs.sendMessage(
tempTab.id,
{
action: "getSearchImage",
srcUrl: targetSrc,
currentTime: targetCurrentTime,
},
(response) => {
browser.tabs.remove(tempTab.id).catch(() => {});
tempTab = null;
if (!response) return;
if (response.searchImage) {
search(response.searchImage);
}
}
);
targetSrc = null;
targetCurrentTime = null;
}
return true;
});
const search = async (dataURL) => {
imageDataURL = dataURL;
newTab = await browser.tabs.create({
url: "https://trace.moe",
});
};
browser.contextMenus.onClicked.addListener(async ({ srcUrl }) => {
if (!srcUrl) return;
const activeTabs = await browser.tabs.query({ active: true });
browser.tabs.sendMessage(
activeTabs[0].id,
{
action: "getSearchImage",
srcUrl,
},
async (response) => {
if (!response) return;
if (response.searchImage) {
search(response.searchImage);
return;
}
targetSrc = srcUrl;
targetCurrentTime = response.currentTime;
tempTab = await browser.tabs.create({
active: response.currentTime !== null,
url: srcUrl,
});
}
);
});