Fix #29, add buttons on message compose confirmation screen

This commit is contained in:
Ian Bicking 2018-06-08 16:52:49 -05:00
Родитель 17ad24bb09
Коммит 63a428eb2e
2 изменённых файлов: 69 добавлений и 2 удалений

Просмотреть файл

@ -7,6 +7,10 @@ browser.runtime.onMessage.addListener((message, source) => {
} else if (message.type == "sendFailed") {
loginInterrupt();
return null;
} else if (message.type === "closeComposeTab") {
return browser.tabs.remove(message.tabId);
} else if (message.type === "closeTabs") {
return closeManyTabs(message.composeTabId, message.closeTabInfo);
}
console.error("Unexpected message type:", message.type);
return null;
@ -17,7 +21,7 @@ async function sendEmail(tabIds) {
let tabInfo = {};
for (let tab of allTabs) {
if (tabIds.includes(tab.id)) {
tabInfo[tab.id] = {url: tab.url, title: tab.title, favIcon: tab.favIconUrl, id: tab.id};
tabInfo[tab.id] = {url: tab.url, urlBar: tab.url, title: tab.title, favIcon: tab.favIconUrl, id: tab.id};
if (tab.discarded) {
console.info("Reloading discarded tab", tab.id, tab.url);
await browser.tabs.reload(tab.id);
@ -62,7 +66,9 @@ async function sendEmail(tabIds) {
});
await browser.tabs.sendMessage(newTab.id, {
type: "setHtml",
html
html,
thisTabId: newTab.id,
tabInfo
});
}
@ -84,3 +90,16 @@ function loginInterrupt() {
});
console.error("Sending failed, probably due to login");
}
async function closeManyTabs(composeTabId, otherTabInfo) {
let tabs = await browser.tabs.query({});
let toClose = [composeTabId];
for (let tab of tabs) {
// Note that .url might be the canonical URL, but .urlBar is what shows up in the URL bar
// and the tab API
if (otherTabInfo[tab.id] && otherTabInfo[tab.id].urlBar === tab.url) {
toClose.push(tab.id);
}
}
await browser.tabs.remove(toClose);
}

Просмотреть файл

@ -1,10 +1,14 @@
/* globals cloneInto */
browser.runtime.onMessage.addListener((message) => {
thisTabId = message.thisTabId;
closeTabInfo = message.tabInfo;
setHtml(message.html);
});
let completed = false;
let thisTabId;
let closeTabInfo;
window.addEventListener("beforeunload", () => {
if (completed) {
@ -87,3 +91,47 @@ function setHtml(html) {
clearTimeout(fixupInterval);
}, 100);
}
let completedTimeout = setInterval(() => {
let viewMessageEl = document.getElementById("link_vsm");
if (viewMessageEl) {
clearTimeout(completedTimeout);
showCloseButtons();
}
}, 300);
function showCloseButtons() {
let html = `<div style="
position: absolute;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -50px;
width: 400px;
">
<button class="done">Done</button> <br>
<button class="close-all-tabs">Close ${Object.keys(closeTabInfo).length} tabs</button>
</div>
`;
let div = document.createElement("div");
div.innerHTML = html;
div = div.childNodes[0];
let doneButton = div.querySelector(".done");
let closeAllTabsButton = div.querySelector(".close-all-tabs");
doneButton.addEventListener("click", async () => {
await browser.runtime.sendMessage({
type: "closeComposeTab",
tabId: thisTabId,
});
});
closeAllTabsButton.addEventListener("click", async () => {
await browser.runtime.sendMessage({
type: "closeTabs",
closeTabInfo,
composeTabId: thisTabId
})
});
document.body.appendChild(div);
}