-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
108 lines (90 loc) · 2.58 KB
/
background.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
import { fetchOptions } from '/modules/options.js'
browser.mailTabs.onSelectedMessagesChanged.addListener(async (tab, messageList) => {
if (messageList.messages.length !== 1) {
browser.messageDisplayAction.disable()
return
}
const message = await browser.messages.getFull(messageList.messages[0].id)
if (message.headers.hasOwnProperty('list-unsubscribe')) {
browser.messageDisplayAction.enable()
return
}
browser.messageDisplayAction.disable()
})
const menuItemId = 'message-list-unsub-button'
browser.runtime.onMessage.addListener(async (runtimeMessage, sender, sendResponse) => {
if (runtimeMessage.action !== 'optionsChanged') {
return
}
browser.menus.remove(menuItemId)
await enableContextButton()
})
window.addEventListener('load', enableContextButton)
async function enableContextButton() {
const options = await fetchOptions()
if (options.enableContextButton !== 'enable') {
return
}
let menuItemEnabled = false
let selectedMessage = null
const openWindow = () => {
if (selectedMessage === null) {
return
}
messenger.windows.create({
url: "popup.html",
type: "popup",
height: 240,
width: 580,
allowScriptsToClose: true,
})
}
browser.menus.create({
id: menuItemId,
contexts: ['message_list'],
title: browser.i18n.getMessage('unsubButtonLabel'),
enabled: menuItemEnabled,
onclick: openWindow
})
let lastMenuInstanceId = 0
let nextMenuInstanceId = 1
browser.menus.onShown.addListener(async (info, tab) => {
let menuInstanceId = nextMenuInstanceId++
lastMenuInstanceId = menuInstanceId
if (!info.contexts.includes('message_list')) {
return
}
if (info.selectedMessages.messages.length === 1) {
const message = info.selectedMessages.messages[0]
const fullMessage = await browser.messages.getFull(message.id)
selectedMessage = message
if (menuInstanceId !== lastMenuInstanceId) {
return
}
if (fullMessage.headers.hasOwnProperty('list-unsubscribe')) {
if (!menuItemEnabled) {
menuItemEnabled = true
browser.menus.update(menuItemId, {enabled: menuItemEnabled})
browser.menus.refresh()
}
return
}
}
selectedMessage = null
if (menuItemEnabled) {
menuItemEnabled = false
browser.menus.update(menuItemId, {enabled: menuItemEnabled})
browser.menus.refresh()
}
})
browser.menus.onHidden.addListener(() => {
lastMenuInstanceId = 0
})
// send last message ID when asked for it
browser.runtime.onMessage.addListener((runtimeMessage, sender, sendResponse) => {
if (runtimeMessage.action !== 'getSelectedMessage') {
return
}
sendResponse({message: selectedMessage})
})
}