@silkui/core 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@silkui/core",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "The node and event API of a Silk UI tree for JavaScript applications that run on the embedded SilkJS engine",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.d.ts CHANGED
@@ -849,6 +849,96 @@ export function removeTray(): void;
849
849
  * elsewhere) is pressed, whichever app has focus. Resolves to whether the system took it.
850
850
  */
851
851
  export function registerShortcut(accelerator: string, callback: () => void): Promise<boolean>;
852
+
853
+ /** An event of a {@link WebView}; its other members depend on `type`. */
854
+ export class WebViewEvent {
855
+ readonly type: string;
856
+ readonly url?: string;
857
+ readonly title?: string;
858
+ readonly ok?: boolean;
859
+ readonly error?: string;
860
+ readonly progress?: number;
861
+ readonly canGoBack?: boolean;
862
+ readonly canGoForward?: boolean;
863
+ readonly request?: number;
864
+ readonly userInitiated?: boolean;
865
+ readonly focused?: boolean;
866
+ readonly level?: "debug" | "log" | "info" | "warning" | "error";
867
+ readonly message?: string;
868
+ readonly command?: string;
869
+ readonly reason?: string;
870
+ }
871
+
872
+ export interface WebViewOptions {
873
+ url?: string;
874
+ /** A name keeps cookies and storage between runs; empty forgets them when the view closes. */
875
+ profile?: string;
876
+ /** Lets Web Inspector attach (default true). */
877
+ inspectable?: boolean;
878
+ /** Every main frame navigation waits for `answerNavigation`. */
879
+ askBeforeNavigating?: boolean;
880
+ /** The node's background shows until the page paints. */
881
+ transparent?: boolean;
882
+ }
883
+
884
+ export interface WebViewScreenshot {
885
+ width: number;
886
+ height: number;
887
+ /** RGBA, rows top first, device pixels. */
888
+ data: Uint8Array;
889
+ }
890
+
891
+ /** A real browser page shown over a node (WKWebView on macOS). */
892
+ export class WebView {
893
+ readonly id: number;
894
+ readonly node: Node;
895
+ readonly opened: Promise<boolean>;
896
+ url: string;
897
+ title: string;
898
+ favicon: string;
899
+ loading: boolean;
900
+ progress: number;
901
+ canGoBack: boolean;
902
+ canGoForward: boolean;
903
+ focused: boolean;
904
+ addEventListener(type: string, listener: (event: WebViewEvent) => void): void;
905
+ removeEventListener(type: string, listener: (event: WebViewEvent) => void): void;
906
+ navigate(url: string): void;
907
+ loadHtml(html: string, baseUrl?: string): void;
908
+ back(): void;
909
+ forward(): void;
910
+ reload(): void;
911
+ hardReload(): void;
912
+ stop(): void;
913
+ setZoom(factor: number): void;
914
+ focus(): void;
915
+ openDevTools(): void;
916
+ setAudioMuted(muted: boolean): void;
917
+ setColorScheme(scheme: "system" | "light" | "dark"): void;
918
+ setShown(shown: boolean): void;
919
+ evaluate(script: string): Promise<unknown>;
920
+ snapshot(): Promise<string>;
921
+ screenshot(): Promise<WebViewScreenshot>;
922
+ clickElement(ref: string): Promise<void>;
923
+ fillElement(ref: string, text: string): Promise<void>;
924
+ clickAt(x: number, y: number, options?: { button?: number; clickCount?: number; modifiers?: number }): void;
925
+ movePointer(x: number, y: number, options?: { modifiers?: number }): void;
926
+ scroll(x: number, y: number, deltaX: number, deltaY: number, options?: { modifiers?: number }): void;
927
+ typeText(text: string): void;
928
+ pressKey(key: string, modifiers?: number): void;
929
+ answerNavigation(request: number, allow: boolean): void;
930
+ findInPage(text: string, options?: { forward?: boolean; newSession?: boolean }): Promise<{ matches: number; activeMatchOrdinal: number }>;
931
+ stopFindInPage(): void;
932
+ close(): void;
933
+ }
934
+
935
+ /** Shows a web view over `node`; the page covers its border box and follows it. */
936
+ export function createWebView(node: Node, options?: WebViewOptions): WebView;
937
+
938
+ /** Clears what the pages of a profile stored: cookies and site storage, the cache, or both. */
939
+ export function clearWebViewData(options?: { profile?: string; cookies?: boolean; cache?: boolean }): Promise<void>;
940
+
941
+ export function quit(code?: number): void;
852
942
  export function unregisterShortcut(accelerator: string): void;
853
943
 
854
944
  /** An item of `setApplicationMenu` or `showContextMenu`, as Electron's menu templates. */
package/src/index.js CHANGED
@@ -4530,6 +4530,8 @@ native.shellEvent = function (kind, a, x, y, text) {
4530
4530
  if (a === appWindow.zoomFactor) return;
4531
4531
  appWindow.zoomFactor = a;
4532
4532
  appWindow._fire("zoomchange", { zoomFactor: a });
4533
+ } else if (kind === WEB_VIEW_EVENT) {
4534
+ webViewEvent(a, x, text);
4533
4535
  }
4534
4536
  };
4535
4537
 
@@ -4757,7 +4759,315 @@ export function registerShortcut(accelerator, callback) {
4757
4759
  return shellCall(3, [id, code, bits]).then((held) => held === 1);
4758
4760
  }
4759
4761
 
4762
+ // Web views: a real browser page (WKWebView on macOS) shown over a node, which a person and
4763
+ // an agent can both use. The views live on the platform thread; calls go through `__ui.shell`
4764
+ // (ops 10 to 14) and their events come back through `__ui.shellEvent` kind 20, with the view,
4765
+ // an event code and the fields separated by U+001F (ui-webview's wire.silk).
4766
+
4767
+ const WEB_VIEW_EVENT = 20;
4768
+ const webViews = new Map();
4769
+ let nextWebView = 0;
4770
+ let nextWebRequest = 0;
4771
+ const webRequests = new Map();
4772
+ const dataClears = new Map();
4773
+
4774
+ const thousandths = (value) => String(Math.round(Number(value) * 1000));
4775
+
4776
+ function webRequest(resolve, reject) {
4777
+ const request = ++nextWebRequest;
4778
+ webRequests.set(request, { resolve, reject });
4779
+ return request;
4780
+ }
4781
+
4782
+ function settleWebRequest(request, ok, value, error) {
4783
+ const waiting = webRequests.get(request);
4784
+ if (!waiting) return;
4785
+ webRequests.delete(request);
4786
+ if (ok) waiting.resolve(value);
4787
+ else waiting.reject(error instanceof Error ? error : new Error(String(error)));
4788
+ }
4789
+
4790
+ const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4791
+ const BASE64_VALUES = new Uint8Array(128);
4792
+ for (let i = 0; i < BASE64.length; i++) BASE64_VALUES[BASE64.charCodeAt(i)] = i;
4793
+
4794
+ function decodeBase64(text) {
4795
+ const clean = text.replace(/=+$/, "");
4796
+ const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
4797
+ let bits = 0;
4798
+ let count = 0;
4799
+ let at = 0;
4800
+ for (let i = 0; i < clean.length; i++) {
4801
+ bits = (bits << 6) | BASE64_VALUES[clean.charCodeAt(i)];
4802
+ count += 6;
4803
+ if (count >= 8) {
4804
+ count -= 8;
4805
+ out[at++] = (bits >> count) & 255;
4806
+ }
4807
+ }
4808
+ return out;
4809
+ }
4810
+
4811
+ const CONSOLE_LEVELS = ["debug", "log", "info", "warning", "error"];
4812
+
4813
+ /** An event of a {@link WebView}: its `type` and the fields of what happened. */
4814
+ export class WebViewEvent {
4815
+ constructor(type, fields) {
4816
+ this.type = type;
4817
+ Object.assign(this, fields);
4818
+ }
4819
+ }
4820
+
4821
+ /**
4822
+ * A web view: a real browser page shown over a node. The page draws over the node's box
4823
+ * (a native view of the system's engine), follows it as layout moves it, and hides when the
4824
+ * node is not shown. Make one with {@link createWebView}.
4825
+ *
4826
+ * The view keeps the page's state as it reports it: `url`, `title`, `favicon`, `loading`,
4827
+ * `progress`, `canGoBack`, `canGoForward`, `focused`. Listen with `addEventListener(type,
4828
+ * listener)` for `loadstart`, `loadprogress`, `loadend` (`ok`, `error`, `errorCode`, a Chromium net error), `urlchange`,
4829
+ * `titlechange`, `faviconchange`, `historychange`, `navigationrequest` (answer it with
4830
+ * `answerNavigation`), `newwindow`, `focuschange`, `console` (`level`, `message`),
4831
+ * `unsupported` (`command`), `crashed` and `closed`.
4832
+ */
4833
+ export class WebView {
4834
+ #listeners = new Map();
4835
+ #closed = false;
4836
+
4837
+ constructor(node, options) {
4838
+ this.id = ++nextWebView;
4839
+ this.node = node;
4840
+ this.url = options.url ?? "";
4841
+ this.title = "";
4842
+ this.favicon = "";
4843
+ this.loading = false;
4844
+ this.progress = 0;
4845
+ this.canGoBack = false;
4846
+ this.canGoForward = false;
4847
+ this.focused = false;
4848
+ webViews.set(this.id, this);
4849
+ const flags = (options.inspectable === false ? 0 : 1) | (options.askBeforeNavigating ? 2 : 0) | (options.transparent ? 4 : 0);
4850
+ /** Resolves to whether the view opened: there are web views only in a desktop window. */
4851
+ this.opened = shellCall(40, [this.id, node.nodeId, this.url, options.profile ?? "", flags]).then((value) => {
4852
+ if (value !== 1) webViews.delete(this.id);
4853
+ return value === 1;
4854
+ });
4855
+ }
4856
+
4857
+ addEventListener(type, listener) {
4858
+ let list = this.#listeners.get(type);
4859
+ if (!list) this.#listeners.set(type, (list = new Set()));
4860
+ list.add(listener);
4861
+ }
4862
+
4863
+ removeEventListener(type, listener) {
4864
+ this.#listeners.get(type)?.delete(listener);
4865
+ }
4866
+
4867
+ _fire(type, fields = {}) {
4868
+ const event = new WebViewEvent(type, fields);
4869
+ const handler = this["on" + type];
4870
+ if (typeof handler === "function") handler.call(this, event);
4871
+ for (const listener of this.#listeners.get(type) ?? []) listener.call(this, event);
4872
+ }
4873
+
4874
+ #command(name, ...fields) {
4875
+ if (this.#closed) return;
4876
+ shellCall(42, [this.id, name, ...fields]);
4877
+ }
4878
+
4879
+ #ask(name, ...fields) {
4880
+ if (this.#closed) return Promise.reject(new Error("the web view is closed"));
4881
+ return new Promise((resolve, reject) => {
4882
+ const request = webRequest(resolve, reject);
4883
+ this.#command(name, request, ...fields);
4884
+ });
4885
+ }
4886
+
4887
+ navigate(url) { this.url = String(url); this.#command("navigate", this.url); }
4888
+ /** Shows `html` as if loaded from `baseUrl`. */
4889
+ loadHtml(html, baseUrl = "") { this.#command("loadHtml", baseUrl, html); }
4890
+ back() { this.#command("back"); }
4891
+ forward() { this.#command("forward"); }
4892
+ reload() { this.#command("reload"); }
4893
+ /** Reloads bypassing the cache. */
4894
+ hardReload() { this.#command("hardReload"); }
4895
+ stop() { this.#command("stop"); }
4896
+ /** Page zoom, 1 for 100%. */
4897
+ setZoom(factor) { this.#command("zoom", thousandths(factor)); }
4898
+ /** Gives the page keyboard focus. */
4899
+ focus() { this.#command("focus"); }
4900
+ /** Opens the engine's developer tools (Web Inspector on macOS). */
4901
+ openDevTools() { this.#command("devtools"); }
4902
+ setAudioMuted(muted) { this.#command("mute", muted ? 1 : 0); }
4903
+ /** The color scheme the page sees: `"system"`, `"light"` or `"dark"`. */
4904
+ setColorScheme(scheme) { this.#command("scheme", scheme === "light" || scheme === "dark" ? scheme : "system"); }
4905
+ /** Shows or hides the view; a hidden view keeps its page. */
4906
+ setShown(shown) { if (!this.#closed) shellCall(44, [this.id, shown ? 1 : 0]); }
4907
+
4908
+ /** Runs `script` in the page; resolves to its value (a promise is awaited), JSON-cloned. */
4909
+ evaluate(script) {
4910
+ return this.#ask("evaluate", String(script)).then((json) => (json === "" ? undefined : JSON.parse(json)));
4911
+ }
4912
+
4913
+ /** A text outline of what the page shows, with refs (`[ref=e12]`) on what an agent can act on. */
4914
+ snapshot() { return this.#ask("snapshot"); }
4915
+
4916
+ /** The page's pixels: `{ width, height, data }`, RGBA rows top first, in device pixels. */
4917
+ screenshot() { return this.#ask("screenshot"); }
4918
+
4919
+ /** Scrolls element `ref` (a snapshot ref) into view and clicks it with real input. */
4920
+ clickElement(ref) { return this.#ask("click", String(ref)); }
4921
+
4922
+ /** Focuses element `ref`, selects its value and types `text` over it with real input. */
4923
+ fillElement(ref, text) { return this.#ask("fill", String(ref), String(text)); }
4924
+
4925
+ /** A click at a point of the page's viewport, in CSS pixels. */
4926
+ clickAt(x, y, { button = 0, clickCount = 1, modifiers = 0 } = {}) {
4927
+ this.#command("clickAt", thousandths(x), thousandths(y), button, clickCount, modifiers);
4928
+ }
4929
+
4930
+ movePointer(x, y, { modifiers = 0 } = {}) { this.#command("move", thousandths(x), thousandths(y), modifiers); }
4931
+
4932
+ /** A wheel turn over a point, deltas in CSS pixels. */
4933
+ scroll(x, y, deltaX, deltaY, { modifiers = 0 } = {}) {
4934
+ this.#command("scroll", thousandths(x), thousandths(y), thousandths(deltaX), thousandths(deltaY), modifiers);
4935
+ }
4936
+
4937
+ /** Types `text` into whatever has focus in the page. */
4938
+ typeText(text) { this.#command("type", String(text)); }
4939
+
4940
+ /** Presses one key, by its DOM `key` value (`"Enter"`, `"a"`); modifiers shift 1, control 2, alt 4, meta 8. */
4941
+ pressKey(key, modifiers = 0) { this.#command("key", modifiers, String(key)); }
4942
+
4943
+ /** Answers a `navigationrequest` event. */
4944
+ answerNavigation(request, allow) { this.#command("answer", request, allow ? 1 : 0); }
4945
+
4946
+ /**
4947
+ * Finds `text` in the page (case insensitive) and highlights the next match, or the previous
4948
+ * one when `forward` is false; resolves to `{ matches, activeMatchOrdinal }`.
4949
+ */
4950
+ findInPage(text, { forward = true, newSession = false } = {}) {
4951
+ return this.#ask("find", forward ? 1 : 0, newSession ? 1 : 0, String(text));
4952
+ }
4953
+
4954
+ stopFindInPage() { this.#command("stopFind"); }
4955
+
4956
+ /** Closes the view; its page goes. */
4957
+ close() {
4958
+ if (this.#closed) return;
4959
+ this.#closed = true;
4960
+ shellCall(41, [this.id]);
4961
+ }
4962
+ }
4963
+
4964
+ /**
4965
+ * Shows a web view over `node` (any node: the page covers its border box). Options: `url`,
4966
+ * `profile` (a name keeps cookies and storage between runs; empty forgets them),
4967
+ * `inspectable` (default true), `askBeforeNavigating`, `transparent`.
4968
+ */
4969
+ export function createWebView(node, options = {}) {
4970
+ return new WebView(node, options);
4971
+ }
4972
+
4973
+ /**
4974
+ * Clears what the pages of profile `profile` stored (an empty name: the views opened without
4975
+ * one): `cookies` (cookies and site storage) and `cache`.
4976
+ */
4977
+ export function clearWebViewData({ profile = "", cookies = true, cache = true } = {}) {
4978
+ return new Promise((resolve) => {
4979
+ const request = ++nextWebRequest;
4980
+ dataClears.set(request, resolve);
4981
+ shellCall(43, [request, cookies ? 1 : 0, cache ? 1 : 0, profile]);
4982
+ });
4983
+ }
4984
+
4985
+ function webViewEvent(id, code, text) {
4986
+ const parts = text === "" ? [] : text.split("\x1f");
4987
+ // The last field of each event may hold the separator itself.
4988
+ const rest = (from) => parts.slice(from).join("\x1f");
4989
+ if (code === 21) {
4990
+ const request = Number(parts[0]);
4991
+ const resolve = dataClears.get(request);
4992
+ dataClears.delete(request);
4993
+ resolve?.();
4994
+ return;
4995
+ }
4996
+ const view = webViews.get(id);
4997
+ if (!view) return;
4998
+ switch (code) {
4999
+ case 1: view._fire("created"); break;
5000
+ case 2:
5001
+ webViews.delete(id);
5002
+ view._fire("closed");
5003
+ break;
5004
+ case 3:
5005
+ view.loading = true;
5006
+ view._fire("loadstart", { url: rest(0) });
5007
+ break;
5008
+ case 4:
5009
+ view.progress = Number(parts[0]) / 1000;
5010
+ view._fire("loadprogress", { progress: view.progress });
5011
+ break;
5012
+ case 5:
5013
+ view.loading = false;
5014
+ view._fire("loadend", { ok: parts[0] === "1", errorCode: -Number(parts[1] ?? 0), url: parts[2] ?? "", error: rest(3) });
5015
+ break;
5016
+ case 6:
5017
+ view.url = rest(0);
5018
+ view._fire("urlchange", { url: view.url });
5019
+ break;
5020
+ case 7:
5021
+ view.title = rest(0);
5022
+ view._fire("titlechange", { title: view.title });
5023
+ break;
5024
+ case 8:
5025
+ view.canGoBack = parts[0] === "1";
5026
+ view.canGoForward = parts[1] === "1";
5027
+ view._fire("historychange", { canGoBack: view.canGoBack, canGoForward: view.canGoForward });
5028
+ break;
5029
+ case 9:
5030
+ view._fire("navigationrequest", { request: Number(parts[0]), userInitiated: parts[1] === "1", url: rest(2) });
5031
+ break;
5032
+ case 10: view._fire("newwindow", { url: rest(0) }); break;
5033
+ case 11:
5034
+ view.focused = parts[0] === "1";
5035
+ view._fire("focuschange", { focused: view.focused });
5036
+ break;
5037
+ case 12: settleWebRequest(Number(parts[0]), parts[1] === "1", rest(2), rest(2)); break;
5038
+ case 13: settleWebRequest(Number(parts[0]), true, rest(1)); break;
5039
+ case 14:
5040
+ settleWebRequest(Number(parts[0]), true, {
5041
+ width: Number(parts[1]),
5042
+ height: Number(parts[2]),
5043
+ data: decodeBase64(parts[3] ?? ""),
5044
+ });
5045
+ break;
5046
+ case 15: settleWebRequest(Number(parts[0]), parts[1] === "1", undefined, rest(2) || "the action failed"); break;
5047
+ case 16: view._fire("console", { level: CONSOLE_LEVELS[Number(parts[0])] ?? "log", message: rest(1) }); break;
5048
+ case 17: {
5049
+ const command = rest(1);
5050
+ if (Number(parts[0])) settleWebRequest(Number(parts[0]), false, undefined, command + " is not supported here");
5051
+ view._fire("unsupported", { command });
5052
+ break;
5053
+ }
5054
+ case 18: view._fire("crashed", { reason: rest(0) }); break;
5055
+ case 19:
5056
+ view.favicon = rest(0);
5057
+ view._fire("faviconchange", { url: view.favicon });
5058
+ break;
5059
+ case 20:
5060
+ settleWebRequest(Number(parts[0]), true, { matches: Number(parts[1]), activeMatchOrdinal: Number(parts[2]) });
5061
+ break;
5062
+ }
5063
+ }
5064
+
4760
5065
  /** Stops the shortcut of `accelerator`. */
5066
+ /** Ends the app with an exit code once the current turn finishes. */
5067
+ export function quit(code = 0) {
5068
+ shellCall(45, [String(code)]);
5069
+ }
5070
+
4761
5071
  export function unregisterShortcut(accelerator) {
4762
5072
  const { code, bits } = parseShortcut(accelerator);
4763
5073
  const id = shortcutIds.get(code + "+" + bits);