Skip to content

Commit

Permalink
fix: remove window/document references from background logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz committed May 7, 2023
1 parent 3dcaca3 commit 9671cac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
25 changes: 16 additions & 9 deletions src/common/BrowserAction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Messaging } from '@common/Messaging';
import { Requests } from '@common/Requests';
import { Shared } from '@common/Shared';
import browser, { Action as WebExtAction } from 'webextension-polyfill';

export interface BrowserActionRotating {
image: HTMLImageElement | null;
canvas: HTMLCanvasElement | null;
context: CanvasRenderingContext2D | null;
image: ImageBitmap | null;
canvas: OffscreenCanvas | null;
context: OffscreenCanvasRenderingContext2D | null;
degrees: number;
canceled: boolean;
}
Expand Down Expand Up @@ -71,18 +72,24 @@ class _BrowserAction {

async setRotatingIcon(): Promise<void> {
if (Shared.pageType === 'background') {
const image = document.createElement('img');
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const imageResponse = await Requests.fetch({
method: 'GET',
url: this.currentIcon,
});
const imageBlob = await imageResponse.blob();
const image = await createImageBitmap(imageBlob);
const canvas = new OffscreenCanvas(image.width, image.height);
const context = canvas.getContext('2d', {
willReadFrequently: true,
}) as OffscreenCanvasRenderingContext2D;
this.rotating = {
image,
canvas,
context,
degrees: 0,
canceled: false,
};
image.onload = () => void this.rotateIcon();
image.src = this.currentIcon;
await this.rotateIcon();
} else {
await Messaging.toExtension({ action: 'set-rotating-icon' });
}
Expand Down Expand Up @@ -134,7 +141,7 @@ class _BrowserAction {
this.rotating.degrees = 0;
}

window.setTimeout(() => void this.rotateIcon(), 30);
setTimeout(() => void this.rotateIcon(), 30);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/common/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ class _Errors {
environment: Shared.environment,
},
});
window.Rollbar = this.rollbar;
if (window) {
window.Rollbar = this.rollbar;
}
} else if (!allowRollbar && this.rollbar) {
delete this.rollbar;
delete window.Rollbar;
if (window) {
delete window.Rollbar;
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/common/Requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ class _Requests {
const retryAfterStr = response.headers.get('Retry-After');
if (retryAfterStr) {
const retryAfter = Number.parseInt(retryAfterStr) * 1000;
window.setTimeout(
() => void this.sendDirectly(request, tabId, resolve, reject),
retryAfter
);
setTimeout(() => void this.sendDirectly(request, tabId, resolve, reject), retryAfter);
return;
}
}
Expand Down

0 comments on commit 9671cac

Please sign in to comment.