-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 Possibly functional tab query api
- Loading branch information
Showing
14 changed files
with
564 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// @ts-check | ||
/// <reference path="../types/index.d.ts" /> | ||
/// <reference path="./ext-browser.js" /> | ||
/// <reference path="../schemaTypes/tabs.d.ts" /> | ||
|
||
/** | ||
* @param {tabs__tabs.QueryInfo} queryInfo | ||
* @returns {[import("@browser/tabs").WindowTab, Window][]} | ||
*/ | ||
function query(queryInfo) { | ||
const windows = [...lazy.WindowTracker.registeredWindows.entries()] | ||
|
||
const urlMatchSet = | ||
(queryInfo.url && | ||
(Array.isArray(queryInfo.url) | ||
? new MatchPatternSet(queryInfo.url) | ||
: new MatchPatternSet([queryInfo.url]))) || | ||
null | ||
|
||
return windows.flatMap(([windowId, window]) => { | ||
const tabs = window.windowTabs() | ||
const activeTab = window.activeTab() | ||
|
||
if ( | ||
typeof queryInfo.windowId !== 'undefined' && | ||
queryInfo.windowId != windowId | ||
) { | ||
return [] | ||
} | ||
|
||
return tabs | ||
.filter((tab) => { | ||
const active = | ||
typeof queryInfo.active !== 'undefined' | ||
? queryInfo.active | ||
? tab === activeTab | ||
: tab !== activeTab | ||
: true | ||
const title = queryInfo.title | ||
? queryInfo.title === tab.view.title | ||
: true | ||
const url = | ||
urlMatchSet === null | ||
? true | ||
: urlMatchSet.matches(tab.view.browser.browsingContext?.currentURI) | ||
|
||
return active && title && url | ||
}) | ||
.map( | ||
/** @returns {[import("@browser/tabs").WindowTab, Window]} */ (tab) => [ | ||
tab, | ||
window, | ||
], | ||
) | ||
}) | ||
} | ||
|
||
const serialize = | ||
(extension) => | ||
/** | ||
* @param {[import("@browser/tabs").WindowTab, Window]} in | ||
* @returns {tabs__tabs.Tab} | ||
*/ | ||
([tab, window]) => { | ||
// TODO: Active tab & host permissions | ||
const hasTabPermission = extension.hasPermission('tabs') | ||
|
||
return { | ||
id: tab.view.browserId, | ||
index: window.windowTabs().findIndex((wTab) => wTab === tab), | ||
active: window.activeTab() === tab, | ||
highlighted: false, // TODO | ||
title: hasTabPermission && tab.view.title, | ||
url: | ||
hasTabPermission && | ||
tab.view.browser.browsingContext?.currentURI.asciiSpec, | ||
windowId: window.windowId, | ||
} | ||
} | ||
|
||
this.tabs = class extends ExtensionAPIPersistent { | ||
PERSISTENT_EVENTS = {} | ||
|
||
/** | ||
* @returns {tabs__tabs.ApiGetterReturn} | ||
*/ | ||
getAPI(context) { | ||
const { extension } = context | ||
|
||
return { | ||
tabs: { | ||
async query(queryInfo) { | ||
return query(queryInfo).map(serialize(extension)) | ||
}, | ||
}, | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @not-mpl | ||
// This file is generated from '../schemas/tabs.json'. This file inherits its license | ||
// Please check that file's license | ||
// | ||
// DO NOT MODIFY MANUALLY | ||
|
||
declare module tabs__tabs { | ||
type Tab = { | ||
id?: number | ||
index: number | ||
active: boolean | ||
highlighted: boolean | ||
title?: string | ||
url?: string | ||
windowId: number | ||
} | ||
type QueryInfo = { | ||
active?: boolean | ||
title?: string | ||
url?: string | string[] | ||
windowId?: number | ||
} | ||
type TabStatus = 'loading' | 'complete' | ||
type ApiGetterReturn = { | ||
tabs: { | ||
query: (queryInfo: QueryInfo) => Promise<Tab[]> | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
[ | ||
{ | ||
"namespace": "tabs", | ||
"description": "Provides access to information about currently open tabs", | ||
"types": [ | ||
{ | ||
"id": "Tab", | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"minimum": -1, | ||
"optional": true | ||
}, | ||
"index": { | ||
"type": "integer", | ||
"minimum": -1 | ||
}, | ||
"active": { | ||
"type": "boolean" | ||
}, | ||
"highlighted": { | ||
"type": "boolean" | ||
}, | ||
"title": { | ||
"type": "string", | ||
"optional": true, | ||
"permissions": ["tabs"] | ||
}, | ||
"url": { | ||
"type": "string", | ||
"optional": true, | ||
"permissions": ["tabs"] | ||
}, | ||
"windowId": { | ||
"type": "integer" | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "object", | ||
"id": "QueryInfo", | ||
"properties": { | ||
"active": { | ||
"type": "boolean", | ||
"optional": true | ||
}, | ||
"title": { | ||
"type": "string", | ||
"optional": true | ||
}, | ||
"url": { | ||
"choices": [ | ||
{ "type": "string" }, | ||
{ "type": "array", "items": { "type": "string" } } | ||
], | ||
"optional": true | ||
}, | ||
"windowId": { | ||
"type": "integer", | ||
"optional": true | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "string", | ||
"id": "TabStatus", | ||
"enum": [{ "name": "loading" }, { "name": "complete" }] | ||
} | ||
], | ||
"functions": [ | ||
{ | ||
"name": "query", | ||
"type": "function", | ||
"async": true, | ||
"parameters": [ | ||
{ | ||
"name": "queryInfo", | ||
"$ref": "QueryInfo" | ||
} | ||
], | ||
"returns": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "Tab" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
{ | ||
"name": "extensions", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"build": "node ./scripts/buildTypes.js", | ||
"dev": "watch 'pnpm build' ./lib/schemas/" | ||
}, | ||
"dependencies": { | ||
"@browser/link": "workspace:*" | ||
}, | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"prettier": "^3.0.3", | ||
"typescript": "^5.4.5", | ||
"watch": "^1.0.2" | ||
} | ||
} |
Oops, something went wrong.