-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
executable file
·140 lines (139 loc) · 3.94 KB
/
background.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import chromeService from "./src/services/chromeService";
import Routes from "./src/routes";
import messagePassing from "./src/services/messagePassing";
import constants from "./constants";
import db, { schema } from "./src/services/dbService";
import Constants from "./constants";
import { DataStore, generateGuid } from "./src/services/helper";
/**
* Main extension functionality
*
* @class Main
*/
class Main {
constructor() {
this.ctxMenuId1 = null;
this.ctxMenuId2 = null;
this.ctxMenuId3 = null;
this.init().catch((e) => {
console.log("Error loading extension", { e });
});
// set feedback form url
this.setFeedbackFormUrl();
}
init = async () => {
Routes();
this.initDb();
this.initContextMenu();
this.popUpClickSetup();
};
/**
* initialize db settings
* @method
* @memberof Main
*/
initDb = async () => {
const res = await db.get("____loaded");
if (!res.hasOwnProperty("____loaded")) {
await db.set({ ____loaded: true, ...schema.data });
chromeService.openHelpPage("welcome");
this.mountCSOnPreviouslyOpenedTabs().catch(() => {});
}
};
openCropWindow = async () => {
const screenshotUrl = await chromeService.takeScreenShot();
await messagePassing.sendMessageToActiveTab(
"/show_popup",
{ screenshotUrl },
() => {}
);
};
/**
* mount content script on previously opened tabs
* @method
* @memberof Main
*/
mountCSOnPreviouslyOpenedTabs = async () => {
chrome.tabs.query({}, (result) => {
result.map((tabInfo) => {
messagePassing.sendMessageToTab(
"/cs_mounted",
tabInfo.id,
{},
async (res) => {
if (!res) {
chrome.tabs.executeScript(tabInfo.id, {
file: "content_script.bundle.js",
});
}
}
);
});
});
};
popUpClickSetup = () => {
chrome.action.onClicked.addListener(this.openCropWindow);
};
/**
* Context menu option initialization
*
* @method
* @memberof Main
*/
initContextMenu = () => {
if (this.ctxMenuId1) return;
const extractTextFromScreenLabel = chromeService.getI18nMessage(
"extractTextFromScreenLabel"
); // Extract Text from this screen
this.ctxMenuId1 = chromeService.createContextMenu({
title: extractTextFromScreenLabel,
contexts: ["all"],
onclick: this.onContextMenu1Click,
});
if (this.ctxMenuId2) return;
const extractTextFromImageLabel = chromeService.getI18nMessage(
"extractTextFromImageLabel"
); // Extract Text from this image
this.ctxMenuId2 = chromeService.createContextMenu({
title: extractTextFromImageLabel,
contexts: ["image"],
onclick: this.onContextMenu2Click,
});
if (this.ctxMenuId3) return;
const uploadPdfLabel = chromeService.getI18nMessage("uploadPdfLabel"); // upload pdf to extract text from
this.ctxMenuId3 = chromeService.createContextMenu({
title: uploadPdfLabel,
contexts: ["all"],
onclick: this.onContextMenu3Click,
});
};
onContextMenu1Click = async (info, tab) => {
this.openCropWindow();
};
onContextMenu2Click = async (info, tab) => {
const { srcUrl } = info;
const uid = generateGuid();
DataStore.set(uid, srcUrl);
const url = `${Constants.appConfig.endpoint}/screen?id=${uid}`;
chrome.tabs.query({ url: "https://imagetext.xyz/*" }, (tabs) => {
const [tab] = tabs;
if (tab) {
chrome.tabs.update(tab.id, { url, active: true });
} else {
chrome.tabs.create({ url }, () => {});
}
});
};
onContextMenu3Click = async (info, tab) => {
const url = `${Constants.appConfig.endpoint}/pdfs`;
chrome.tabs.create({ url }, () => {});
};
/**
*set feedback form url shown while uninstalling
* */
setFeedbackFormUrl = () => {
chrome.runtime.setUninstallURL(constants.support.uninstallFeedbackForm);
};
}
// init main
new Main();