spot fix: login check on reopen, give small timeout to tab opening

This commit is contained in:
nick-delirium 2024-09-18 17:45:52 +02:00
parent 715edd2b9c
commit 61b664bf52
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
2 changed files with 37 additions and 19 deletions

View file

@ -7,7 +7,9 @@ import {
} from "../utils/networkTracking"; } from "../utils/networkTracking";
import { import {
isTokenExpired isTokenExpired
} from '../utils/jwt' } from '../utils/jwt';
let checkBusy = false;
export default defineBackground(() => { export default defineBackground(() => {
const CHECK_INT = 60 * 1000; const CHECK_INT = 60 * 1000;
@ -217,7 +219,6 @@ export default defineBackground(() => {
} }
const { jwtToken, settings } = data; const { jwtToken, settings } = data;
const refreshUrl = `${safeApiUrl(settings.ingestPoint)}/api/spot/refresh` const refreshUrl = `${safeApiUrl(settings.ingestPoint)}/api/spot/refresh`
console.log(settings.ingestPoint, refreshUrl);
if (!isTokenExpired(jwtToken) || !jwtToken) { if (!isTokenExpired(jwtToken) || !jwtToken) {
if (refreshInt) { if (refreshInt) {
clearInterval(refreshInt); clearInterval(refreshInt);
@ -296,18 +297,20 @@ export default defineBackground(() => {
} }
let lastReq: Record<string, any> | null = null; let lastReq: Record<string, any> | null = null;
async function checkTokenValidity() { async function checkTokenValidity() {
if (checkBusy) return;
checkBusy = true;
const data = await browser.storage.local.get("jwtToken"); const data = await browser.storage.local.get("jwtToken");
console.log(data)
if (!data.jwtToken) { if (!data.jwtToken) {
void browser.runtime.sendMessage({ void browser.runtime.sendMessage({
type: messages.popup.to.noLogin, type: messages.popup.to.noLogin,
}); });
checkBusy = false
return; return;
} }
const ok = await refreshToken(); const ok = await refreshToken();
if (ok) { if (ok) {
setJWTToken(data.jwtToken)
if (!refreshInt) { if (!refreshInt) {
refreshInt = setInterval(() => { refreshInt = setInterval(() => {
void refreshToken(); void refreshToken();
@ -319,6 +322,7 @@ export default defineBackground(() => {
}, PING_INT); }, PING_INT);
} }
} }
checkBusy = false
} }
// @ts-ignore // @ts-ignore
browser.runtime.onMessage.addListener((request, sender, respond) => { browser.runtime.onMessage.addListener((request, sender, respond) => {
@ -884,14 +888,16 @@ export default defineBackground(() => {
) )
? "https://app.openreplay.com" ? "https://app.openreplay.com"
: settings.ingestPoint; : settings.ingestPoint;
void browser.tabs.create({
url: `${link}/view-spot/${id}`,
active: settings.openInNewTab,
});
void sendToActiveTab({ void sendToActiveTab({
type: "content:spot-saved", type: "content:spot-saved",
url: `${link}/view-spot/${id}`, url: `${link}/view-spot/${id}`,
}); });
setTimeout(() => {
void browser.tabs.create({
url: `${link}/view-spot/${id}`,
active: settings.openInNewTab,
});
}, 250)
const blob = base64ToBlob(videoData); const blob = base64ToBlob(videoData);
const mPromise = fetch(mobURL, { const mPromise = fetch(mobURL, {
@ -967,23 +973,34 @@ export default defineBackground(() => {
void initializeOffscreenDocument(); void initializeOffscreenDocument();
async function initializeOffscreenDocument() { async function initializeOffscreenDocument() {
const existingContexts = await browser.runtime.getContexts({}); const existingContexts = await browser.runtime.getContexts({
let recording = false; contextTypes: ['OFFSCREEN_DOCUMENT'],
});
const offscreenDocument = existingContexts.find( const offscreenDocument = existingContexts.find(
(c: { contextType: string }) => c.contextType === "OFFSCREEN_DOCUMENT", (c: { contextType: string }) => c.contextType === "OFFSCREEN_DOCUMENT",
); );
if (offscreenDocument) { if (offscreenDocument) {
await browser.offscreen.closeDocument(); return;
// TODO: check manifestv3 for reloading context
// try {
// await browser.offscreen.closeDocument();
// } catch (e) {
// console.trace(e)
// }
} }
await browser.offscreen.createDocument({ try {
url: "offscreen.html", await browser.offscreen.createDocument({
reasons: ["DISPLAY_MEDIA", "USER_MEDIA", "BLOBS"], url: "offscreen.html",
justification: "Recording from chrome.tabCapture API", reasons: ["DISPLAY_MEDIA", "USER_MEDIA", "BLOBS"],
}); justification: "Recording from chrome.tabCapture API",
});
} catch (e) {
console.log('cant create new offscreen document', e)
}
return recording; return;
} }
async function sendToActiveTab(message: { async function sendToActiveTab(message: {
type: string; type: string;
@ -1008,7 +1025,8 @@ export default defineBackground(() => {
if (contentArmy[sendTo]) { if (contentArmy[sendTo]) {
await browser.tabs.sendMessage(sendTo, message); await browser.tabs.sendMessage(sendTo, message);
} else { } else {
console.error("Content script not ready in tab", sendTo); console.error("Content script might not be ready in tab", sendTo);
await browser.tabs.sendMessage(sendTo, message);
} }
} }

View file

@ -41,7 +41,7 @@ export default defineUnlistedScript(() => {
const notificationContent = ` const notificationContent = `
<div class="flex gap-3 items-center"> <div class="flex gap-3 items-center">
<div class="spinner"></div> <div class="spinner"></div>
<span>${message}</span> <span>${message}</span>
</div> </div>
`; `;