-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbackground.js
More file actions
94 lines (88 loc) · 2.92 KB
/
background.js
File metadata and controls
94 lines (88 loc) · 2.92 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
function sendCommandToTab(action) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs.length) return;
const tabId = tabs[0].id;
chrome.scripting.executeScript(
{ target: { tabId }, files: ["content.js"] },
() => {
if (chrome.runtime.lastError) return;
chrome.tabs.sendMessage(tabId, { action }, (response) => {
if (chrome.runtime.lastError) {
showNotification("NebulaEncrypt", "Перезагрузите страницу MAX и нажмите сочетание клавиш снова.");
return;
}
if (!response?.success && response?.message) {
showNotification("NebulaEncrypt", response.message);
}
});
}
);
});
}
function showNotification(title, message) {
try {
chrome.notifications.create({
type: "basic",
iconUrl: "icon-128.png",
title,
message,
silent: true,
});
} catch {}
}
chrome.commands.onCommand.addListener((command) => {
if (command === "encrypt-text") sendCommandToTab("encryptText");
if (command === "decrypt-text") sendCommandToTab("decryptText");
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "updateBadge") {
const count = request.count;
const tabId = sender.tab?.id;
if (!tabId) return;
try {
const text = count > 0 ? String(count) : "";
chrome.action.setBadgeText({ text, tabId });
chrome.action.setBadgeBackgroundColor({ color: "#4CAF50", tabId });
} catch {}
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== "complete" || !tab?.url) return;
try {
const url = new URL(tab.url);
const base = `${url.protocol}//${url.host}`;
const urlPattern = base.endsWith("/") ? base : base + "/";
chrome.storage.local.get("urlKeys", (result) => {
const urlKeys = result.urlKeys || {};
const hasKeys = !!urlKeys[urlPattern];
try {
chrome.action.setBadgeText({ text: hasKeys ? "" : "!", tabId });
chrome.action.setBadgeBackgroundColor({
color: hasKeys ? "#4CAF50" : "#FF9800",
tabId,
});
} catch {}
});
} catch {}
});
chrome.tabs.onActivated.addListener(({ tabId }) => {
chrome.tabs.get(tabId, (tab) => {
if (!tab?.url) return;
try {
const url = new URL(tab.url);
const base = `${url.protocol}//${url.host}`;
const urlPattern = base.endsWith("/") ? base : base + "/";
chrome.storage.local.get("urlKeys", (result) => {
const urlKeys = result.urlKeys || {};
const hasKeys = !!urlKeys[urlPattern];
try {
chrome.action.setBadgeText({ text: hasKeys ? "" : "!", tabId });
chrome.action.setBadgeBackgroundColor({
color: hasKeys ? "#4CAF50" : "#FF9800",
tabId,
});
} catch {}
});
} catch {}
});
});