File: background.js

package info (click to toggle)
allow-html-temp 10.0.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 368 kB
  • sloc: javascript: 1,454; makefile: 25; sh: 7
file content (205 lines) | stat: -rw-r--r-- 8,576 bytes parent folder | download | duplicates (3)
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
// Since we disconnect all tabs from the policy prefs, we need to manually reload
// them on pref updates. If the pref change is a temporary add-on-inflicted change,
// we reload only a specific tab.
var ahtStatus = {
  reloadAllTabs: true
};

var blockButtonClicks = [];

messenger.messageContentPolicy.onChanged.addListener(async (newValue) => {
  // If this policy change is part of an add-on inflicted temporary
  // policy change, ignore it.
  if (ahtStatus.ignorePolicyChange) {
    return;
  }

  let tabs = await messenger.tabs.query({});
  for (let tab of tabs) {
    if (!["messageDisplay", "mail"].includes(tab.type)) {
      continue;
    }
    if (ahtStatus.reloadAllTabs || ahtStatus.reloadTab == tab.id) {
      await messenger.allowHtmlTemp.reloadTab(tab.id);
    }
  }
  await setButtonIconAndLabel();
});

// An option change could cause the button policy to be identical to the current
// policy and the button needs to be disabled.
messenger.storage.onChanged.addListener(async (changes) => {
  await optionsInit();
  if (changes.buttonHtmlMode) {
    // Reload all already open tabs, to trigger the single entry point for all
    // UI updates of the AHT addon: a message display.
    let tabs = await messenger.tabs.query({});
    for (let tab of tabs) {
      if (!["messageDisplay", "mail"].includes(tab.type)) {
        continue;
      }
      await messenger.allowHtmlTemp.reloadTab(tab.id);
    }
  }
  await setButtonIconAndLabel();
  await setCommandKey();
});

// Add eventListener for onClicked on the message header button.
messenger.messageDisplayAction.onClicked.addListener(async (tab, info) => {
  await ahtLogic(tab.id, info, false);
});

// Add eventListener for remote item in remoteContentBar doorhanger context menu
messenger.allowHtmlTemp.onClick.addListener(async (tabId) => {
  consoleDebug("AHT: allowHtmlTemp.onClick.addListener: tabId = ", tabId);
  await ahtLogic(tabId, "", true);
});

// Disconnect all message tabs from the menu/prefs and disable/enable buttons.
messenger.messageDisplay.onMessagesDisplayed.addListener(async (tab, messages) => {
  await messenger.allowHtmlTemp.disconnectTab(tab.id);

  // This is a reload caused by an add-on inflicted policy change, return to the
  // user defined value without reloading the tab again.
  if (ahtStatus.returnToPolicy) {
    // The following policy update should not trigger a reload.
    // not 100 % sure, but use the complete object, not only msgBodyAs
    let updateProperties = ahtStatus.returnToPolicy;
    ahtStatus = {
      ignorePolicyChange: true
    }
    // not 100 % sure, but use the complete object, not only msgBodyAs
    await messenger.messageContentPolicy.update(updateProperties);
  } else {
    // Update buttons for the newly loaded message.
    updateActionButtonForNewMessages(tab, messages);
  }

  // Remove the action block now that the policy has been reset to default. 
  blockButtonClicks[tab.id] = false;
  consoleDebug("AHT: onMessagesDisplayed: blockButtonClicks tab.id: ", tab.id);
  consoleDebug("AHT: onMessagesDisplayed: blockButtonClicks value: ", blockButtonClicks[tab.id]);

  // Reconnect after the add-on inflicted temporary policy change, to have all tabs reloaded 
  // in case of later default policy change
  if (ahtStatus.ignorePolicyChange) {
    ahtStatus = {
      reloadAllTabs: true
    }
  }
  await setButtonIconAndLabel();
});

(async () => {getCurrentPolicy
  await optionsMigrate();
  await optionsInit();

  // Reload all already open tabs, to trigger the single entry point for all
  // UI updates of the AHT addon: a message display.
  let tabs = await messenger.tabs.query({});
  for (let tab of tabs) {
    if (!["messageDisplay", "mail"].includes(tab.type)) {
      continue;
    }
    await messenger.allowHtmlTemp.reloadTab(tab.id);
  }

})();

async function ahtLogic(tabId, info, remoteButton) {
  consoleDebug("AHT: allowHtmlTemp: fired");
  consoleDebug("AHT: allowHtmlTemp: options.debug = " + options.debug);
  consoleDebug("AHT: allowHtmlTemp: options.buttonHtmlMode = " + options.buttonHtmlMode);
  consoleDebug("AHT: allowHtmlTemp: options.tempRemoteContent = " + options.tempRemoteContent);
  consoleDebug("AHT: allowHtmlTemp: tabId: " + tabId);
  consoleDebug("AHT: allowHtmlTemp: info.modifiers: " + info.modifiers);
  consoleDebug("AHT: allowHtmlTemp: remoteButton: " + remoteButton);

  let buttonPolicy = await getButtonPolicy(info, remoteButton);
  let currentPolicy = await getCurrentPolicy();
  consoleDebug("AHT: ahtLogic: buttonPolicy: ", buttonPolicy);
  consoleDebug("AHT: ahtLogic: currentPolicy: ", currentPolicy);

  // Block the action in case of quick multiple clicks, to prevent garbled default prefs
  consoleDebug("AHT: ahtLogic: blockButtonClicks tabId: ", tabId);
  consoleDebug("AHT: ahtLogic: blockButtonClicks value: ", blockButtonClicks[tabId]);
  if (blockButtonClicks[tabId])  { return; }
  blockButtonClicks[tabId] = true;
  consoleDebug("AHT: ahtLogic: blockButtonClicks tabId: ", tabId);
  consoleDebug("AHT: ahtLogic: blockButtonClicks value: ", blockButtonClicks[tabId]);

  // The following policy switch is not a change of the default, but a temporary
  // switch, so we have to return to the current policy once the tab is loaded.
  ahtStatus = {
    returnToPolicy: currentPolicy,
    reloadTab: tabId,
  }

  await ahtFunctions.allowHtmlTemp(tabId, info.modifiers, remoteButton);

  // In the following case the above call of ahtFunctions.allowhtmltemp() doesn't trigger a message reload,
  // so we need to trigger the message reload manual by the following lines
  if (buttonPolicy.msgBodyAs == currentPolicy.msgBodyAs) {
    consoleDebug("AHT: ahtLogic: buttonPolicy.msgBodyAs == currentPolicy.msgBodyAs: ", (buttonPolicy.msgBodyAs == currentPolicy.msgBodyAs));
    await messenger.allowHtmlTemp.reloadTab(tabId);
  }
}

async function getButtonPolicy(info, remoteButton) {
  // RemoteContent popupmenu item clicked in remote content bar in a HTML message
  if (remoteButton === true) {
    consoleDebug("AHT: getButtonPolicy: remoteButton === true");
    return { 
      msgBodyAs: "original",
      disableRemoteContent: false,
      // do not use false for attachmentsInline, but null to leave option unset if not tempInline
      attachmentsInline: ((options.tempInline === true) ? true : null)
    };
  }

  // Adapt button policy according to the possible modifier keys
  // Addon button clicked + both CTRL and SHIFT key
  if((info.modifiers.includes("Ctrl") || info.modifiers.includes("Command")) && info.modifiers.includes("Shift")) {
    consoleDebug("AHT: getButtonPolicy: Addon button clicked + both CTRL and SHIFT key");
    return { msgBodyAs: "sanitized" };
  }
  // Addon button clicked + only CTRL key
  else if((info.modifiers.includes("Ctrl") || info.modifiers.includes("Command")) && !(info.modifiers.includes("Shift"))) {
    consoleDebug("AHT: getButtonPolicy: Addon button clicked + only CTRL key");
    return { 
      msgBodyAs: "original",
      disableRemoteContent: false,
      // do not use false for attachmentsInline, but null to leave option unset if not tempInline
      attachmentsInline: ((options.tempInline === true) ? true : null)
    };
  }
  // Addon button clicked + only SHIFT key
  else if ((info.modifiers.includes("Shift")) && !((info.modifiers.includes("Ctrl") || info.modifiers.includes("Command")))) {
    consoleDebug("AHT: getButtonPolicy: Addon button clicked + only SHIFT key");
    return { msgBodyAs: "plaintext" };
  }

  // The last case is just the button without a relevant modifier key
  else {
    const modeMap = {
      "buttonMode_plaintext": "plaintext",
      "buttonMode_sanitized": "sanitized",
      "buttonMode_html": "original",
    }
    // return modeMap[await getOption("buttonHtmlMode")];
    // buttonPolicy must include the options tempRemoteContent (which is vice versa to disableRemoteContent) 
    // and tempInline
    return { 
      msgBodyAs: modeMap[await getOption("buttonHtmlMode")], 
      disableRemoteContent: !(options.tempRemoteContent), 
      // do not use false for attachmentsInline, but null to leave option unset if not tempInline
      attachmentsInline: ((options.tempInline === true) ? true : null)
    };
  }
}

async function getCurrentPolicy() {
  let currentPolicy = await messenger.messageContentPolicy.getCurrent();
  return currentPolicy;
}