-
Notifications
You must be signed in to change notification settings - Fork 1
/
background-script.js
139 lines (123 loc) · 3.83 KB
/
background-script.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
/*
* Copy Patch Thunderbird Add-On
*
* Copyright (c) Jan Kiszka, 2019-2023
* Copyright (c) John Bieling, 2023
*
* Authors:
* Jan Kiszka <[email protected]>
* John Bieling <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import "./node_modules/email-addresses/lib/email-addresses.js";
function getFirstHeader(arr)
{
if (Array.isArray(arr) && arr.length > 0) {
return arr[0];
}
return undefined;
}
function getAllHeader(arr)
{
if (Array.isArray(arr) && arr.length > 0) {
return arr;
}
return undefined;
}
function parseDisplayName(addr)
{
let rv = emailAddresses.parseOneAddress(addr);
return {
name: rv.name,
email: rv.address,
}
}
/* Find first text/plain body */
function getBody(parts)
{
/* First check all parts in this level */
for (let part of parts) {
if (part.body && part.contentType == "text/plain") {
return part.body;
}
}
/* Now check all subparts */
for (let part of parts) {
if (part.parts) {
let body = getBody(part.parts);
if (body) {
return body;
}
}
}
return null;
}
async function getMsgData(messageId)
{
let full = await browser.messages.getFull(messageId);
let date = await getFirstHeader(full.headers["date"]);
let from = await getAllHeader(full.headers["from"]);
let replyTo = await getAllHeader(full.headers["reply-to"]);
let subject = await getFirstHeader(full.headers["subject"]);
let body = getBody(full.parts);
if (!body) {
return null;
}
return {
header: {
date: date ? new Date(date) : date,
from: from ? from.map(addr => parseDisplayName(addr)) : from,
replyTo: replyTo ? replyTo.map(addr => parseDisplayName(addr)) : replyTo,
subject: subject
},
body: body,
isPatch: (body.indexOf("\n---") >= 0 && body.indexOf("\n+++") >= 0) ||
body.indexOf("\ndiff --git") >= 0
}
}
function main()
{
messenger.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getMsg") {
return browser.messageDisplay.getDisplayedMessage(sender.tab.id).then(
msg => getMsgData(msg.id)
);
}
if (request.action === "clipboardWrite") {
navigator.clipboard.writeText(request.text);
messenger.messageDisplayAction.setBadgeBackgroundColor(
{tabId: sender.tab.id, color: "green"});
messenger.messageDisplayAction.setBadgeText(
{tabId: sender.tab.id, text: "✔"});
setTimeout(() => {
messenger.messageDisplayAction.setBadgeText(
{tabId: sender.tab.id, text: null});
}, 500);
return Promise.resolve();
}
return false;
});
messenger.messageDisplayAction.onClicked.addListener(tab => {
messenger.tabs.executeScript(tab.id, {file: "content-script.js"});
});
messenger.commands.onCommand.addListener(async (name, tab) => {
if (name !== "copyPatch") {
return;
}
if (await messenger.messageDisplayAction.isEnabled({tabId: tab.id})) {
messenger.tabs.executeScript(tab.id, {file: "content-script.js"});
}
});
messenger.messageDisplay.onMessageDisplayed.addListener(async (tab, message) => {
let msg = await getMsgData(message.id);
if (msg && msg.isPatch) {
messenger.messageDisplayAction.enable(tab.id);
} else {
messenger.messageDisplayAction.disable(tab.id);
}
});
}
main();