forked from dgileadi/thunderbird-render-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
160 lines (138 loc) · 4.35 KB
/
background.js
File metadata and controls
160 lines (138 loc) · 4.35 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
const markdownPatterns = [
/^text\/markdown\b/i,
/^text\/x-markdown\b/i,
/\bmarkup=markdown\b/i,
];
const textPlainPattern = /^text\/plain\b/i;
const rfc822Pattern = /^message\/rfc822\b/i;
const defaultDetectScope = 'all-plain-text';
// Variables existentes
let detectScope = defaultDetectScope;
let isMarkdownMessage = false;
let showingMarkdown = false;
// Nuevas variables para el filtro de remitente
let senderEmail = '';
let enableSenderFilter = false;
// Nueva función para verificar el remitente
async function checkSender(messageId) {
try {
const messageDetails = await messenger.messages.get(messageId);
const from = messageDetails.author || messageDetails.from;
// Si el filtro no está activado, permitir todos los remitentes
if (!enableSenderFilter) {
return true;
}
// Si el filtro está activado, verificar el remitente
return from.toLowerCase().includes(senderEmail.toLowerCase());
} catch (error) {
console.error('Error checking sender:', error);
return false;
}
}
function shouldDisplayMarkdown(contentType) {
let patterns =
detectScope === defaultDetectScope
? [textPlainPattern, rfc822Pattern]
: markdownPatterns;
return patterns.some((pattern) => pattern.test(contentType));
}
function getPlainText(messagePart) {
if (
messagePart.body &&
(textPlainPattern.test(messagePart.contentType) ||
markdownPatterns.some((pattern) => pattern.test(messagePart.contentType)))
) {
return messagePart.body;
}
if (messagePart.parts) {
for (let i = 0; i < messagePart.parts.length; i++) {
let body = getPlainText(messagePart.parts[i]);
if (body) {
return body;
}
}
}
}
// Función modificada para incluir verificación de remitente
async function detectMarkdownMessage(tabId) {
isMarkdownMessage = false;
showingMarkdown = false;
let message = await messenger.messageDisplay.getDisplayedMessage(tabId);
// Verificar el remitente antes de procesar el mensaje
const isSenderAllowed = await checkSender(message.id);
if (!isSenderAllowed) {
return;
}
let full = await messenger.messages.getFull(message.id);
if (shouldDisplayMarkdown(full.headers['content-type'])) {
const text = getPlainText(full);
if (text) {
sendRenderMarkdownCommand(tabId, text);
isMarkdownMessage = true;
showingMarkdown = true;
}
}
updateContextMenuItem();
}
function sendRenderMarkdownCommand(tabId, text) {
messenger.tabs.sendMessage(tabId, {
command: 'renderMarkdown',
text: text,
as: showingMarkdown ? 'plain' : 'markdown'
});
}
async function updateContextMenuItem() {
if (contextMenuId) {
messenger.menus.update(contextMenuId, {
title: showingMarkdown ? 'Show as Plain Text' : 'Show as Markdown',
visible: isMarkdownMessage,
});
}
}
// Listener modificado para incluir las nuevas opciones
browser.storage.onChanged.addListener((changes) => {
if (changes.scope) {
detectScope = changes.scope.newValue || defaultDetectScope;
}
if (changes.senderEmail) {
senderEmail = changes.senderEmail.newValue || '';
}
if (changes.enableSenderFilter !== undefined) {
enableSenderFilter = changes.enableSenderFilter.newValue;
}
});
// Carga inicial de configuración modificada
browser.storage.sync.get(['scope', 'senderEmail', 'enableSenderFilter']).then((res) => {
detectScope = res.scope || defaultDetectScope;
senderEmail = res.senderEmail || '';
enableSenderFilter = res.enableSenderFilter || false;
});
// When the page sends us a command (when it loaded), detect markdown
browser.runtime.onMessage.addListener((data, sender) => {
if (data.command === 'detectMarkdownMessage') {
detectMarkdownMessage(sender.tab.id);
}
});
// Inject markdown rendering scripts and CSS
browser.messageDisplayScripts.register({
js: [
{ file: 'marked.min.js' },
{ file: 'purify.min.js' },
{ file: 'initial.js' },
],
css: [{ file: 'css/markdown.css' }],
});
// Add context menu item
const contextMenuId = messenger.menus.create({
title: 'Show as Plain Text',
contexts: ['page', 'frame'],
visible: false,
});
// Register a listener for the context menu click
messenger.menus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId == contextMenuId) {
sendRenderMarkdownCommand(tab.id, null);
showingMarkdown = !showingMarkdown;
updateContextMenuItem();
}
});