mirror of
https://github.com/bitwarden/browser
synced 2026-02-20 19:34:03 +00:00
[deps] Platform: Update @types/chrome to v0.1.0 (#15697)
* [deps] Platform: Update @types/chrome to v0.1.0 * Fix typing * Fix other build errors * Fix strict compile * Update pkg and fix remaining type errors --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Daniel García <dani-garcia@users.noreply.github.com>
This commit is contained in:
@@ -141,8 +141,24 @@ function buildRegisterContentScriptsPolyfill() {
|
||||
return [possibleArray];
|
||||
}
|
||||
|
||||
function arrayOrUndefined(value?: number) {
|
||||
return value === undefined ? undefined : [value];
|
||||
function createTarget(
|
||||
tabId: number,
|
||||
frameId: number | undefined,
|
||||
allFrames: boolean,
|
||||
): chrome.scripting.InjectionTarget {
|
||||
if (frameId === undefined) {
|
||||
return {
|
||||
tabId,
|
||||
frameIds: undefined,
|
||||
allFrames: allFrames,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
tabId,
|
||||
frameIds: [frameId],
|
||||
allFrames: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function insertCSS(
|
||||
@@ -170,15 +186,17 @@ function buildRegisterContentScriptsPolyfill() {
|
||||
}
|
||||
|
||||
if (gotScripting) {
|
||||
return chrome.scripting.insertCSS({
|
||||
target: {
|
||||
tabId,
|
||||
frameIds: arrayOrUndefined(frameId),
|
||||
allFrames: frameId === undefined ? allFrames : undefined,
|
||||
},
|
||||
files: "file" in content ? [content.file] : undefined,
|
||||
css: "code" in content ? content.code : undefined,
|
||||
});
|
||||
if ("file" in content) {
|
||||
return chrome.scripting.insertCSS({
|
||||
target: createTarget(tabId, frameId, allFrames),
|
||||
files: [content.file],
|
||||
});
|
||||
} else {
|
||||
return chrome.scripting.insertCSS({
|
||||
target: createTarget(tabId, frameId, allFrames),
|
||||
css: content.code,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return chromeProxy.tabs.insertCSS(tabId, {
|
||||
@@ -226,11 +244,7 @@ function buildRegisterContentScriptsPolyfill() {
|
||||
if (gotScripting) {
|
||||
assertNoCode(normalizedFiles);
|
||||
const injection = chrome.scripting.executeScript({
|
||||
target: {
|
||||
tabId,
|
||||
frameIds: arrayOrUndefined(frameId),
|
||||
allFrames: frameId === undefined ? allFrames : undefined,
|
||||
},
|
||||
target: createTarget(tabId, frameId, allFrames),
|
||||
files: normalizedFiles.map(({ file }: { file: string }) => file),
|
||||
});
|
||||
|
||||
@@ -397,7 +411,7 @@ function buildRegisterContentScriptsPolyfill() {
|
||||
};
|
||||
const tabListener = async (
|
||||
tabId: number,
|
||||
{ status }: chrome.tabs.TabChangeInfo,
|
||||
{ status }: chrome.tabs.OnUpdatedInfo,
|
||||
{ url }: chrome.tabs.Tab,
|
||||
) => {
|
||||
if (status === "loading" && url) {
|
||||
|
||||
@@ -375,7 +375,7 @@ describe("BrowserApi", () => {
|
||||
describe("executeScriptInTab", () => {
|
||||
it("calls to the extension api to execute a script within the give tabId", async () => {
|
||||
const tabId = 1;
|
||||
const injectDetails = mock<chrome.tabs.InjectDetails>();
|
||||
const injectDetails = mock<chrome.extensionTypes.InjectDetails>();
|
||||
jest.spyOn(BrowserApi, "manifestVersion", "get").mockReturnValue(2);
|
||||
(chrome.tabs.executeScript as jest.Mock).mockImplementation(
|
||||
(tabId, injectDetails, callback) => callback(executeScriptResult),
|
||||
@@ -393,7 +393,7 @@ describe("BrowserApi", () => {
|
||||
|
||||
it("calls the manifest v3 scripting API if the extension manifest is for v3", async () => {
|
||||
const tabId = 1;
|
||||
const injectDetails = mock<chrome.tabs.InjectDetails>({
|
||||
const injectDetails = mock<chrome.extensionTypes.InjectDetails>({
|
||||
file: "file.js",
|
||||
allFrames: true,
|
||||
runAt: "document_start",
|
||||
@@ -419,7 +419,7 @@ describe("BrowserApi", () => {
|
||||
it("injects the script into a specified frameId when the extension is built for manifest v3", async () => {
|
||||
const tabId = 1;
|
||||
const frameId = 2;
|
||||
const injectDetails = mock<chrome.tabs.InjectDetails>({
|
||||
const injectDetails = mock<chrome.extensionTypes.InjectDetails>({
|
||||
file: "file.js",
|
||||
allFrames: true,
|
||||
runAt: "document_start",
|
||||
@@ -443,7 +443,7 @@ describe("BrowserApi", () => {
|
||||
|
||||
it("injects the script into the MAIN world context when injecting a script for manifest v3", async () => {
|
||||
const tabId = 1;
|
||||
const injectDetails = mock<chrome.tabs.InjectDetails>({
|
||||
const injectDetails = mock<chrome.extensionTypes.InjectDetails>({
|
||||
file: null,
|
||||
allFrames: true,
|
||||
runAt: "document_start",
|
||||
|
||||
@@ -685,29 +685,27 @@ export class BrowserApi {
|
||||
*/
|
||||
static executeScriptInTab(
|
||||
tabId: number,
|
||||
details: chrome.tabs.InjectDetails,
|
||||
details: chrome.extensionTypes.InjectDetails,
|
||||
scriptingApiDetails?: {
|
||||
world: chrome.scripting.ExecutionWorld;
|
||||
},
|
||||
): Promise<unknown> {
|
||||
if (BrowserApi.isManifestVersion(3)) {
|
||||
const target: chrome.scripting.InjectionTarget = {
|
||||
tabId,
|
||||
};
|
||||
let target: chrome.scripting.InjectionTarget;
|
||||
|
||||
if (typeof details.frameId === "number") {
|
||||
target.frameIds = [details.frameId];
|
||||
}
|
||||
|
||||
if (!target.frameIds?.length && details.allFrames) {
|
||||
target.allFrames = details.allFrames;
|
||||
target = { tabId, frameIds: [details.frameId] };
|
||||
} else if (details.allFrames) {
|
||||
target = { tabId, allFrames: true };
|
||||
} else {
|
||||
target = { tabId };
|
||||
}
|
||||
|
||||
return chrome.scripting.executeScript({
|
||||
target,
|
||||
files: details.file ? [details.file] : null,
|
||||
injectImmediately: details.runAt === "document_start",
|
||||
world: scriptingApiDetails?.world || "ISOLATED",
|
||||
world: scriptingApiDetails?.world || chrome.scripting.ExecutionWorld.ISOLATED,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ describe("ChromeStorageApiService", () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
chrome.runtime.lastError = undefined;
|
||||
(chrome.runtime.lastError as any) = undefined;
|
||||
});
|
||||
|
||||
it("uses `objToStore` to prepare a value for set", async () => {
|
||||
@@ -80,7 +80,7 @@ describe("ChromeStorageApiService", () => {
|
||||
|
||||
it("translates chrome.runtime.lastError to promise rejection", async () => {
|
||||
setMock.mockImplementation((data, callback) => {
|
||||
chrome.runtime.lastError = new Error("Test Error");
|
||||
(chrome.runtime.lastError as any) = new Error("Test Error");
|
||||
callback();
|
||||
});
|
||||
|
||||
@@ -101,7 +101,7 @@ describe("ChromeStorageApiService", () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
chrome.runtime.lastError = undefined;
|
||||
(chrome.runtime.lastError as any) = undefined;
|
||||
});
|
||||
|
||||
it("returns a stored value when it is serialized", async () => {
|
||||
@@ -132,9 +132,9 @@ describe("ChromeStorageApiService", () => {
|
||||
|
||||
it("translates chrome.runtime.lastError to promise rejection", async () => {
|
||||
getMock.mockImplementation((key, callback) => {
|
||||
chrome.runtime.lastError = new Error("Test Error");
|
||||
(chrome.runtime.lastError as any) = new Error("Test Error");
|
||||
callback();
|
||||
chrome.runtime.lastError = undefined;
|
||||
(chrome.runtime.lastError as any) = undefined;
|
||||
});
|
||||
|
||||
await expect(async () => await service.get("test")).rejects.toThrow("Test Error");
|
||||
|
||||
@@ -41,7 +41,10 @@ describe("ScriptInjectorService", () => {
|
||||
const mv2SpecificFile = "content/autofill-init-mv2.js";
|
||||
const mv2Details = { file: mv2SpecificFile };
|
||||
const mv3SpecificFile = "content/autofill-init-mv3.js";
|
||||
const mv3Details: Mv3ScriptInjectionDetails = { file: mv3SpecificFile, world: "MAIN" };
|
||||
const mv3Details: Mv3ScriptInjectionDetails = {
|
||||
file: mv3SpecificFile,
|
||||
world: chrome.scripting.ExecutionWorld.MAIN,
|
||||
};
|
||||
const sharedInjectDetails: CommonScriptInjectionDetails = {
|
||||
runAt: "document_start",
|
||||
};
|
||||
|
||||
@@ -63,7 +63,7 @@ export class BrowserScriptInjectorService extends ScriptInjectorService {
|
||||
if (BrowserApi.isManifestVersion(3)) {
|
||||
try {
|
||||
await BrowserApi.executeScriptInTab(tabId, injectionDetails, {
|
||||
world: mv3Details?.world ?? "ISOLATED",
|
||||
world: mv3Details?.world ?? chrome.scripting.ExecutionWorld.ISOLATED,
|
||||
});
|
||||
} catch (error) {
|
||||
// Swallow errors for host permissions, since this is believed to be a Manifest V3 Chrome bug
|
||||
@@ -112,9 +112,9 @@ export class BrowserScriptInjectorService extends ScriptInjectorService {
|
||||
private buildInjectionDetails(
|
||||
injectDetails: CommonScriptInjectionDetails,
|
||||
file: string,
|
||||
): chrome.tabs.InjectDetails {
|
||||
): chrome.extensionTypes.InjectDetails {
|
||||
const { frame, runAt } = injectDetails;
|
||||
const injectionDetails: chrome.tabs.InjectDetails = { file };
|
||||
const injectionDetails: chrome.extensionTypes.InjectDetails = { file };
|
||||
|
||||
if (runAt) {
|
||||
injectionDetails.runAt = runAt;
|
||||
|
||||
@@ -167,7 +167,7 @@ describe("Browser Utils Service", () => {
|
||||
|
||||
it("returns false if special error is sent", async () => {
|
||||
chrome.runtime.sendMessage = jest.fn().mockImplementation((message, callback) => {
|
||||
chrome.runtime.lastError = new Error(
|
||||
(chrome.runtime.lastError as any) = new Error(
|
||||
"Could not establish connection. Receiving end does not exist.",
|
||||
);
|
||||
callback(undefined);
|
||||
@@ -177,7 +177,7 @@ describe("Browser Utils Service", () => {
|
||||
|
||||
expect(isViewOpen).toBe(false);
|
||||
|
||||
chrome.runtime.lastError = null;
|
||||
(chrome.runtime.lastError as any) = null;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ export class BrowserSystemNotificationService implements SystemNotificationsServ
|
||||
return new Promise<string>((resolve) => {
|
||||
const deviceType: DeviceType = this.platformUtilsService.getDevice();
|
||||
|
||||
const options: chrome.notifications.NotificationOptions<true> = {
|
||||
const options: chrome.notifications.NotificationCreateOptions = {
|
||||
iconUrl: chrome.runtime.getURL("images/icon128.png"),
|
||||
message: createInfo.body,
|
||||
type: "basic",
|
||||
@@ -70,6 +70,7 @@ export class BrowserSystemNotificationService implements SystemNotificationsServ
|
||||
}
|
||||
|
||||
async clear(clearInfo: SystemNotificationClearInfo): Promise<undefined> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
chrome.notifications.clear(clearInfo.id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user