-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
205 lines (175 loc) · 5.21 KB
/
popup.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
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
// wait for theme detection as to not flash the default theme
const theme = await browser.theme.getCurrent()
document.documentElement.classList.add(`theme-${theme.properties.color_scheme}`)
browser.tabs.query({
active: true,
currentWindow: true,
}).then(handleActiveTabs)
let shouldAutoclose = false
async function autoclose() {
if (!shouldAutoclose) {
return
}
const info = await browser.windows.getCurrent()
browser.windows.remove(info.id)
}
async function askForMessage() {
try {
const response = await browser.runtime.sendMessage({action: 'getSelectedMessage'})
await handleMessage(response.message)
} catch (e) {
console.warn(e)
}
}
async function handleActiveTabs(tabs) {
if (tabs[0].type !== 'mail') {
shouldAutoclose = true
await askForMessage()
return
}
const message = await browser.messageDisplay.getDisplayedMessage(tabs[0].id)
if (!message) {
return
}
await handleMessage(message)
}
async function handleMessage(message) {
let fullMessage = await browser.messages.getFull(message.id)
const headers = fullMessage.headers
const messageEl = document.getElementById('message')
const actionContainerEl = document.getElementById('action-container')
if (!headers.hasOwnProperty('list-unsubscribe')) {
messageEl.innerText = browser.i18n.getMessage('noUnsub')
return
}
let unsubEmail = null
let unsubLink = null
headers['list-unsubscribe'][0].split(',').forEach(link => {
let result
if (result = link.match(/^\s*<(mailto:.+)>/)) {
unsubEmail = result[1]
return
}
if (result = link.match(/^\s*<(https?:\/\/.+)>/)) {
unsubLink = result[1]
return
}
})
// validate link
if (unsubLink !== null) {
try {
unsubLink = new URL(unsubLink)
} catch (e) {
console.warn(e)
unsubLink = null
}
}
// validate and prepare email
let unsubEmailSubject = null
if (unsubEmail !== null) {
try {
unsubEmail = new URL(unsubEmail)
// extract subject
if (unsubEmail.searchParams.has('subject')) {
unsubEmailSubject = unsubEmail.searchParams.get('subject')
}
unsubEmail = unsubEmail.pathname
if (!unsubEmail.match(/^[^@]+@[^@]+$/)) {
console.warn('invalid email address', unsubEmail)
unsubEmail = null
}
} catch (e) {
console.warn(e)
unsubEmail = null
}
}
if (unsubLink === null && unsubEmail === null) {
messageEl.innerText = browser.i18n.getMessage('noUnsub')
console.error('no valid unsubscribe link or email found')
return
}
// check if RFC 8058 is supported
let unsubCommand = null
if (headers.hasOwnProperty('list-unsubscribe-post')) {
unsubCommand = headers['list-unsubscribe-post'][0]
}
messageEl.innerText = browser.i18n.getMessage('unsubConfirmPrompt')
if (unsubLink !== null) {
const linkContainerEl = document.getElementById('unsub-link-container')
const linkEl = document.createElement('code')
linkEl.innerText = unsubLink
linkEl.addEventListener('click', () => {
navigator.clipboard.writeText(linkEl.innerText)
linkEl.classList.add('hl')
setTimeout(() => {
linkEl.classList.remove('hl')
}, 100)
})
linkContainerEl.innerText = browser.i18n.getMessage('unsubLink')
linkContainerEl.appendChild(linkEl)
if (unsubCommand) {
const button = document.createElement('button')
button.innerText = browser.i18n.getMessage('confirmOneClick')
button.addEventListener('click', async () => {
linkContainerEl.style.display = 'none'
actionContainerEl.style.display = 'none'
messageEl.innerText = browser.i18n.getMessage('oneClickInProgress')
try {
await fetch(unsubLink, {
method: 'POST',
body: unsubCommand,
});
messageEl.innerText = browser.i18n.getMessage(
'oneClickSuccess'
);
} catch (e) {
messageEl.innerText = browser.i18n.getMessage(
'oneClickFailure',
e.message
);
}
})
actionContainerEl.appendChild(button)
}
const button = document.createElement('button')
button.innerText = browser.i18n.getMessage('confirmOpenLink')
button.addEventListener('click', async () => {
browser.tabs.create({
url: unsubLink.toString(),
})
await autoclose()
})
actionContainerEl.appendChild(button)
}
if (unsubEmail !== null) {
const button = document.createElement('button')
button.innerText = browser.i18n.getMessage('confirmComposeEmail')
button.addEventListener('click', async () => {
const identityId = await (async (message) => {
// try to get identityId from the message
if (typeof message.folder === 'Object') {
const identities = await messenger.accounts.get(message.folder.accountId).identities
// we can use it only if there is only one identity
if (identities.length === 1) {
return identities[0].id
}
}
// try match email address to identity
const identities = await messenger.identities.list()
const matching = identities.find(identity => message.recipients.some(email => identity.email === email))
if (matching === undefined) {
console.warn('no matching identity found, using default')
return identities[0].id
}
return matching.id
})(message)
browser.compose.beginNew({
to: unsubEmail,
subject: unsubEmailSubject ?? 'unsubscribe',
identityId,
})
await autoclose()
})
actionContainerEl.appendChild(button)
}
}