Last active
February 8, 2024 16:02
-
-
Save Juraj-Masiar/cadb8fa0a8f556a0ad06aa3e588a7bfc to your computer and use it in GitHub Desktop.
Polyfill for the missing browser.window API in the Firefox for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!browser.windows) browser.windows = {}; | |
if (!browser.windows.getCurrent) { | |
browser.windows.getCurrent = async (getInfo?: browser.windows.GetInfo): Promise<browser.windows.Window> => { | |
const tabs = await browser.tabs.query({}); | |
const activeTab = tabs.find(tab => tab.active); | |
return { | |
id: activeTab?.windowId ?? 1, | |
title: activeTab?.title ?? '', | |
focused: true, | |
alwaysOnTop: false, | |
incognito: !!browser.extension.inIncognitoContext, | |
...(getInfo?.populate ? {tabs: tabs} : {}), | |
state: 'maximized', | |
type: 'normal', | |
height: screen.availHeight, | |
width: screen.availWidth, | |
top: 0, | |
left: 0, | |
}; | |
}; | |
} | |
if (!browser.windows.getLastFocused) { | |
browser.windows.getLastFocused = async (getInfo?: browser.windows.GetInfo): Promise<browser.windows.Window> => browser.windows.getCurrent(getInfo); | |
} | |
if (!browser.windows.get) { | |
browser.windows.get = async (windowId: number, getInfo?: browser.windows.GetInfo): Promise<browser.windows.Window> => { | |
const [tab] = await browser.tabs.query({windowId: windowId}); | |
return { | |
...(await browser.windows.getCurrent(getInfo)), | |
id: tab?.windowId ?? 1, | |
title: tab?.title ?? '', | |
focused: tab?.active, | |
}; | |
}; | |
} | |
if (!browser.windows.getAll) { | |
browser.windows.getAll = async (getInfo?: browser.windows._GetAllGetInfo): Promise<browser.windows.Window[]> => { | |
return getInfo?.windowTypes?.includes('normal') !== false ? [await browser.windows.getCurrent(getInfo)] : []; | |
} | |
} | |
if (!browser.windows.update) { // we can emulate some window operations using tabs API (since each Tab is in own window) | |
browser.windows.update = async (windowId: number, updateInfo: browser.windows._UpdateUpdateInfo): Promise<browser.windows.Window> => { | |
if (updateInfo.focused || updateInfo.state === 'maximized') { | |
const [tab] = await browser.tabs.query({windowId: windowId}); | |
if (tab) await browser.tabs.update(tab.id!, {active: true}); | |
} | |
return browser.windows.getCurrent(); | |
}; | |
} | |
if (!browser.windows.remove) { | |
browser.windows.remove = async (windowId: number): Promise<void> => { | |
const [tab] = await browser.tabs.query({windowId: windowId}); | |
if (tab) await browser.tabs.remove(tab.id!); | |
}; | |
} | |
if (!browser.windows.create) { | |
browser.windows.create = async (createData?: browser.windows._CreateCreateData): Promise<browser.windows.Window> => { | |
const links = !createData?.url ? ['about:blank'] : Array.isArray(createData.url) ? createData.url : [createData.url]; | |
for (const url of links) { | |
await browser.tabs.create({url: url, active: createData?.focused}); | |
} | |
const win = await browser.windows.getCurrent({populate: true}); | |
win.tabs = win.tabs!.filter(tab => tab.windowId === win.id); // make sure the "tabs" property contains ONLY tabs from this window (which is always only one in Android) | |
return win; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment