@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/connect.js ADDED
@@ -0,0 +1,59 @@
1
+ // The connection: which host an extension is made for, and the socket it holds
2
+ // to one space there once the person signed in. A **configuration** is the
3
+ // host as data (`{name, origin, socket, description?}`), the one thing two of
4
+ // these extensions differ by. A **space** is where the person's agent runs,
5
+ // served at `<space>.<host>`.
6
+ import { protocols } from "./wire.js";
7
+ /** The configuration a JSON value holds, or a sentence saying what is wrong
8
+ * with it. */ export let configOf = (v)=>{
9
+ if (!v || typeof v != 'object') return 'A configuration is a JSON object.';
10
+ let { name, origin, socket, description } = {
11
+ ...v
12
+ };
13
+ if (typeof name != 'string' || !name) return 'It needs a `name`.';
14
+ if (typeof origin != 'string' || !origin) return 'It needs an `origin`.';
15
+ let at;
16
+ try {
17
+ at = new URL(origin);
18
+ } catch {
19
+ return `Its origin, ${origin}, is not an address.`;
20
+ }
21
+ if (at.origin != origin) {
22
+ return `Its origin is written ${at.origin}, with no path.`;
23
+ }
24
+ if (typeof socket != 'string' || !/^wss?:\/\/.*\{space\}/.test(socket)) {
25
+ return 'It needs a `socket`, a ws(s) address with `{space}` in it.';
26
+ }
27
+ if (description != null && typeof description != 'string') {
28
+ return 'Its `description` is a sentence.';
29
+ }
30
+ return {
31
+ name,
32
+ origin,
33
+ socket,
34
+ ...description ? {
35
+ description
36
+ } : {}
37
+ };
38
+ };
39
+ let SLUG = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
40
+ /// let config = { name: 'yaks.app', origin: 'https://yaks.app' }
41
+ /// spaceOf(config, ' Jeff ') -> 'jeff'
42
+ /// spaceOf(config, 'https://jeff.yaks.app/notes') -> 'jeff'
43
+ /// spaceOf(config, 'jeff.yaks.app') -> 'jeff'
44
+ /// spaceOf(config, 'not a space!') -> null
45
+ /** The space a person typed, as its name or its address, or null when it
46
+ * names none. */ export let spaceOf = (config, typed)=>{
47
+ let suffix = `.${new URL(config.origin).hostname}`;
48
+ let t = typed.trim().toLowerCase().replace(/^[a-z]+:\/\//, '').replace(/[/?#].*$/, '');
49
+ let name = t.endsWith(suffix) ? t.slice(0, -suffix.length) : t;
50
+ return SLUG.test(name) ? name : null;
51
+ };
52
+ /// spaceHost({ name: 'yaks.app', origin: 'https://yaks.app' }, 'jeff') -> 'jeff.yaks.app'
53
+ /** Where a space is served. */ export let spaceHost = (config, space)=>`${space}.${new URL(config.origin).host}`;
54
+ /** The socket's address and subprotocols: the configured door for the
55
+ * person's space, with the bearer token in the subprotocols (./wire.ts). */ export let socket = (config, signed)=>({
56
+ url: config.socket.replaceAll('{space}', encodeURIComponent(signed.space)),
57
+ protocols: protocols(signed.token)
58
+ });
59
+ //# sourceMappingURL=connect.js.map
package/connect.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect.js","sources":["./connect.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,2EAA2E;AAC3E,8EAA8E;AAC9E,4EAA4E;AAC5E,8BAA8B;AAE9B,SAAS,SAAS,oBAAmB;AAkBrC;YACY,GACZ,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,CAAC,KAAK,OAAO,KAAK,UAAU,OAAO;EACvC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAA4B;IAAE,GAAG,CAAC;EAAC;EAC5E,IAAI,OAAO,QAAQ,YAAY,CAAC,MAAM,OAAO;EAC7C,IAAI,OAAO,UAAU,YAAY,CAAC,QAAQ,OAAO;EACjD,IAAI;EACJ,IAAI;IACF,KAAK,IAAI,IAAI;EACf,EAAE,OAAM;IACN,OAAO,CAAC,YAAY,EAAE,OAAO,oBAAoB,CAAC;EACpD;EACA,IAAI,GAAG,MAAM,IAAI,QAAQ;IACvB,OAAO,CAAC,sBAAsB,EAAE,GAAG,MAAM,CAAC,eAAe,CAAC;EAC5D;EACA,IAAI,OAAO,UAAU,YAAY,CAAC,wBAAwB,IAAI,CAAC,SAAS;IACtE,OAAO;EACT;EACA,IAAI,eAAe,QAAQ,OAAO,eAAe,UAAU;IACzD,OAAO;EACT;EACA,OAAO;IAAE;IAAM;IAAQ;IAAQ,GAAI,cAAc;MAAE;IAAY,IAAI,CAAC,CAAC;EAAE;AACzE,EAAC;AAED,IAAI,OAAO;AAEX,iEAAiE;AACjE,uCAAuC;AACvC,4DAA4D;AAC5D,8CAA8C;AAC9C,2CAA2C;AAC3C;eACe,GACf,OAAO,IAAI,UAAU,CACnB,QACA;EAEA,IAAI,SAAS,CAAC,CAAC,EAAE,IAAI,IAAI,OAAO,MAAM,EAAE,QAAQ,EAAE;EAClD,IAAI,IAAI,MAAM,IAAI,GAAG,WAAW,GAAG,OAAO,CAAC,gBAAgB,IACxD,OAAO,CAAC,YAAY;EACvB,IAAI,OAAO,EAAE,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,OAAO,MAAM,IAAI;EAC7D,OAAO,KAAK,IAAI,CAAC,QAAQ,OAAO;AAClC,EAAC;AAED,0FAA0F;AAC1F,6BAA6B,GAC7B,OAAO,IAAI,YAAY,CACrB,QACA,QACW,GAAG,MAAM,CAAC,EAAE,IAAI,IAAI,OAAO,MAAM,EAAE,IAAI,EAAE,CAAA;AAEtD;0EAC0E,GAC1E,OAAO,IAAI,SAAS,CAClB,QACA,SACyC,CAAC;IAC1C,KAAK,OAAO,MAAM,CAAC,UAAU,CAAC,WAAW,mBAAmB,OAAO,KAAK;IACxE,WAAW,UAAU,OAAO,KAAK;EACnC,CAAC,EAAC"}
package/connect.ts ADDED
@@ -0,0 +1,85 @@
1
+ // The connection: which host an extension is made for, and the socket it holds
2
+ // to one space there once the person signed in. A **configuration** is the
3
+ // host as data (`{name, origin, socket, description?}`), the one thing two of
4
+ // these extensions differ by. A **space** is where the person's agent runs,
5
+ // served at `<space>.<host>`.
6
+
7
+ import { protocols } from './wire.js'
8
+
9
+ /** The host an extension is made for. */
10
+ export type Config = {
11
+ /** What the person knows the host as, such as `yaks.app`. */
12
+ name: string
13
+ /** Where it signs in, such as `https://yaks.app`. */
14
+ origin: string
15
+ /** The socket a space's agent is reached on, `{space}` standing for the
16
+ * space, such as `wss://yaks.app/manage/{space}/operator?browser=1`. */
17
+ socket: string
18
+ /** One line saying what the extension does, for the browser's listing. */
19
+ description?: string
20
+ }
21
+
22
+ /** A person signed in, with the space they chose. */
23
+ export type Signed = { token: string; space: string }
24
+
25
+ /** The configuration a JSON value holds, or a sentence saying what is wrong
26
+ * with it. */
27
+ export let configOf = (v: unknown): Config | string => {
28
+ if (!v || typeof v != 'object') return 'A configuration is a JSON object.'
29
+ let { name, origin, socket, description }: Record<string, unknown> = { ...v }
30
+ if (typeof name != 'string' || !name) return 'It needs a `name`.'
31
+ if (typeof origin != 'string' || !origin) return 'It needs an `origin`.'
32
+ let at: URL
33
+ try {
34
+ at = new URL(origin)
35
+ } catch {
36
+ return `Its origin, ${origin}, is not an address.`
37
+ }
38
+ if (at.origin != origin) {
39
+ return `Its origin is written ${at.origin}, with no path.`
40
+ }
41
+ if (typeof socket != 'string' || !/^wss?:\/\/.*\{space\}/.test(socket)) {
42
+ return 'It needs a `socket`, a ws(s) address with `{space}` in it.'
43
+ }
44
+ if (description != null && typeof description != 'string') {
45
+ return 'Its `description` is a sentence.'
46
+ }
47
+ return { name, origin, socket, ...(description ? { description } : {}) }
48
+ }
49
+
50
+ let SLUG = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/
51
+
52
+ /// let config = { name: 'yaks.app', origin: 'https://yaks.app' }
53
+ /// spaceOf(config, ' Jeff ') -> 'jeff'
54
+ /// spaceOf(config, 'https://jeff.yaks.app/notes') -> 'jeff'
55
+ /// spaceOf(config, 'jeff.yaks.app') -> 'jeff'
56
+ /// spaceOf(config, 'not a space!') -> null
57
+ /** The space a person typed, as its name or its address, or null when it
58
+ * names none. */
59
+ export let spaceOf = (
60
+ config: Pick<Config, 'origin'>,
61
+ typed: string,
62
+ ): string | null => {
63
+ let suffix = `.${new URL(config.origin).hostname}`
64
+ let t = typed.trim().toLowerCase().replace(/^[a-z]+:\/\//, '')
65
+ .replace(/[/?#].*$/, '')
66
+ let name = t.endsWith(suffix) ? t.slice(0, -suffix.length) : t
67
+ return SLUG.test(name) ? name : null
68
+ }
69
+
70
+ /// spaceHost({ name: 'yaks.app', origin: 'https://yaks.app' }, 'jeff') -> 'jeff.yaks.app'
71
+ /** Where a space is served. */
72
+ export let spaceHost = (
73
+ config: Pick<Config, 'origin'>,
74
+ space: string,
75
+ ): string => `${space}.${new URL(config.origin).host}`
76
+
77
+ /** The socket's address and subprotocols: the configured door for the
78
+ * person's space, with the bearer token in the subprotocols (./wire.ts). */
79
+ export let socket = (
80
+ config: Config,
81
+ signed: Signed,
82
+ ): { url: string; protocols: string[] } => ({
83
+ url: config.socket.replaceAll('{space}', encodeURIComponent(signed.space)),
84
+ protocols: protocols(signed.token),
85
+ })
package/consent.js ADDED
@@ -0,0 +1,50 @@
1
+ // Per-site consent: which sites the person let their agent read, and the one
2
+ // it is asking about now. A **site** is a page's origin, `https://example.com`.
3
+ // Nothing is read from a site until the person allows it, and they can take it
4
+ // back. The value is plain JSON, which the browser glue keeps in
5
+ // `chrome.storage.local`; a site's permission itself is the browser's
6
+ // (`chrome.permissions`), asked for with `pattern`.
7
+ /** The sites allowed, and the site the agent is waiting on, if any. */ /** Nothing allowed, nothing asked. */ export let NONE = {
8
+ sites: []
9
+ };
10
+ /// site('https://example.com/a?b#c') -> 'https://example.com'
11
+ /// site('chrome://newtab/') -> null
12
+ /// site('not a url') -> null
13
+ /** The site a page's address is on, or null for a page that is not on the
14
+ * web (the browser's own pages, files, extensions). */ export let site = (url)=>{
15
+ try {
16
+ let u = new URL(url);
17
+ return u.protocol == 'https:' || u.protocol == 'http:' ? u.origin : null;
18
+ } catch {
19
+ return null;
20
+ }
21
+ };
22
+ /// host('https://example.com') -> 'example.com'
23
+ /** A site as a person reads it. */ export let host = (site)=>new URL(site).host;
24
+ /// pattern('https://example.com') -> 'https://example.com/*'
25
+ /** The match pattern the browser grants a site's permission under. */ export let pattern = (site)=>`${site}/*`;
26
+ /// allowed('https://a.com')({ sites: ['https://a.com'] }) -> true
27
+ /// allowed('https://b.com')({ sites: ['https://a.com'] }) -> false
28
+ /** Whether the person allowed a site. */ export let allowed = (site)=>(c)=>c.sites.includes(site);
29
+ // Leaves `asking` out rather than undefined, so the value stays as stored.
30
+ let shaped = (sites, asking)=>asking ? {
31
+ sites,
32
+ asking
33
+ } : {
34
+ sites
35
+ };
36
+ /// ask('https://a.com')(NONE) -> { sites: [], asking: 'https://a.com' }
37
+ /** The agent asked about a site: it is the one the person is asked about,
38
+ * unless they already allowed it. */ export let ask = (site)=>(c)=>allowed(site)(c) ? c : shaped(c.sites, site);
39
+ /// allow('https://a.com')({ sites: [], asking: 'https://a.com' }) -> { sites: ['https://a.com'] }
40
+ /** The person allowed a site; a question about it is answered. */ export let allow = (site)=>(c)=>shaped([
41
+ ...new Set([
42
+ ...c.sites,
43
+ site
44
+ ])
45
+ ].sort(), c.asking == site ? undefined : c.asking);
46
+ /// revoke('https://a.com')({ sites: ['https://a.com', 'https://b.com'] }) -> { sites: ['https://b.com'] }
47
+ /** The person took a site back. */ export let revoke = (site)=>(c)=>shaped(c.sites.filter((s)=>s != site), c.asking);
48
+ /// dismiss({ sites: [], asking: 'https://a.com' }) -> { sites: [] }
49
+ /** The person said not now: nothing is asked until the agent asks again. */ export let dismiss = (c)=>shaped(c.sites);
50
+ //# sourceMappingURL=consent.js.map
package/consent.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent.js","sources":["./consent.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,gFAAgF;AAChF,+EAA+E;AAC/E,iEAAiE;AACjE,sEAAsE;AACtE,oDAAoD;AAEpD,qEAAqE,GAGrE,oCAAoC,GACpC,OAAO,IAAI,OAAgB;EAAE,OAAO,EAAE;AAAC,EAAC;AAExC,8DAA8D;AAC9D,oCAAoC;AACpC,6BAA6B;AAC7B;qDACqD,GACrD,OAAO,IAAI,OAAO,CAAC;EACjB,IAAI;IACF,IAAI,IAAI,IAAI,IAAI;IAChB,OAAO,EAAE,QAAQ,IAAI,YAAY,EAAE,QAAQ,IAAI,UAAU,EAAE,MAAM,GAAG;EACtE,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC;AAED,gDAAgD;AAChD,iCAAiC,GACjC,OAAO,IAAI,OAAO,CAAC,OAAyB,IAAI,IAAI,MAAM,IAAI,CAAA;AAE9D,6DAA6D;AAC7D,oEAAoE,GACpE,OAAO,IAAI,UAAU,CAAC,OAAyB,GAAG,KAAK,EAAE,CAAC,CAAA;AAE1D,kEAAkE;AAClE,mEAAmE;AACnE,uCAAuC,GACvC,OAAO,IAAI,UAAU,CAAC,OAAiB,CAAC,IACtC,EAAE,KAAK,CAAC,QAAQ,CAAC,MAAK;AAExB,2EAA2E;AAC3E,IAAI,SAAS,CAAC,OAAiB,SAC7B,SAAS;IAAE;IAAO;EAAO,IAAI;IAAE;EAAM;AAEvC,wEAAwE;AACxE;mCACmC,GACnC,OAAO,IAAI,MAAM,CAAC,OAAiB,CAAC,IAClC,QAAQ,MAAM,KAAK,IAAI,OAAO,EAAE,KAAK,EAAE,MAAK;AAE9C,kGAAkG;AAClG,gEAAgE,GAChE,OAAO,IAAI,QAAQ,CAAC,OAAiB,CAAC,IACpC,OACE;SAAI,IAAI,IAAI;WAAI,EAAE,KAAK;QAAE;OAAK;KAAE,CAAC,IAAI,IACrC,EAAE,MAAM,IAAI,OAAO,YAAY,EAAE,MAAM,EACxC;AAEH,0GAA0G;AAC1G,iCAAiC,GACjC,OAAO,IAAI,SAAS,CAAC,OAAiB,CAAC,IACrC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,IAAM,KAAK,OAAO,EAAE,MAAM,EAAC;AAEpD,oEAAoE;AACpE,0EAA0E,GAC1E,OAAO,IAAI,UAAU,CAAC,IAAwB,OAAO,EAAE,KAAK,EAAC"}
package/consent.ts ADDED
@@ -0,0 +1,67 @@
1
+ // Per-site consent: which sites the person let their agent read, and the one
2
+ // it is asking about now. A **site** is a page's origin, `https://example.com`.
3
+ // Nothing is read from a site until the person allows it, and they can take it
4
+ // back. The value is plain JSON, which the browser glue keeps in
5
+ // `chrome.storage.local`; a site's permission itself is the browser's
6
+ // (`chrome.permissions`), asked for with `pattern`.
7
+
8
+ /** The sites allowed, and the site the agent is waiting on, if any. */
9
+ export type Consent = { sites: string[]; asking?: string }
10
+
11
+ /** Nothing allowed, nothing asked. */
12
+ export let NONE: Consent = { sites: [] }
13
+
14
+ /// site('https://example.com/a?b#c') -> 'https://example.com'
15
+ /// site('chrome://newtab/') -> null
16
+ /// site('not a url') -> null
17
+ /** The site a page's address is on, or null for a page that is not on the
18
+ * web (the browser's own pages, files, extensions). */
19
+ export let site = (url: string): string | null => {
20
+ try {
21
+ let u = new URL(url)
22
+ return u.protocol == 'https:' || u.protocol == 'http:' ? u.origin : null
23
+ } catch {
24
+ return null
25
+ }
26
+ }
27
+
28
+ /// host('https://example.com') -> 'example.com'
29
+ /** A site as a person reads it. */
30
+ export let host = (site: string): string => new URL(site).host
31
+
32
+ /// pattern('https://example.com') -> 'https://example.com/*'
33
+ /** The match pattern the browser grants a site's permission under. */
34
+ export let pattern = (site: string): string => `${site}/*`
35
+
36
+ /// allowed('https://a.com')({ sites: ['https://a.com'] }) -> true
37
+ /// allowed('https://b.com')({ sites: ['https://a.com'] }) -> false
38
+ /** Whether the person allowed a site. */
39
+ export let allowed = (site: string) => (c: Consent): boolean =>
40
+ c.sites.includes(site)
41
+
42
+ // Leaves `asking` out rather than undefined, so the value stays as stored.
43
+ let shaped = (sites: string[], asking?: string): Consent =>
44
+ asking ? { sites, asking } : { sites }
45
+
46
+ /// ask('https://a.com')(NONE) -> { sites: [], asking: 'https://a.com' }
47
+ /** The agent asked about a site: it is the one the person is asked about,
48
+ * unless they already allowed it. */
49
+ export let ask = (site: string) => (c: Consent): Consent =>
50
+ allowed(site)(c) ? c : shaped(c.sites, site)
51
+
52
+ /// allow('https://a.com')({ sites: [], asking: 'https://a.com' }) -> { sites: ['https://a.com'] }
53
+ /** The person allowed a site; a question about it is answered. */
54
+ export let allow = (site: string) => (c: Consent): Consent =>
55
+ shaped(
56
+ [...new Set([...c.sites, site])].sort(),
57
+ c.asking == site ? undefined : c.asking,
58
+ )
59
+
60
+ /// revoke('https://a.com')({ sites: ['https://a.com', 'https://b.com'] }) -> { sites: ['https://b.com'] }
61
+ /** The person took a site back. */
62
+ export let revoke = (site: string) => (c: Consent): Consent =>
63
+ shaped(c.sites.filter((s) => s != site), c.asking)
64
+
65
+ /// dismiss({ sites: [], asking: 'https://a.com' }) -> { sites: [] }
66
+ /** The person said not now: nothing is asked until the agent asks again. */
67
+ export let dismiss = (c: Consent): Consent => shaped(c.sites)
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/extension","version":"0.2.36","license":"Apache-2.0","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.","exports":{".":"./mod.ts","./wire":"./wire.ts","./tools":"./tools.ts","./build":"./build.ts"},"imports":{"@yaks/extension":"./mod.ts","@yaks/extension/wire":"./wire.ts","@yaks/extension/tools":"./tools.ts","@yaks/extension/build":"./build.ts"}}
package/give.ts ADDED
@@ -0,0 +1,124 @@
1
+ /// <reference types="./chrome" />
2
+ // What each need asks of the tab, in the browser (./answer.ts `Need`): the
3
+ // page read, a screenshot, its controls, a step on one, a navigation. Each
4
+ // decision is a pure module's (./act.ts, ./answer.ts); this file only reaches
5
+ // the browser, and the person, through the popup.
6
+
7
+ import type { Answered, Need, Seen } from './answer.ts'
8
+ import { confirms, doing, named } from './act.ts'
9
+ import { allowed, NONE, site } from './consent.ts'
10
+ import { read } from './read.ts'
11
+ import { type Touch, touch, type Touched } from './touch.ts'
12
+ import type { Control } from './touch.ts'
13
+ import type { Call } from './wire.ts'
14
+ import { keep, stored, until } from './chrome.ts'
15
+
16
+ /** The tab a call is answered from. */
17
+ export type Tab = { id: number; windowId: number; url: string }
18
+
19
+ type Give = (call: Call['call'], tab: Tab, by: number) => Promise<Partial<Seen>>
20
+
21
+ // How long a step waits for the page it started to load, in milliseconds.
22
+ let LOAD = 10_000
23
+
24
+ let wait = (ms: number) => new Promise((ok) => setTimeout(ok, ms))
25
+
26
+ /** Bring the popup forward to ask the person something. Chrome opens it only
27
+ * in a focused window; otherwise the badge asks. */
28
+ export let pop = (): Promise<void> => chrome.action.openPopup().catch(() => {})
29
+
30
+ let touched = async (tab: Tab, t: Touch): Promise<Touched> =>
31
+ (await chrome.scripting.executeScript({
32
+ target: { tabId: tab.id },
33
+ func: touch,
34
+ args: [t],
35
+ }))[0]?.result ?? { controls: [] }
36
+
37
+ // Where a tab is once what a step started has loaded, and whether the person
38
+ // allowed its site.
39
+ let settled = async (id: number): Promise<Seen['now']> => {
40
+ await wait(300)
41
+ let end = Date.now() + LOAD
42
+ let t = await chrome.tabs.get(id)
43
+ while (t.status == 'loading' && Date.now() < end) {
44
+ await wait(200)
45
+ t = await chrome.tabs.get(id)
46
+ }
47
+ let { consent = NONE } = await stored()
48
+ let on = site(t.url ?? '')
49
+ return {
50
+ url: t.url ?? '',
51
+ title: t.title,
52
+ allowed: !!on && allowed(on)(consent),
53
+ }
54
+ }
55
+
56
+ /** The person's answer to a step that waits for their Do it, until `by`:
57
+ * nothing when they pressed it, or the step needs none. */
58
+ let confirmed = async (
59
+ call: Call['call'],
60
+ control: Control | undefined,
61
+ tab: Tab,
62
+ by: number,
63
+ ): Promise<Answered | undefined> => {
64
+ if (!confirms(call, control)) return undefined
65
+ let id = call.id
66
+ await keep({
67
+ confirm: {
68
+ id,
69
+ site: site(tab.url) ?? tab.url,
70
+ doing: doing(call, control),
71
+ },
72
+ })
73
+ pop()
74
+ let yes = await until(
75
+ (s) => s.confirm?.id == id ? s.confirm.yes : undefined,
76
+ by - Date.now(),
77
+ )
78
+ if ((await stored()).confirm?.id == id) await keep({ confirm: undefined })
79
+ return yes ? undefined : yes == false ? 'no' : 'none'
80
+ }
81
+
82
+ export let GIVE: Record<Need, Give> = {
83
+ page: async (_, tab) => ({
84
+ page: (await chrome.scripting.executeScript({
85
+ target: { tabId: tab.id },
86
+ func: read,
87
+ }))[0]?.result,
88
+ }),
89
+ shot: async (_, tab) => ({
90
+ shot: await chrome.tabs.captureVisibleTab(tab.windowId, { format: 'png' }),
91
+ }),
92
+ list: async (_, tab) => {
93
+ let { controls, error } = await touched(tab, { op: 'list' })
94
+ return { controls, more: error }
95
+ },
96
+ act: async (call, tab, by) => {
97
+ let ref = String(call.args.ref ?? '')
98
+ let [control] = (await touched(tab, { op: 'find', ref })).controls
99
+ if (!control || !named(call.args.name, control)) {
100
+ return { controls: control ? [control] : [] }
101
+ }
102
+ let answered = await confirmed(call, control, tab, by)
103
+ if (answered) return { controls: [control], answered }
104
+ let done = await touched(tab, {
105
+ op: call.tool == 'browser_click' ? 'click' : 'type',
106
+ ref,
107
+ name: control.name,
108
+ text: String(call.args.text ?? ''),
109
+ enter: call.args.enter === true,
110
+ })
111
+ return done.error
112
+ ? { controls: [control], failed: done.error }
113
+ : { controls: [control], now: await settled(tab.id) }
114
+ },
115
+ go: async (call, tab, by) => {
116
+ let answered = await confirmed(call, undefined, tab, by)
117
+ if (answered) return { answered }
118
+ let url = String(call.args.url)
119
+ let opened = call.args.new_tab === true
120
+ ? await chrome.tabs.create({ url, active: true })
121
+ : await chrome.tabs.update(tab.id, { url })
122
+ return { now: await settled(opened?.id ?? tab.id) }
123
+ },
124
+ }
package/icon.js ADDED
@@ -0,0 +1,60 @@
1
+ // The extension's icon, drawn by code at build time so no image lives in the
2
+ // repository, and written as a PNG.
3
+ import { png as encode } from "@yaks/blob/png";
4
+ let GREEN = [
5
+ 167,
6
+ 192,
7
+ 128,
8
+ 255
9
+ ];
10
+ let DARK = [
11
+ 45,
12
+ 53,
13
+ 59,
14
+ 255
15
+ ];
16
+ let CREAM = [
17
+ 211,
18
+ 198,
19
+ 170,
20
+ 255
21
+ ];
22
+ let CLEAR = [
23
+ 0,
24
+ 0,
25
+ 0,
26
+ 0
27
+ ];
28
+ /** The icon's colour at a point of the unit square: a rounded square, an
29
+ * eye's dark almond across it, its iris and its pupil. */ export let eye = (x, y)=>{
30
+ let [dx, dy] = [
31
+ Math.abs(x - 0.5),
32
+ Math.abs(y - 0.5)
33
+ ];
34
+ let corner = Math.hypot(Math.max(dx - 0.3, 0), Math.max(dy - 0.3, 0));
35
+ let r = Math.hypot(dx, dy);
36
+ return corner > 0.2 ? CLEAR : r < 0.065 ? DARK : r < 0.14 ? CREAM : Math.hypot(dx / 0.38, dy / 0.24) <= 1 ? DARK : GREEN;
37
+ };
38
+ /** A square PNG `size` pixels wide, each pixel painted from four samples of
39
+ * `paint` over the unit square, so its edges are smooth. */ export let png = async (size, paint = eye)=>{
40
+ let pixels = new Uint8Array(size * size * 4);
41
+ for(let y = 0; y < size; y++){
42
+ for(let x = 0; x < size; x++){
43
+ let at = (y * size + x) * 4;
44
+ let samples = [
45
+ 0.25,
46
+ 0.75
47
+ ].flatMap((sy)=>[
48
+ 0.25,
49
+ 0.75
50
+ ].map((sx)=>paint((x + sx) / size, (y + sy) / size)));
51
+ let alpha = samples.reduce((a, s)=>a + s[3], 0);
52
+ for(let c = 0; c < 3; c++){
53
+ pixels[at + c] = alpha && Math.round(samples.reduce((a, s)=>a + s[c] * s[3], 0) / alpha);
54
+ }
55
+ pixels[at + 3] = Math.round(alpha / 4);
56
+ }
57
+ }
58
+ return await encode(size, size, pixels);
59
+ };
60
+ //# sourceMappingURL=icon.js.map
package/icon.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"icon.js","sources":["./icon.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,oCAAoC;AAEpC,SAAS,OAAO,MAAM,8BAAmC;AAKzD,IAAI,QAAc;EAAC;EAAK;EAAK;EAAK;CAAI;AACtC,IAAI,OAAa;EAAC;EAAI;EAAI;EAAI;CAAI;AAClC,IAAI,QAAc;EAAC;EAAK;EAAK;EAAK;CAAI;AACtC,IAAI,QAAc;EAAC;EAAG;EAAG;EAAG;CAAE;AAE9B;wDACwD,GACxD,OAAO,IAAI,MAAM,CAAC,GAAW;EAC3B,IAAI,CAAC,IAAI,GAAG,GAAG;IAAC,KAAK,GAAG,CAAC,IAAI;IAAM,KAAK,GAAG,CAAC,IAAI;GAAK;EACrD,IAAI,SAAS,KAAK,KAAK,CAAC,KAAK,GAAG,CAAC,KAAK,KAAK,IAAI,KAAK,GAAG,CAAC,KAAK,KAAK;EAClE,IAAI,IAAI,KAAK,KAAK,CAAC,IAAI;EACvB,OAAO,SAAS,MACZ,QACA,IAAI,QACJ,OACA,IAAI,OACJ,QACA,KAAK,KAAK,CAAC,KAAK,MAAM,KAAK,SAAS,IACpC,OACA;AACN,EAAC;AAED;0DAC0D,GAC1D,OAAO,IAAI,MAAM,OACf,MACA,QAAwC,GAAG;EAE3C,IAAI,SAAS,IAAI,WAAW,OAAO,OAAO;EAC1C,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,IAAK;IAC7B,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,IAAK;MAC7B,IAAI,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI;MAC1B,IAAI,UAAU;QAAC;QAAM;OAAK,CAAC,OAAO,CAAC,CAAC,KAClC;UAAC;UAAM;SAAK,CAAC,GAAG,CAAC,CAAC,KAAO,MAAM,CAAC,IAAI,EAAE,IAAI,MAAM,CAAC,IAAI,EAAE,IAAI;MAE7D,IAAI,QAAQ,QAAQ,MAAM,CAAC,CAAC,GAAG,IAAM,IAAI,CAAC,CAAC,EAAE,EAAE;MAC/C,IAAK,IAAI,IAAI,GAAG,IAAI,GAAG,IAAK;QAC1B,MAAM,CAAC,KAAK,EAAE,GAAG,SACf,KAAK,KAAK,CAAC,QAAQ,MAAM,CAAC,CAAC,GAAG,IAAM,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,EAAE,KAAK;MAC9D;MACA,MAAM,CAAC,KAAK,EAAE,GAAG,KAAK,KAAK,CAAC,QAAQ;IACtC;EACF;EACA,OAAO,MAAM,OAAO,MAAM,MAAM;AAClC,EAAC"}
package/icon.ts ADDED
@@ -0,0 +1,53 @@
1
+ // The extension's icon, drawn by code at build time so no image lives in the
2
+ // repository, and written as a PNG.
3
+
4
+ import { png as encode } from '@yaks/blob/png'
5
+
6
+ /** A colour, as red, green, blue and alpha bytes. */
7
+ export type Rgba = [number, number, number, number]
8
+
9
+ let GREEN: Rgba = [167, 192, 128, 255]
10
+ let DARK: Rgba = [45, 53, 59, 255]
11
+ let CREAM: Rgba = [211, 198, 170, 255]
12
+ let CLEAR: Rgba = [0, 0, 0, 0]
13
+
14
+ /** The icon's colour at a point of the unit square: a rounded square, an
15
+ * eye's dark almond across it, its iris and its pupil. */
16
+ export let eye = (x: number, y: number): Rgba => {
17
+ let [dx, dy] = [Math.abs(x - 0.5), Math.abs(y - 0.5)]
18
+ let corner = Math.hypot(Math.max(dx - 0.3, 0), Math.max(dy - 0.3, 0))
19
+ let r = Math.hypot(dx, dy)
20
+ return corner > 0.2
21
+ ? CLEAR
22
+ : r < 0.065
23
+ ? DARK
24
+ : r < 0.14
25
+ ? CREAM
26
+ : Math.hypot(dx / 0.38, dy / 0.24) <= 1
27
+ ? DARK
28
+ : GREEN
29
+ }
30
+
31
+ /** A square PNG `size` pixels wide, each pixel painted from four samples of
32
+ * `paint` over the unit square, so its edges are smooth. */
33
+ export let png = async (
34
+ size: number,
35
+ paint: (x: number, y: number) => Rgba = eye,
36
+ ): Promise<Uint8Array> => {
37
+ let pixels = new Uint8Array(size * size * 4)
38
+ for (let y = 0; y < size; y++) {
39
+ for (let x = 0; x < size; x++) {
40
+ let at = (y * size + x) * 4
41
+ let samples = [0.25, 0.75].flatMap((sy) =>
42
+ [0.25, 0.75].map((sx) => paint((x + sx) / size, (y + sy) / size))
43
+ )
44
+ let alpha = samples.reduce((a, s) => a + s[3], 0)
45
+ for (let c = 0; c < 3; c++) {
46
+ pixels[at + c] = alpha &&
47
+ Math.round(samples.reduce((a, s) => a + s[c] * s[3], 0) / alpha)
48
+ }
49
+ pixels[at + 3] = Math.round(alpha / 4)
50
+ }
51
+ }
52
+ return await encode(size, size, pixels)
53
+ }
package/manifest.js ADDED
@@ -0,0 +1,50 @@
1
+ // The extension's Manifest V3 `manifest.json`, made from a configuration. It
2
+ // may reach its host and the host's spaces from the start; every other site is
3
+ // an optional permission, asked for one site at a time (./consent.ts).
4
+ import pkg from './package.json' with {
5
+ type: 'json'
6
+ };
7
+ /** The sizes the icons are drawn at, as the browser asks for them. */ export let SIZES = [
8
+ 16,
9
+ 32,
10
+ 48,
11
+ 128
12
+ ];
13
+ let icons = Object.fromEntries(SIZES.map((n)=>[
14
+ n,
15
+ `icon${n}.png`
16
+ ]));
17
+ /** The manifest for a configuration. */ export let manifest = (config)=>{
18
+ let { protocol, hostname } = new URL(config.origin);
19
+ return {
20
+ manifest_version: 3,
21
+ name: config.name,
22
+ version: pkg.version,
23
+ description: config.description ?? `Lets your agent on ${config.name} see the page you're on.`,
24
+ icons,
25
+ action: {
26
+ default_popup: 'popup.html',
27
+ default_icon: icons
28
+ },
29
+ background: {
30
+ service_worker: 'background.js',
31
+ type: 'module'
32
+ },
33
+ permissions: [
34
+ 'activeTab',
35
+ 'scripting',
36
+ 'storage',
37
+ 'identity',
38
+ 'tabs',
39
+ 'alarms'
40
+ ],
41
+ host_permissions: [
42
+ `${config.origin}/*`,
43
+ `${protocol}//*.${hostname}/*`
44
+ ],
45
+ optional_host_permissions: [
46
+ '*://*/*'
47
+ ]
48
+ };
49
+ };
50
+ //# sourceMappingURL=manifest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.js","sources":["./manifest.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,+EAA+E;AAC/E,uEAAuE;AAEvE,OAAO,SAAS,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAGtD,oEAAoE,GACpE,OAAO,IAAI,QAAQ;EAAC;EAAI;EAAI;EAAI;CAAI,CAAA;AAEpC,IAAI,QAAQ,OAAO,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,IAAM;IAAC;IAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC;GAAC;AAgBnE,sCAAsC,GACtC,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,IAAI,IAAI,OAAO,MAAM;EAClD,OAAO;IACL,kBAAkB;IAClB,MAAM,OAAO,IAAI;IACjB,SAAS,IAAI,OAAO;IACpB,aAAa,OAAO,WAAW,IAC7B,CAAC,mBAAmB,EAAE,OAAO,IAAI,CAAC,wBAAwB,CAAC;IAC7D;IACA,QAAQ;MAAE,eAAe;MAAc,cAAc;IAAM;IAC3D,YAAY;MAAE,gBAAgB;MAAiB,MAAM;IAAS;IAC9D,aAAa;MACX;MACA;MACA;MACA;MACA;MACA;KACD;IACD,kBAAkB;MAChB,GAAG,OAAO,MAAM,CAAC,EAAE,CAAC;MACpB,GAAG,SAAS,IAAI,EAAE,SAAS,EAAE,CAAC;KAC/B;IACD,2BAA2B;MAAC;KAAU;EACxC;AACF,EAAC"}
package/manifest.ts ADDED
@@ -0,0 +1,53 @@
1
+ // The extension's Manifest V3 `manifest.json`, made from a configuration. It
2
+ // may reach its host and the host's spaces from the start; every other site is
3
+ // an optional permission, asked for one site at a time (./consent.ts).
4
+
5
+ import pkg from './package.json' with { type: 'json' }
6
+ import type { Config } from './connect.ts'
7
+
8
+ /** The sizes the icons are drawn at, as the browser asks for them. */
9
+ export let SIZES = [16, 32, 48, 128]
10
+
11
+ let icons = Object.fromEntries(SIZES.map((n) => [n, `icon${n}.png`]))
12
+
13
+ /** A Manifest V3 `manifest.json`, as far as this extension fills it in. */
14
+ export type Manifest = {
15
+ manifest_version: 3
16
+ name: string
17
+ version: string
18
+ description: string
19
+ icons: Record<string, string>
20
+ action: { default_popup: string; default_icon: Record<string, string> }
21
+ background: { service_worker: string; type: 'module' }
22
+ permissions: string[]
23
+ host_permissions: string[]
24
+ optional_host_permissions: string[]
25
+ }
26
+
27
+ /** The manifest for a configuration. */
28
+ export let manifest = (config: Config): Manifest => {
29
+ let { protocol, hostname } = new URL(config.origin)
30
+ return {
31
+ manifest_version: 3,
32
+ name: config.name,
33
+ version: pkg.version,
34
+ description: config.description ??
35
+ `Lets your agent on ${config.name} see the page you're on.`,
36
+ icons,
37
+ action: { default_popup: 'popup.html', default_icon: icons },
38
+ background: { service_worker: 'background.js', type: 'module' },
39
+ permissions: [
40
+ 'activeTab',
41
+ 'scripting',
42
+ 'storage',
43
+ 'identity',
44
+ 'tabs',
45
+ 'alarms',
46
+ ],
47
+ host_permissions: [
48
+ `${config.origin}/*`,
49
+ `${protocol}//*.${hostname}/*`,
50
+ ],
51
+ optional_host_permissions: ['*://*/*'],
52
+ }
53
+ }
package/mod.js 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
+ export * from "./wire.js";
5
+ export * from "./tools.js";
6
+ export * from "./read.js";
7
+ export * from "./touch.js";
8
+ export * from "./act.js";
9
+ export * from "./answer.js";
10
+ export * from "./consent.js";
11
+ export * from "./connect.js";
12
+ export * from "./signin.js";
13
+ export * from "./manifest.js";
14
+ export * from "./screen.js";
15
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,gEAAgE;AAChE,qEAAqE;AAErE,0BAAyB;AACzB,2BAA0B;AAC1B,0BAAyB;AACzB,2BAA0B;AAC1B,yBAAwB;AACxB,4BAA2B;AAC3B,6BAA4B;AAC5B,6BAA4B;AAC5B,4BAA2B;AAC3B,8BAA6B;AAC7B,4BAA2B"}