@yaks/extension 0.0.0-stage → 0.2.36

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.
Files changed (78) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +275 -2
  3. package/_dist/act.d.ts +12 -0
  4. package/_dist/act.d.ts.map +1 -0
  5. package/_dist/answer.d.ts +42 -0
  6. package/_dist/answer.d.ts.map +1 -0
  7. package/_dist/build.d.ts +3 -0
  8. package/_dist/build.d.ts.map +1 -0
  9. package/_dist/connect.d.ts +22 -0
  10. package/_dist/connect.d.ts.map +1 -0
  11. package/_dist/consent.d.ts +16 -0
  12. package/_dist/consent.d.ts.map +1 -0
  13. package/_dist/manifest.d.ts +22 -0
  14. package/_dist/manifest.d.ts.map +1 -0
  15. package/_dist/mod.d.ts +12 -0
  16. package/_dist/mod.d.ts.map +1 -0
  17. package/_dist/read.d.ts +11 -0
  18. package/_dist/read.d.ts.map +1 -0
  19. package/_dist/screen.d.ts +22 -0
  20. package/_dist/screen.d.ts.map +1 -0
  21. package/_dist/signin.d.ts +27 -0
  22. package/_dist/signin.d.ts.map +1 -0
  23. package/_dist/tools.d.ts +23 -0
  24. package/_dist/tools.d.ts.map +1 -0
  25. package/_dist/touch.d.ts +32 -0
  26. package/_dist/touch.d.ts.map +1 -0
  27. package/_dist/wire.d.ts +33 -0
  28. package/_dist/wire.d.ts.map +1 -0
  29. package/act.js +41 -0
  30. package/act.js.map +1 -0
  31. package/act.ts +67 -0
  32. package/answer.js +157 -0
  33. package/answer.js.map +1 -0
  34. package/answer.ts +269 -0
  35. package/background.ts +190 -0
  36. package/build.js +67 -0
  37. package/build.js.map +1 -0
  38. package/build.ts +71 -0
  39. package/chrome.ts +76 -0
  40. package/connect.js +59 -0
  41. package/connect.js.map +1 -0
  42. package/connect.ts +85 -0
  43. package/consent.js +50 -0
  44. package/consent.js.map +1 -0
  45. package/consent.ts +67 -0
  46. package/deno.json +1 -0
  47. package/give.ts +124 -0
  48. package/icon.js +60 -0
  49. package/icon.js.map +1 -0
  50. package/icon.ts +53 -0
  51. package/manifest.js +50 -0
  52. package/manifest.js.map +1 -0
  53. package/manifest.ts +53 -0
  54. package/mod.js +15 -0
  55. package/mod.js.map +1 -0
  56. package/mod.ts +15 -0
  57. package/package.json +36 -4
  58. package/popup.css +117 -0
  59. package/popup.html +5 -0
  60. package/popup.ts +89 -0
  61. package/read.js +61 -0
  62. package/read.js.map +1 -0
  63. package/read.ts +80 -0
  64. package/screen.js +41 -0
  65. package/screen.js.map +1 -0
  66. package/screen.ts +125 -0
  67. package/signin.js +77 -0
  68. package/signin.js.map +1 -0
  69. package/signin.ts +94 -0
  70. package/tools.js +183 -0
  71. package/tools.js.map +1 -0
  72. package/tools.ts +192 -0
  73. package/touch.js +216 -0
  74. package/touch.js.map +1 -0
  75. package/touch.ts +259 -0
  76. package/wire.js +66 -0
  77. package/wire.js.map +1 -0
  78. package/wire.ts +91 -0
package/mod.ts ADDED
@@ -0,0 +1,15 @@
1
+ // The generic core of a browser extension that lends the tab a person is on
2
+ // to their agent: everything here is pure, and the browser glue
3
+ // (background.ts, popup.ts) and the build (build.ts) are made of it.
4
+
5
+ export * from './wire.js'
6
+ export * from './tools.js'
7
+ export * from './read.js'
8
+ export * from './touch.js'
9
+ export * from './act.js'
10
+ export * from './answer.js'
11
+ export * from './consent.js'
12
+ export * from './connect.js'
13
+ export * from './signin.js'
14
+ export * from './manifest.js'
15
+ export * from './screen.js'
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@yaks/extension",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/extension",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./wire": {
12
+ "types": "./_dist/wire.d.ts",
13
+ "default": "./wire.js"
14
+ },
15
+ "./tools": {
16
+ "types": "./_dist/tools.d.ts",
17
+ "default": "./tools.js"
18
+ },
19
+ "./build": {
20
+ "types": "./_dist/build.d.ts",
21
+ "default": "./build.js"
22
+ }
23
+ },
24
+ "license": "Apache-2.0",
25
+ "dependencies": {
26
+ "@yaks/blob": "0.2.36",
27
+ "@yaks/fp": "0.2.36",
28
+ "@yaks/oauth": "0.2.36",
29
+ "@yaks/ui": "0.2.36",
30
+ "@types/react": "19.3.0",
31
+ "@types/react-dom": "19.3.0",
32
+ "esbuild": "0.28.1",
33
+ "react": "19.3.0",
34
+ "react-dom": "19.3.0"
35
+ },
36
+ "description": "A browser extension that lends a person's tab to their agent: reading and acting on the page with per-site consent, a Do it before any step that sends, buys, posts or deletes, and an unpacked Chrome extension built from a configuration.",
37
+ "repository": "https://github.com/yak-sh/yak.git"
38
+ }
package/popup.css ADDED
@@ -0,0 +1,117 @@
1
+ /* The popup: one column, the browser's own font, Everforest's colours. */
2
+ :root {
3
+ --bg: #fdf6e3;
4
+ --fg: #5c6a72;
5
+ --line: #e6e2cc;
6
+ --green: #8da101;
7
+ --red: #f85552;
8
+ --button: #2d353b;
9
+ color-scheme: light dark;
10
+ }
11
+ @media (prefers-color-scheme: dark) {
12
+ :root {
13
+ --bg: #2d353b;
14
+ --fg: #d3c6aa;
15
+ --line: #475258;
16
+ --green: #a7c080;
17
+ --red: #e67e80;
18
+ --button: #a7c080;
19
+ }
20
+ }
21
+ body {
22
+ margin: 0;
23
+ width: 300px;
24
+ background: var(--bg);
25
+ color: var(--fg);
26
+ font: 14px/1.4 system-ui, sans-serif;
27
+ }
28
+ main {
29
+ display: flex;
30
+ flex-direction: column;
31
+ gap: 12px;
32
+ padding: 16px;
33
+ }
34
+ h1 {
35
+ margin: 0;
36
+ font-size: 16px;
37
+ }
38
+ h2 {
39
+ margin: 0 0 4px;
40
+ font-size: 13px;
41
+ }
42
+ p {
43
+ margin: 0;
44
+ }
45
+ button {
46
+ padding: 6px 12px;
47
+ border: 0;
48
+ border-radius: 6px;
49
+ background: var(--button);
50
+ color: var(--bg);
51
+ font: inherit;
52
+ cursor: pointer;
53
+ }
54
+ form,
55
+ label {
56
+ display: flex;
57
+ flex-direction: column;
58
+ gap: 8px;
59
+ }
60
+ .space {
61
+ display: flex;
62
+ align-items: center;
63
+ gap: 2px;
64
+ }
65
+ input {
66
+ flex: 1;
67
+ min-width: 0;
68
+ padding: 6px;
69
+ border: 1px solid var(--line);
70
+ border-radius: 6px;
71
+ background: transparent;
72
+ color: inherit;
73
+ font: inherit;
74
+ }
75
+ .connected {
76
+ color: var(--green);
77
+ }
78
+ .error,
79
+ .offline {
80
+ color: var(--red);
81
+ }
82
+ .ask {
83
+ display: flex;
84
+ flex-wrap: wrap;
85
+ gap: 8px;
86
+ padding: 12px;
87
+ border: 1px solid var(--line);
88
+ border-radius: 8px;
89
+ }
90
+ .Ask_Text {
91
+ flex-basis: 100%;
92
+ }
93
+ .ask .doing {
94
+ font-weight: 600;
95
+ overflow-wrap: anywhere;
96
+ }
97
+ ul {
98
+ margin: 0;
99
+ padding: 0;
100
+ list-style: none;
101
+ }
102
+ li {
103
+ display: flex;
104
+ justify-content: space-between;
105
+ align-items: center;
106
+ padding: 4px 0;
107
+ }
108
+ li button,
109
+ footer button {
110
+ background: transparent;
111
+ color: inherit;
112
+ border: 1px solid var(--line);
113
+ }
114
+ footer {
115
+ display: flex;
116
+ gap: 8px;
117
+ }
package/popup.html ADDED
@@ -0,0 +1,5 @@
1
+ <!doctype html>
2
+ <meta charset="utf-8">
3
+ <link rel="stylesheet" href="popup.css">
4
+ <main></main>
5
+ <script type="module" src="popup.js"></script>
package/popup.ts ADDED
@@ -0,0 +1,89 @@
1
+ /// <reference types="./chrome" />
2
+ // The popup: it draws ./screen.ts from what storage holds, and runs what its
3
+ // buttons name. A site's permission is asked for here, because the browser
4
+ // grants one only during a click; signing in is the background's, because a
5
+ // sign-in window closes the popup. The build bundles it with a line that
6
+ // calls `popup` with the configuration.
7
+
8
+ import {
9
+ allow,
10
+ type Consent,
11
+ dismiss,
12
+ NONE,
13
+ pattern,
14
+ revoke,
15
+ } from './consent.ts'
16
+ import { type Config, spaceOf } from './connect.ts'
17
+ import { screen } from './screen.ts'
18
+ import { keep, said, stored } from './chrome.ts'
19
+
20
+ let send = (does: string) => chrome.runtime.sendMessage({ do: does })
21
+
22
+ let consent = async (change: (c: Consent) => Consent) =>
23
+ keep({ consent: change((await stored()).consent ?? NONE) })
24
+
25
+ // The person's answer to the step waiting on their Do it.
26
+ let decide = async (yes: boolean) => {
27
+ let { confirm } = await stored()
28
+ if (confirm) await keep({ confirm: { ...confirm, yes } })
29
+ }
30
+
31
+ export let popup = (config: Config): void => {
32
+ let main = document.querySelector('main')!
33
+ let draw = async () => {
34
+ let s = await stored()
35
+ main.innerHTML = screen(config, {
36
+ signed: !!s.grant?.access_token,
37
+ space: s.space,
38
+ link: s.link,
39
+ consent: s.consent ?? NONE,
40
+ lend: s.lend,
41
+ confirm: s.confirm?.yes == null ? s.confirm : undefined,
42
+ error: s.error,
43
+ })
44
+ }
45
+
46
+ // What each button does, given the site it names. A permission request must
47
+ // be the first thing a click does, before anything is awaited.
48
+ let DOES: Record<string, (site: string) => Promise<unknown>> = {
49
+ signin: () => send('signin'),
50
+ signout: () => send('signout'),
51
+ change: () => keep({ space: undefined }).then(() => send('connect')),
52
+ allow: async (site) => {
53
+ if (!await chrome.permissions.request({ origins: [pattern(site)] })) {
54
+ return
55
+ }
56
+ await consent(allow(site))
57
+ },
58
+ dismiss: () => consent(dismiss),
59
+ yes: () => decide(true),
60
+ no: () => decide(false),
61
+ revoke: (site) =>
62
+ chrome.permissions.remove({ origins: [pattern(site)] }).catch(() => {})
63
+ .then(() => consent(revoke(site))),
64
+ }
65
+
66
+ main.addEventListener('click', (e) => {
67
+ let b = e.target instanceof Element && e.target.closest('button[data-do]')
68
+ if (!(b instanceof HTMLButtonElement)) return
69
+ let does = DOES[b.dataset.do ?? '']
70
+ does?.(b.dataset.site ?? '').catch((e) => keep({ error: said(e) }))
71
+ })
72
+ main.addEventListener('submit', (e) => {
73
+ e.preventDefault()
74
+ let form = e.target instanceof HTMLFormElement ? e.target : undefined
75
+ let space = spaceOf(config, String(new FormData(form).get('space') ?? ''))
76
+ keep(
77
+ space ? { space, error: undefined } : {
78
+ error: `That isn't a space's name. It is the part before .${
79
+ new URL(config.origin).host
80
+ } in your space's address.`,
81
+ },
82
+ ).then(() => space && send('connect'))
83
+ })
84
+ chrome.storage.onChanged.addListener(draw)
85
+ // Opening the popup is a click on the extension's button, which lets
86
+ // Chrome take a screenshot of this tab: a screenshot waiting on it is
87
+ // taken again.
88
+ stored().then((s) => s.lend ? keep({ lend: undefined }) : draw())
89
+ }
package/read.js ADDED
@@ -0,0 +1,61 @@
1
+ // Reading the page a tab shows: its address, its title, its visible text and
2
+ // what the person selected, as one plain value a tool's answer is made from.
3
+ //
4
+ // `read` runs inside the page. The browser glue hands it to
5
+ // `chrome.scripting.executeScript({func: read})`, which sends the function's
6
+ // source text to the tab and calls it there with no arguments, so it closes
7
+ // over nothing in this module: every helper and limit lives inside it, and its
8
+ // defaults read the page's own `document`. Its result crosses back by
9
+ // structured clone, so it is strings only.
10
+ /** What a tab gave when its page was read. */ /** The page a document shows, read as a person sees it: scripts, styles and
11
+ * hidden elements skipped, whitespace collapsed to single spaces within a
12
+ * line, one line per block, and the text and selection each cut at `limit`
13
+ * characters with a note saying so. */ export let read = (doc = document, selected = doc.getSelection?.()?.toString() ?? '', limit = 50_000)=>{
14
+ let SKIP = /^(SCRIPT|STYLE|NOSCRIPT|TEMPLATE|IFRAME|OBJECT|EMBED|CANVAS|SVG|HEAD|SELECT|TEXTAREA|INPUT)$/;
15
+ let BLOCK = /^(ADDRESS|ARTICLE|ASIDE|BLOCKQUOTE|BR|DD|DETAILS|DIALOG|DIV|DL|DT|FIELDSET|FIGCAPTION|FIGURE|FOOTER|FORM|H[1-6]|HEADER|HR|LI|MAIN|NAV|OL|P|PRE|SECTION|SUMMARY|TABLE|TD|TH|TR|UL)$/;
16
+ let HIDDEN = /(^|;)\s*(display\s*:\s*none|visibility\s*:\s*hidden)/i;
17
+ let hidden = (el)=>el.hasAttribute('hidden') || el.getAttribute('aria-hidden') == 'true' || HIDDEN.test(el.getAttribute('style') ?? '') || // A browser knows what its stylesheets hid; a parsed document doesn't.
18
+ 'checkVisibility' in el && !el.checkVisibility({
19
+ visibilityProperty: true
20
+ });
21
+ let isEl = (n)=>n.nodeType == 1;
22
+ // The text so far, in parts. Whitespace is collapsed as it arrives, so a
23
+ // run that spans two text nodes still ends up one space, and a `pre` keeps
24
+ // its own. Parts, not one string, so a long page costs one join.
25
+ let parts = [];
26
+ let end = ()=>parts.at(-1)?.at(-1) ?? '\n';
27
+ let put = (t, pre)=>{
28
+ if (!pre) t = t.replace(/\s+/g, ' ');
29
+ if (!pre && (end() == ' ' || end() == '\n')) t = t.replace(/^ /, '');
30
+ if (t) parts.push(t);
31
+ };
32
+ let line = ()=>{
33
+ let last = parts.pop() ?? '';
34
+ if (last.endsWith(' ')) last = last.slice(0, -1);
35
+ if (last) parts.push(last);
36
+ if (end() != '\n') parts.push('\n');
37
+ };
38
+ let walk = (node, pre)=>{
39
+ if (node.nodeType == 3) return put(node.textContent ?? '', pre);
40
+ if (!isEl(node)) return;
41
+ let tag = node.tagName.toUpperCase();
42
+ if (SKIP.test(tag) || hidden(node)) return;
43
+ let block = BLOCK.test(tag);
44
+ if (block) line();
45
+ for (let child of Array.from(node.childNodes)){
46
+ walk(child, pre || tag == 'PRE');
47
+ }
48
+ if (block) line();
49
+ };
50
+ let note = (n, what)=>`\n[${what} is cut here, at ${limit} of its ${n} characters.]`;
51
+ let cut = (text, what)=>text.length <= limit ? text : text.slice(0, limit) + note(text.length, what);
52
+ walk(doc.body ?? doc.documentElement, false);
53
+ let text = parts.join('').trim();
54
+ return {
55
+ url: doc.location?.href ?? doc.URL ?? '',
56
+ title: (doc.title ?? '').replace(/\s+/g, ' ').trim(),
57
+ text: cut(text, 'The text'),
58
+ selection: cut(selected.trim(), 'The selection')
59
+ };
60
+ };
61
+ //# sourceMappingURL=read.js.map
package/read.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","sources":["./read.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6EAA6E;AAC7E,EAAE;AACF,4DAA4D;AAC5D,6EAA6E;AAC7E,4EAA4E;AAC5E,+EAA+E;AAC/E,sEAAsE;AACtE,2CAA2C;AAE3C,4CAA4C,GAQ5C;;;qCAGqC,GACrC,OAAO,IAAI,OAAO,CAChB,MAAgB,QAAQ,EACxB,WAAmB,IAAI,YAAY,MAAM,cAAc,EAAE,EACzD,QAAgB,MAAM;EAEtB,IAAI,OACF;EACF,IAAI,QACF;EACF,IAAI,SAAS;EACb,IAAI,SAAS,CAAC,KACZ,GAAG,YAAY,CAAC,aAAa,GAAG,YAAY,CAAC,kBAAkB,UAC/D,OAAO,IAAI,CAAC,GAAG,YAAY,CAAC,YAAY,OACxC,uEAAuE;IACtE,qBAAqB,MACpB,CAAC,GAAG,eAAe,CAAC;MAAE,oBAAoB;IAAK;EACnD,IAAI,OAAO,CAAC,IAA0B,EAAE,QAAQ,IAAI;EACpD,yEAAyE;EACzE,2EAA2E;EAC3E,iEAAiE;EACjE,IAAI,QAAkB,EAAE;EACxB,IAAI,MAAM,IAAM,MAAM,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,MAAM;EACxC,IAAI,MAAM,CAAC,GAAW;IACpB,IAAI,CAAC,KAAK,IAAI,EAAE,OAAO,CAAC,QAAQ;IAChC,IAAI,CAAC,OAAO,CAAC,SAAS,OAAO,SAAS,IAAI,GAAG,IAAI,EAAE,OAAO,CAAC,MAAM;IACjE,IAAI,GAAG,MAAM,IAAI,CAAC;EACpB;EACA,IAAI,OAAO;IACT,IAAI,OAAO,MAAM,GAAG,MAAM;IAC1B,IAAI,KAAK,QAAQ,CAAC,MAAM,OAAO,KAAK,KAAK,CAAC,GAAG,CAAC;IAC9C,IAAI,MAAM,MAAM,IAAI,CAAC;IACrB,IAAI,SAAS,MAAM,MAAM,IAAI,CAAC;EAChC;EACA,IAAI,OAAO,CAAC,MAAY;IACtB,IAAI,KAAK,QAAQ,IAAI,GAAG,OAAO,IAAI,KAAK,WAAW,IAAI,IAAI;IAC3D,IAAI,CAAC,KAAK,OAAO;IACjB,IAAI,MAAM,KAAK,OAAO,CAAC,WAAW;IAClC,IAAI,KAAK,IAAI,CAAC,QAAQ,OAAO,OAAO;IACpC,IAAI,QAAQ,MAAM,IAAI,CAAC;IACvB,IAAI,OAAO;IACX,KAAK,IAAI,SAAS,MAAM,IAAI,CAAC,KAAK,UAAU,EAAG;MAC7C,KAAK,OAAO,OAAO,OAAO;IAC5B;IACA,IAAI,OAAO;EACb;EACA,IAAI,OAAO,CAAC,GAAW,OACrB,CAAC,GAAG,EAAE,KAAK,iBAAiB,EAAE,MAAM,QAAQ,EAAE,EAAE,aAAa,CAAC;EAChE,IAAI,MAAM,CAAC,MAAc,OACvB,KAAK,MAAM,IAAI,QAAQ,OAAO,KAAK,KAAK,CAAC,GAAG,SAAS,KAAK,KAAK,MAAM,EAAE;EACzE,KAAK,IAAI,IAAI,IAAI,IAAI,eAAe,EAAE;EACtC,IAAI,OAAO,MAAM,IAAI,CAAC,IAAI,IAAI;EAC9B,OAAO;IACL,KAAK,IAAI,QAAQ,EAAE,QAAQ,IAAI,GAAG,IAAI;IACtC,OAAO,CAAC,IAAI,KAAK,IAAI,EAAE,EAAE,OAAO,CAAC,QAAQ,KAAK,IAAI;IAClD,MAAM,IAAI,MAAM;IAChB,WAAW,IAAI,SAAS,IAAI,IAAI;EAClC;AACF,EAAC"}
package/read.ts ADDED
@@ -0,0 +1,80 @@
1
+ // Reading the page a tab shows: its address, its title, its visible text and
2
+ // what the person selected, as one plain value a tool's answer is made from.
3
+ //
4
+ // `read` runs inside the page. The browser glue hands it to
5
+ // `chrome.scripting.executeScript({func: read})`, which sends the function's
6
+ // source text to the tab and calls it there with no arguments, so it closes
7
+ // over nothing in this module: every helper and limit lives inside it, and its
8
+ // defaults read the page's own `document`. Its result crosses back by
9
+ // structured clone, so it is strings only.
10
+
11
+ /** What a tab gave when its page was read. */
12
+ export type Page = {
13
+ url: string
14
+ title: string
15
+ text: string
16
+ selection: string
17
+ }
18
+
19
+ /** The page a document shows, read as a person sees it: scripts, styles and
20
+ * hidden elements skipped, whitespace collapsed to single spaces within a
21
+ * line, one line per block, and the text and selection each cut at `limit`
22
+ * characters with a note saying so. */
23
+ export let read = (
24
+ doc: Document = document,
25
+ selected: string = doc.getSelection?.()?.toString() ?? '',
26
+ limit: number = 50_000,
27
+ ): Page => {
28
+ let SKIP =
29
+ /^(SCRIPT|STYLE|NOSCRIPT|TEMPLATE|IFRAME|OBJECT|EMBED|CANVAS|SVG|HEAD|SELECT|TEXTAREA|INPUT)$/
30
+ let BLOCK =
31
+ /^(ADDRESS|ARTICLE|ASIDE|BLOCKQUOTE|BR|DD|DETAILS|DIALOG|DIV|DL|DT|FIELDSET|FIGCAPTION|FIGURE|FOOTER|FORM|H[1-6]|HEADER|HR|LI|MAIN|NAV|OL|P|PRE|SECTION|SUMMARY|TABLE|TD|TH|TR|UL)$/
32
+ let HIDDEN = /(^|;)\s*(display\s*:\s*none|visibility\s*:\s*hidden)/i
33
+ let hidden = (el: Element) =>
34
+ el.hasAttribute('hidden') || el.getAttribute('aria-hidden') == 'true' ||
35
+ HIDDEN.test(el.getAttribute('style') ?? '') ||
36
+ // A browser knows what its stylesheets hid; a parsed document doesn't.
37
+ ('checkVisibility' in el &&
38
+ !el.checkVisibility({ visibilityProperty: true }))
39
+ let isEl = (n: Node): n is Element => n.nodeType == 1
40
+ // The text so far, in parts. Whitespace is collapsed as it arrives, so a
41
+ // run that spans two text nodes still ends up one space, and a `pre` keeps
42
+ // its own. Parts, not one string, so a long page costs one join.
43
+ let parts: string[] = []
44
+ let end = () => parts.at(-1)?.at(-1) ?? '\n'
45
+ let put = (t: string, pre: boolean) => {
46
+ if (!pre) t = t.replace(/\s+/g, ' ')
47
+ if (!pre && (end() == ' ' || end() == '\n')) t = t.replace(/^ /, '')
48
+ if (t) parts.push(t)
49
+ }
50
+ let line = () => {
51
+ let last = parts.pop() ?? ''
52
+ if (last.endsWith(' ')) last = last.slice(0, -1)
53
+ if (last) parts.push(last)
54
+ if (end() != '\n') parts.push('\n')
55
+ }
56
+ let walk = (node: Node, pre: boolean): void => {
57
+ if (node.nodeType == 3) return put(node.textContent ?? '', pre)
58
+ if (!isEl(node)) return
59
+ let tag = node.tagName.toUpperCase()
60
+ if (SKIP.test(tag) || hidden(node)) return
61
+ let block = BLOCK.test(tag)
62
+ if (block) line()
63
+ for (let child of Array.from(node.childNodes)) {
64
+ walk(child, pre || tag == 'PRE')
65
+ }
66
+ if (block) line()
67
+ }
68
+ let note = (n: number, what: string) =>
69
+ `\n[${what} is cut here, at ${limit} of its ${n} characters.]`
70
+ let cut = (text: string, what: string) =>
71
+ text.length <= limit ? text : text.slice(0, limit) + note(text.length, what)
72
+ walk(doc.body ?? doc.documentElement, false)
73
+ let text = parts.join('').trim()
74
+ return {
75
+ url: doc.location?.href ?? doc.URL ?? '',
76
+ title: (doc.title ?? '').replace(/\s+/g, ' ').trim(),
77
+ text: cut(text, 'The text'),
78
+ selection: cut(selected.trim(), 'The selection'),
79
+ }
80
+ }
package/screen.js ADDED
@@ -0,0 +1,41 @@
1
+ // The popup, drawn from what the extension knows: whether the person signed
2
+ // in, the space they chose, how the socket is, and their consent. It is HTML
3
+ // text, so a test reads it on a parsed document. Each button names what it
4
+ // does in `data-do` (and a site in `data-site`); the popup's glue runs that.
5
+ import { Field } from "@yaks/ui/parts";
6
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h } from "react";
7
+ /* @ts-types="npm:@types/react-dom@19.3.0/server" */ import { renderToStaticMarkup as renderToString } from "react-dom/server";
8
+ import { spaceHost } from "./connect.js";
9
+ import { host } from "./consent.js";
10
+ let ENTITY = {
11
+ '&': '&amp;',
12
+ '<': '&lt;',
13
+ '>': '&gt;',
14
+ '"': '&quot;',
15
+ "'": '&#39;'
16
+ };
17
+ /// esc('<a href="x">') -> '&lt;a href=&quot;x&quot;&gt;'
18
+ /** Text as HTML. */ export let esc = (s)=>s.replace(/[&<>"']/g, (c)=>ENTITY[c]);
19
+ let button = (does, label, site)=>`<button data-do="${does}"${site ? ` data-site="${esc(site)}"` : ''}>${esc(label)}</button>`;
20
+ let LINKS = {
21
+ connecting: (at)=>`Connecting to ${at}…`,
22
+ connected: (at)=>`Connected to ${at}. Your agent there can ask about the page you're on.`,
23
+ offline: (at)=>`Can't reach ${at}. Trying again.`
24
+ };
25
+ let signin = (config)=>`<p>${esc(config.description ?? `Lets ${config.name} see the page you're on.`)}</p>${button('signin', `Sign in to ${config.name}`)}`;
26
+ let choose = (config)=>`<form data-do="space"><label>Your space<span class="space">` + renderToString(/*#__PURE__*/ h(Field, {
27
+ name: 'space',
28
+ placeholder: 'e.g. oak-street',
29
+ autocomplete: 'off',
30
+ required: true
31
+ })) + `<span>.${esc(new URL(config.origin).host)}</span></span></label>` + `<button>Connect</button></form>`;
32
+ let linked = (config, space, link)=>`<p class="link ${link}">${esc(LINKS[link](spaceHost(config, space)))}</p>`;
33
+ let asking = (site)=>`<section class="ask"><strong>Allow on ${esc(host(site))}?</strong>` + `<p class="Ask_Text">Your agent asked to use this site. It reads or does something on a page only when it asks.</p>` + `${button('allow', 'Allow', site)}${button('dismiss', 'Not now')}</section>`;
34
+ let lending = (site)=>`<section class="ask"><strong>Your agent wants a screenshot of ${esc(host(site))}</strong>` + `<p class="Ask_Text">Chrome lets it take one only after you click this extension's button on that tab. Go to the tab and click the button there.</p></section>`;
35
+ let confirming = (c)=>`<section class="ask"><strong>Do this on ${esc(host(c.site))}?</strong>` + `<p class="doing Ask_Text">${esc(c.doing)}</p>` + `<p class="Ask_Text">Your agent waits for you, and does nothing unless you press Do it.</p>` + `${button('yes', 'Do it')}${button('no', "Don't")}</section>`;
36
+ let sites = (c)=>c.sites.length ? `<section><h2>Sites it can read</h2><ul>${c.sites.map((s)=>`<li>${esc(host(s))}${button('revoke', 'Remove', s)}</li>`).join('')}</ul></section>` : '';
37
+ /** The popup's HTML for a state. */ export let screen = (config, s)=>`<h1>${esc(config.name)}</h1>` + (s.error ? `<p class="error">${esc(s.error)}</p>` : '') + (!s.signed ? signin(config) : !s.space ? choose(config) : linked(config, s.space, s.link ?? 'connecting') + (s.confirm ? confirming(s.confirm) : '') + (s.consent.asking ? asking(s.consent.asking) : '') + (s.lend ? lending(s.lend) : '') + sites(s.consent) + `<footer>${button('change', 'Change space')}${button('signout', 'Sign out')}</footer>`);
38
+ /// asked({ consent: { sites: [], asking: 'https://a.com' } }) -> true
39
+ /// asked({ consent: { sites: [] } }) -> false
40
+ /** Whether anything waits on the person, which the badge shows. */ export let asked = (s)=>!!(s.consent.asking || s.lend || s.confirm);
41
+ //# sourceMappingURL=screen.js.map
package/screen.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"screen.js","sources":["./screen.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,2EAA2E;AAC3E,6EAA6E;AAE7E,SAAS,KAAK,8BAAmC;AACjD,uCAAuC,GAAG,SAAS,iBAAiB,CAAC,gBAA0B;AAC/F,kDAAkD,GAAG,SAAS,wBAAwB,cAAc,2BAAqC;AAEzI,SAAS,SAAS,uBAAsB;AACxC,SAAuB,IAAI,uBAAsB;AAwBjD,IAAI,SAAiC;EACnC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;AACP;AAEA,yDAAyD;AACzD,kBAAkB,GAClB,OAAO,IAAI,MAAM,CAAC,IAAsB,EAAE,OAAO,CAAC,YAAY,CAAC,IAAM,MAAM,CAAC,EAAE,EAAC;AAE/E,IAAI,SAAS,CAAC,MAAc,OAAe,OACzC,CAAC,iBAAiB,EAAE,KAAK,CAAC,EAAE,OAAO,CAAC,YAAY,EAAE,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1,125 @@
1
+ // The popup, drawn from what the extension knows: whether the person signed
2
+ // in, the space they chose, how the socket is, and their consent. It is HTML
3
+ // text, so a test reads it on a parsed document. Each button names what it
4
+ // does in `data-do` (and a site in `data-site`); the popup's glue runs that.
5
+
6
+ import { Field } from '@yaks/ui/parts'
7
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h } from 'react'
8
+ /* @ts-types="npm:@types/react-dom@19.3.0/server" */ import { renderToStaticMarkup as renderToString } from 'react-dom/server'
9
+ import type { Config } from './connect.js'
10
+ import { spaceHost } from './connect.js'
11
+ import { type Consent, host } from './consent.js'
12
+
13
+ /** How the socket to the space is. */
14
+ export type Link = 'connecting' | 'connected' | 'offline'
15
+
16
+ /** A step the agent asks the person to let it do. */
17
+ export type Asked = { site: string; doing: string }
18
+
19
+ /** What the popup shows. */
20
+ export type State = {
21
+ signed: boolean
22
+ space?: string
23
+ link?: Link
24
+ consent: Consent
25
+ /** The site of a tab the agent wants a screenshot of, waiting on the
26
+ * person's click on the extension's button there. */
27
+ lend?: string
28
+ /** A step waiting on the person's Do it: the site it is on, and what it
29
+ * does, in words that may be the page's. */
30
+ confirm?: Asked
31
+ /** The last thing that went wrong, said to the person. */
32
+ error?: string
33
+ }
34
+
35
+ let ENTITY: Record<string, string> = {
36
+ '&': '&amp;',
37
+ '<': '&lt;',
38
+ '>': '&gt;',
39
+ '"': '&quot;',
40
+ "'": '&#39;',
41
+ }
42
+
43
+ /// esc('<a href="x">') -> '&lt;a href=&quot;x&quot;&gt;'
44
+ /** Text as HTML. */
45
+ export let esc = (s: string): string => s.replace(/[&<>"']/g, (c) => ENTITY[c])
46
+
47
+ let button = (does: string, label: string, site?: string) =>
48
+ `<button data-do="${does}"${site ? ` data-site="${esc(site)}"` : ''}>${
49
+ esc(label)
50
+ }</button>`
51
+
52
+ let LINKS: Record<Link, (at: string) => string> = {
53
+ connecting: (at) => `Connecting to ${at}…`,
54
+ connected: (at) =>
55
+ `Connected to ${at}. Your agent there can ask about the page you're on.`,
56
+ offline: (at) => `Can't reach ${at}. Trying again.`,
57
+ }
58
+
59
+ let signin = (config: Config) =>
60
+ `<p>${
61
+ esc(config.description ?? `Lets ${config.name} see the page you're on.`)
62
+ }</p>${button('signin', `Sign in to ${config.name}`)}`
63
+
64
+ let choose = (config: Config) =>
65
+ `<form data-do="space"><label>Your space<span class="space">` +
66
+ renderToString(h(Field, {
67
+ name: 'space',
68
+ placeholder: 'e.g. oak-street',
69
+ autocomplete: 'off',
70
+ required: true,
71
+ })) +
72
+ `<span>.${esc(new URL(config.origin).host)}</span></span></label>` +
73
+ `<button>Connect</button></form>`
74
+
75
+ let linked = (config: Config, space: string, link: Link) =>
76
+ `<p class="link ${link}">${esc(LINKS[link](spaceHost(config, space)))}</p>`
77
+
78
+ let asking = (site: string) =>
79
+ `<section class="ask"><strong>Allow on ${esc(host(site))}?</strong>` +
80
+ `<p class="Ask_Text">Your agent asked to use this site. It reads or does something on a page only when it asks.</p>` +
81
+ `${button('allow', 'Allow', site)}${button('dismiss', 'Not now')}</section>`
82
+
83
+ let lending = (site: string) =>
84
+ `<section class="ask"><strong>Your agent wants a screenshot of ${
85
+ esc(host(site))
86
+ }</strong>` +
87
+ `<p class="Ask_Text">Chrome lets it take one only after you click this extension's button on that tab. Go to the tab and click the button there.</p></section>`
88
+
89
+ let confirming = (c: Asked) =>
90
+ `<section class="ask"><strong>Do this on ${esc(host(c.site))}?</strong>` +
91
+ `<p class="doing Ask_Text">${esc(c.doing)}</p>` +
92
+ `<p class="Ask_Text">Your agent waits for you, and does nothing unless you press Do it.</p>` +
93
+ `${button('yes', 'Do it')}${button('no', "Don't")}</section>`
94
+
95
+ let sites = (c: Consent) =>
96
+ c.sites.length
97
+ ? `<section><h2>Sites it can read</h2><ul>${
98
+ c.sites.map((s) =>
99
+ `<li>${esc(host(s))}${button('revoke', 'Remove', s)}</li>`
100
+ ).join('')
101
+ }</ul></section>`
102
+ : ''
103
+
104
+ /** The popup's HTML for a state. */
105
+ export let screen = (config: Config, s: State): string =>
106
+ `<h1>${esc(config.name)}</h1>` +
107
+ (s.error ? `<p class="error">${esc(s.error)}</p>` : '') +
108
+ (!s.signed
109
+ ? signin(config)
110
+ : !s.space
111
+ ? choose(config)
112
+ : linked(config, s.space, s.link ?? 'connecting') +
113
+ (s.confirm ? confirming(s.confirm) : '') +
114
+ (s.consent.asking ? asking(s.consent.asking) : '') +
115
+ (s.lend ? lending(s.lend) : '') + sites(s.consent) +
116
+ `<footer>${button('change', 'Change space')}${
117
+ button('signout', 'Sign out')
118
+ }</footer>`)
119
+
120
+ /// asked({ consent: { sites: [], asking: 'https://a.com' } }) -> true
121
+ /// asked({ consent: { sites: [] } }) -> false
122
+ /** Whether anything waits on the person, which the badge shows. */
123
+ export let asked = (
124
+ s: Pick<State, 'consent' | 'lend' | 'confirm'>,
125
+ ): boolean => !!(s.consent.asking || s.lend || s.confirm)