From 6e89242491336e95be45246e5d43a48d22271082 Mon Sep 17 00:00:00 2001 From: TrickyPR <23250792+trickypr@users.noreply.github.com> Date: Sat, 13 Apr 2024 17:30:19 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20reload=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/extensions/lib/parent/ext-tabs.js | 64 +++++++++++++++-------- apps/extensions/lib/schemaTypes/tabs.d.ts | 1 + apps/extensions/lib/schemas/tabs.json | 14 +++++ 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/apps/extensions/lib/parent/ext-tabs.js b/apps/extensions/lib/parent/ext-tabs.js index 9b394de..7051d95 100644 --- a/apps/extensions/lib/parent/ext-tabs.js +++ b/apps/extensions/lib/parent/ext-tabs.js @@ -84,31 +84,38 @@ this.tabs = class extends ExtensionAPIPersistent { getAPI(context) { const { extension } = context - return { - tabs: { - async get(tabId) { - const window = [ - ...lazy.WindowTracker.registeredWindows.values(), - ].find((window) => - window.windowTabs().some((tab) => tab.view.browserId === tabId), - ) - - if (!window) { - return Promise.reject({ - message: `Cannot find tab matching the id ${tabId}`, - }) - } + /** + * @param {number} tabId + */ + async function get(tabId) { + const window = [...lazy.WindowTracker.registeredWindows.values()].find( + (window) => + window.windowTabs().some((tab) => tab.view.browserId === tabId), + ) - const tab = window - .windowTabs() - .find((tab) => tab.view.browserId === tabId) + if (!window) { + return Promise.reject({ + message: `Cannot find tab matching the id ${tabId}`, + }) + } - if (!tab) { - return Promise.reject({ - message: `Cannot find tab matching the id ${tabId}`, - }) - } + const tab = window + .windowTabs() + .find((tab) => tab.view.browserId === tabId) + + if (!tab) { + return Promise.reject({ + message: `Cannot find tab matching the id ${tabId}`, + }) + } + return { tab, window } + } + + return { + tabs: { + async get(tabId) { + const { tab, window } = await get(tabId) return serialize(extension)([tab, window]) }, @@ -149,6 +156,19 @@ this.tabs = class extends ExtensionAPIPersistent { } }, + async reload(tabIds) { + if (typeof tabIds === 'number') { + const { tab } = await get(tabIds) + tab.view.browser.reload() + return + } + + for (const id of tabIds) { + const { tab } = await get(id) + tab.view.browser.reload() + } + }, + async update(tabId, updateProperties) { const windows = lazy.WindowTracker.registeredWindows.values() for (const window of windows) { diff --git a/apps/extensions/lib/schemaTypes/tabs.d.ts b/apps/extensions/lib/schemaTypes/tabs.d.ts index 643dfce..157f1ff 100644 --- a/apps/extensions/lib/schemaTypes/tabs.d.ts +++ b/apps/extensions/lib/schemaTypes/tabs.d.ts @@ -36,6 +36,7 @@ declare module tabs__tabs { get: (tabId: number) => Promise query: (queryInfo: QueryInfo) => Promise remove: (tabIds: number | number[]) => unknown + reload: (tabIds: number | number[]) => unknown update: (tabId: number, updateProperties: UpdateInfo) => unknown } } diff --git a/apps/extensions/lib/schemas/tabs.json b/apps/extensions/lib/schemas/tabs.json index 45063db..ad1a283 100644 --- a/apps/extensions/lib/schemas/tabs.json +++ b/apps/extensions/lib/schemas/tabs.json @@ -151,6 +151,20 @@ } ] }, + { + "name": "reload", + "type": "function", + "async": true, + "parameters": [ + { + "name": "tabIds", + "choices": [ + { "type": "integer" }, + { "type": "array", "items": { "type": "integer" } } + ] + } + ] + }, { "name": "update", "type": "function",