Skip to content

Commit 7f3e933

Browse files
authored
✨ First pass tab api implementation (#57)
## APIs added - `tabs.query` (partial) ## Infra added - `apps/extensions/scripts/buildTypes.js`: Generates typescript definitions from the schema files to ensure everything is in sync ## Testing - Basic tests for `tabs.query`
1 parent a2cbb78 commit 7f3e933

File tree

24 files changed

+1406
-47
lines changed

24 files changed

+1406
-47
lines changed

apps/content/src/browser/browser.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
// @ts-check
55
import BrowserWindow from './BrowserWindow.svelte'
66
import './browser.css'
7+
import { browserImports } from './browserImports.js'
78
import * as WindowTabs from './windowApi/WindowTabs.js'
89
import { registerEventBus } from './windowApi/eventBus.js'
910

1011
// Handle window arguments
1112
let rawArgs = window.arguments && window.arguments[0]
12-
/** @type {Record<string, string>} */
13+
/** @type {Record<string, string | string[]>} */
1314
let args = {}
1415

1516
if (rawArgs && rawArgs instanceof Ci.nsISupports) {
@@ -18,12 +19,17 @@ if (rawArgs && rawArgs instanceof Ci.nsISupports) {
1819
args = rawArgs
1920
}
2021

21-
const initialUrls = args.initialUrl
22-
? [args.initialUrl]
22+
const initialUrls = args.initialUrls
23+
? args.initialUrls
2324
: ['https://google.com/', 'https://svelte.dev/']
2425

2526
WindowTabs.initialize(initialUrls)
2627

2728
registerEventBus()
2829

2930
new BrowserWindow({ target: document.body })
31+
32+
browserImports.WindowTracker.registerWindow(window)
33+
window.addEventListener('unload', () =>
34+
browserImports.WindowTracker.removeWindow(window),
35+
)

apps/content/src/browser/browserImports.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export const browserImports = lazyESModuleGetters({
1010
EPageActions: 'resource://app/modules/EPageActions.sys.mjs',
1111
NetUtil: 'resource://gre/modules/NetUtil.sys.mjs',
1212
PageThumbs: 'resource://gre/modules/PageThumbs.sys.mjs',
13+
WindowTracker: 'resource://app/modules/BrowserWindowTracker.sys.mjs',
1314
})

apps/content/src/browser/windowApi/WebsiteView.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import mitt from 'mitt'
88
import { readable } from 'svelte/store'
99

10+
import { browserImports } from '../browserImports.js'
1011
import { createBrowser } from '../utils/browserElement.js'
1112
import { eventBus } from './eventBus.js'
1213

@@ -31,6 +32,8 @@ export function create(uri) {
3132
const view = {
3233
windowBrowserId: nextWindowBrowserId++,
3334
browser: createBrowser(uri),
35+
uri,
36+
websiteState: 'loading',
3437

3538
/** @type {import('mitt').Emitter<WebsiteViewEvents>} */
3639
events: mitt(),
@@ -49,6 +52,13 @@ export function create(uri) {
4952
registerViewThemeListener(view)
5053
})
5154

55+
view.events.on('goTo', (e) => goTo(view, browserImports.NetUtil.newURI(e)))
56+
view.events.on('locationChange', (e) => (view.uri = e.aLocation))
57+
view.events.on(
58+
'loadingChange',
59+
(e) => (view.websiteState = e ? 'loading' : 'complete'),
60+
)
61+
5262
eventBus.on('iconUpdate', ({ browserId, iconUrl }) => {
5363
if (view.browser.browserId === browserId) {
5464
view.iconUrl = iconUrl
@@ -293,6 +303,7 @@ class TabProgressListener {
293303
* @returns {void}
294304
*/
295305
onLocationChange(aWebProgress, aRequest, aLocation, aFlags) {
306+
if (!aWebProgress || !aWebProgress.isTopLevel) return
296307
this.view.events.emit('locationChange', {
297308
aWebProgress,
298309
aRequest,

apps/content/src/browser/windowApi/WindowTabs.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
// @ts-check
5-
import { writable } from '@amadeus-it-group/tansu'
6-
import { derived } from 'svelte/store'
5+
import { derived, writable } from '@amadeus-it-group/tansu'
76

87
import { browserImports } from '../browserImports.js'
98
import * as WebsiteViewApi from './WebsiteView.js'
109

11-
/**
12-
* @typedef {object} WebsiteTab
13-
* @property {'website'} kind
14-
* @property {WebsiteView} view
15-
*/
16-
1710
export const activeTabId = writable(0)
1811

1912
/**
@@ -23,6 +16,9 @@ export const activeTabId = writable(0)
2316
*/
2417
export const selectedTabIds = writable([])
2518

19+
window.activeTabId = activeTabId
20+
window.selectedTabIds = selectedTabIds
21+
2622
/**
2723
* @param {number[]} ids
2824
*/
@@ -53,7 +49,7 @@ activeTabId.subscribe((activeId) => {
5349
})
5450

5551
/**
56-
* @type {import('@amadeus-it-group/tansu').WritableSignal<WebsiteTab[]>}
52+
* @type {import('@amadeus-it-group/tansu').WritableSignal<import('@browser/tabs').WindowTabs>}
5753
*/
5854
export const windowTabs = writable([])
5955

@@ -63,6 +59,9 @@ export const activeTab = derived(
6359
$windowTabs.find((tab) => tab.view.windowBrowserId === $activeTabId),
6460
)
6561

62+
window.windowTabs = windowTabs
63+
window.activeTab = activeTab
64+
6665
/**
6766
* @param {string[]} urls
6867
*/

apps/extensions/lib/ext-browser.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
"scopes": ["addon_parent"],
66
"manifest": ["page_action"],
77
"paths": [["pageAction"]]
8+
},
9+
"tabs": {
10+
"schema": "chrome://bextensions/content/schemas/tabs.json",
11+
"url": "chrome://bextensions/content/parent/ext-tabs.js",
12+
"scopes": ["addon_parent"],
13+
"paths": [["tabs"]]
814
}
915
}

apps/extensions/lib/parent/ext-pageAction.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ this.pageAction = class extends ExtensionAPIPersistent {
6868
*/
6969
onClicked({ fire }) {
7070
const callback = async (_name, clickInfo) => {
71-
console.log(fire, fire.wakeup, !!fire.wakeup)
7271
if (fire.wakeup) await fire.wakeup()
73-
console.log('fire')
7472
fire.sync(clickInfo)
7573
}
7674

0 commit comments

Comments
 (0)