-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
264 lines (230 loc) · 9.68 KB
/
background.js
File metadata and controls
264 lines (230 loc) · 9.68 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
**********************************************************************
* -------------------------------------------------------------------
* Project Name : Abdal Reverse Type
* File Name : background.js
* Author : Ebrahim Shafiei (EbraSha)
* Email : Prof.Shafiei@Gmail.com
* Created On : 2024-03-21 13:00:00
* Description : Background script for handling context menu
* -------------------------------------------------------------------
*
* "Coding is an engaging and beloved hobby for me. I passionately and insatiably pursue knowledge in cybersecurity and programming."
* – Ebrahim Shafiei
*
**********************************************************************
*/
console.log('Background script loading...');
// Storage compatibility
const storage = (typeof browser !== 'undefined') ? browser.storage : chrome.storage;
const runtime = (typeof browser !== 'undefined') ? browser.runtime : chrome.runtime;
// Current user shortcut
let currentShortcut = null;
// Load settings from storage
function loadSettings() {
storage.sync.get(['shortcut'], function(result) {
if (result.shortcut) {
currentShortcut = parseShortcutString(result.shortcut);
console.log('Loaded shortcut:', currentShortcut);
}
});
}
// Parse shortcut string into key combination
function parseShortcutString(shortcutString) {
if (!shortcutString) return null;
console.log('Parsing shortcut string:', shortcutString);
const parts = shortcutString.split('+');
const result = {
ctrl: parts.includes('Ctrl'),
alt: parts.includes('Alt'),
shift: parts.includes('Shift'),
meta: parts.includes('Meta'),
key: parts[parts.length - 1].toUpperCase() // Always use uppercase for key
};
console.log('Parsed shortcut:', result);
return result;
}
// Create context menu
chrome.contextMenus.create({
id: "convertText",
title: "تبدیل متن انتخاب شده",
contexts: ["selection"]
}, () => {
if (chrome.runtime.lastError) {
console.error('Error creating context menu:', chrome.runtime.lastError);
} else {
console.log('Context menu created successfully');
}
});
// Handle browser action click (toolbar icon)
chrome.browserAction.onClicked.addListener(function(tab) {
console.log('Browser action clicked');
// Open options page
if (typeof browser !== 'undefined') {
browser.runtime.openOptionsPage();
} else {
chrome.runtime.openOptionsPage();
}
});
// Function to convert selected text on the active tab
function convertSelectedText() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "convertText"
}, function(response) {
if (chrome.runtime.lastError) {
console.error('Error sending message:', chrome.runtime.lastError);
// Try injecting the content script again
chrome.tabs.executeScript(tabs[0].id, {
file: 'content.js'
}, function() {
if (chrome.runtime.lastError) {
console.error('Error injecting content script:', chrome.runtime.lastError);
} else {
// Try sending the message again after injection
setTimeout(() => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "convertText"
});
}, 100);
}
});
} else {
console.log('Response from content script:', response);
}
});
}
});
}
// Handle context menu click
chrome.contextMenus.onClicked.addListener((info, tab) => {
console.log('Context menu clicked:', info);
if (info.menuItemId === "convertText") {
console.log('Convert text menu item clicked');
// Send message to content script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "convertText",
selectedText: info.selectionText
}, function(response) {
if (chrome.runtime.lastError) {
console.error('Error sending message:', chrome.runtime.lastError);
// Try injecting the content script again
chrome.tabs.executeScript(tabs[0].id, {
file: 'content.js'
}, function() {
if (chrome.runtime.lastError) {
console.error('Error injecting content script:', chrome.runtime.lastError);
} else {
// Try sending the message again after injection
setTimeout(() => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "convertText",
selectedText: info.selectionText
});
}, 100);
}
});
} else {
console.log('Response from content script:', response);
}
});
}
});
}
});
// Listen for messages from options page and content scripts
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// Handle settings update broadcasts
if (request.action === 'settingsUpdated') {
console.log('Settings updated, broadcasting to all tabs:', request.settings);
// Broadcast the settings update to all tabs
chrome.tabs.query({}, function(tabs) {
for (let tab of tabs) {
console.log('Broadcasting settings to tab:', tab.id);
chrome.tabs.sendMessage(tab.id, {
action: "updateSettings",
settings: request.settings
}, function(response) {
if (chrome.runtime.lastError) {
// Ignore errors for tabs that don't have content script running
console.debug('Could not update tab:', tab.id, chrome.runtime.lastError.message);
} else if (response) {
console.log('Tab', tab.id, 'settings update response:', response);
}
});
}
});
sendResponse({success: true, message: 'Settings broadcast initiated'});
return true;
}
// Check if the current shortcut is registered
if (request.action === 'checkShortcut') {
console.log('Checking current shortcut:', currentShortcut);
sendResponse({
success: true,
shortcut: currentShortcut
});
return true;
}
// Handle keyboard events from content script
if (request.action === 'keyEvent') {
console.log('Received key event:', request.keyEvent);
console.log('Current shortcut:', currentShortcut);
if (!currentShortcut) {
console.log('No shortcut registered');
sendResponse({matched: false});
return true;
}
// Check if the key event matches our shortcut
const keyEvent = request.keyEvent;
const shortcut = currentShortcut;
console.log('Comparing keyEvent:', keyEvent, 'with shortcut:', shortcut);
// Check each property carefully
const ctrlMatch = keyEvent.ctrl === shortcut.ctrl;
const altMatch = keyEvent.alt === shortcut.alt;
const shiftMatch = keyEvent.shift === shortcut.shift;
const metaMatch = keyEvent.meta === shortcut.meta;
// Use case-insensitive comparison for key
const keyMatch = keyEvent.key.toUpperCase() === shortcut.key.toUpperCase();
console.log('Match details:', {
ctrlMatch,
altMatch,
shiftMatch,
metaMatch,
keyMatch,
keyEventKey: keyEvent.key,
shortcutKey: shortcut.key
});
const matched = ctrlMatch && altMatch && shiftMatch && metaMatch && keyMatch;
if (matched) {
console.log('Shortcut matched! Converting text...');
// Send message back to the content script to convert text
chrome.tabs.sendMessage(sender.tab.id, {
action: "convertText"
}, function(response) {
console.log('Response from content script after shortcut:', response);
});
sendResponse({matched: true});
} else {
sendResponse({matched: false});
}
return true;
}
// Register a new shortcut
if (request.action === 'registerShortcut') {
console.log('Registering shortcut:', request.shortcut);
currentShortcut = parseShortcutString(request.shortcut);
// Save to storage to ensure persistence
storage.sync.set({shortcut: request.shortcut}, function() {
console.log('Shortcut saved to storage');
});
sendResponse({success: true, shortcut: currentShortcut});
return true;
}
return true; // Keep the message channel open for async response
});
// Initialize
loadSettings();