Files
auto-login/dist/messaging.js
T

28 lines
931 B
JavaScript
Raw Normal View History

import { t } from "./i18n.js";
export function sendMessage(message, retries = 3) {
return new Promise((resolve, reject) => {
let attempt = 0;
const trySend = () => {
attempt++;
if (!chrome.runtime?.id) {
location.reload();
reject(new Error(t("toastContextReload")));
return;
}
chrome.runtime.sendMessage(message, (resp) => {
const err = chrome.runtime.lastError;
if (err) {
if (attempt < retries && /Receiving end does not exist/i.test(err.message ?? "")) {
setTimeout(trySend, 200 * attempt);
return;
}
reject(new Error(err.message ?? ""));
return;
}
resolve(resp);
});
};
trySend();
});
}