-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
129 lines (110 loc) · 4.28 KB
/
background.js
File metadata and controls
129 lines (110 loc) · 4.28 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
chrome.contextMenus.create({
title: 'copy base64 ➜ text',
id: 'base64coderbase64text',
contexts: ['selection'],
visible: true
}, () => chrome.runtime.lastError);
chrome.contextMenus.create({
title: 'copy text ➜ base64',
id: 'base64codermenutextbase64',
contexts: ['selection'],
visible: true
}, () => chrome.runtime.lastError);
chrome.contextMenus.create({
title: 'copy image ➜ base64',
id: 'base64codermenuimagebase64',
contexts: ['page', 'image', 'frame', 'link'],
visible: true
}, () => chrome.runtime.lastError);
chrome.contextMenus.create({
title: 'open base64',
id: 'base64coderopenbase64',
contexts: ['selection'],
visible: true
}, () => chrome.runtime.lastError);
chrome.omnibox.onInputEntered.addListener((text) => {
const decoded = encodeURIComponent(text);
chrome.tabs.create({ url: chrome.runtime.getURL(`convert/index.html?text=${decoded}`) });
});
chrome.omnibox.onInputChanged.addListener((text, suggest) => {
text = text.trim();
var suggestions = [];
suggestions.push({ content: text, description: "Base64Coder" });
suggestions.push({ content: "[from] " + text, description: "base64 ➜ text" });
suggestions.push({ content: "[to] " + text, description: "text ➜ base64" });
// Set first suggestion as the default suggestion
chrome.omnibox.setDefaultSuggestion({ description: suggestions[0].description });
// Suggest the remaining suggestions
suggest(suggestions);
})
chrome.contextMenus.onClicked.addListener(async function (info, tab) {
if (info.menuItemId == "base64coderopenbase64") openBase64(info.selectionText, tab);
if (info.menuItemId == "base64coderbase64text") decodeText(info.selectionText, tab);
if (info.menuItemId == "base64codermenutextbase64") encodeText(info.selectionText, tab);
if (info.menuItemId == "base64codermenuimagebase64") await encodeImage(info, tab);
});
chrome.action.onClicked.addListener(function () {
chrome.tabs.create({ url: chrome.runtime.getURL('convert/index.html') });
})
async function encodeImage(info, tab) {
waitBadge();
await chrome.tabs.sendMessage(tab.id, { type: 'getClickedEl', frameId: info.frameId, tabId: tab.id });
}
async function openBase64(base64) {
chrome.tabs.create({ url: chrome.runtime.getURL(`convert/index.html?text=${base64}`) });
}
async function decodeText(base64, tab) {
const decodedText = decodeURIComponent(atob(base64));
await chrome.tabs.sendMessage(tab.id, { type: 'copy', text: decodedText });
successBadge();
}
async function encodeText(text, tab) {
let encodedText;
try {
encodedText = btoa(text);
} catch (err) {
encodedText = btoa(encodeURIComponent(text));
}
await chrome.tabs.sendMessage(tab.id, { type: 'copy', text: encodedText });
successBadge();
}
chrome.runtime.onMessage.addListener(async function (request, sender, sendResponse) {
if (request.type === 'getBase64ImageFromElement') {
fetch(request.src)
.then(response => response.blob())
.then(blob => {
var reader = new FileReader();
reader.onload = async function () {
await chrome.tabs.sendMessage(request.tabId, { type: 'copy', text: this.result });
sendResponse('ok');
successBadge();
};
reader.readAsDataURL(blob);
})
.catch(() => errorBadge());
}
if (request.type === 'error') errorBadge();
if (request.type === 'success') successBadge();
if (request.type === 'openFAQ') {
chrome.tabs.create({ url: chrome.runtime.getURL('faq/index.html') });
}
sendResponse({ received: true });
});
function successBadge() {
chrome.action.setBadgeText({ text: 'DONE' });
chrome.action.setBadgeBackgroundColor({ color: 'green' });
setTimeout(() => chrome.action.setBadgeText({ text: '' }), 500);
}
function errorBadge() {
chrome.action.setBadgeText({ text: 'ERR' });
chrome.action.setBadgeBackgroundColor({ color: 'red' });
setTimeout(() => chrome.action.setBadgeText({ text: '' }), 500);
}
function waitBadge() {
chrome.action.setBadgeText({ text: 'WAIT' });
chrome.action.setBadgeBackgroundColor({ color: 'yellow' });
setTimeout(async () => {
const badgeText = await chrome.action.getBadgeText({});
if (badgeText === 'WAIT') errorBadge();
}, 2000);
}