@gemigo/extension-sdk 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 GemiGo Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # @gemigo/extension-sdk
2
+
3
+ GemiGo Extension SDK for building apps that run inside the GemiGo browser extension.
4
+
5
+ ## Installation
6
+
7
+ ### CDN (Recommended for simple apps)
8
+
9
+ ```html
10
+ <script src="https://unpkg.com/@gemigo/extension-sdk/dist/gemigo-extension-sdk.umd.js"></script>
11
+ ```
12
+
13
+ Or use jsDelivr:
14
+ ```html
15
+ <script src="https://cdn.jsdelivr.net/npm/@gemigo/extension-sdk/dist/gemigo-extension-sdk.umd.js"></script>
16
+ ```
17
+
18
+ ### npm
19
+
20
+ ```bash
21
+ npm install @gemigo/extension-sdk
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ### CDN Usage
27
+
28
+ ```html
29
+ <script src="https://unpkg.com/@gemigo/extension-sdk/dist/gemigo-extension-sdk.umd.js"></script>
30
+ <script>
31
+ GemigoExtensionSDK.connect().then((gemigo) => {
32
+ // Get current page info
33
+ gemigo.getPageInfo().then(console.log);
34
+
35
+ // Subscribe to context menu events
36
+ gemigo.on('contextMenu', (event) => {
37
+ console.log('Menu clicked:', event.menuId, event.selectionText);
38
+ });
39
+ });
40
+ </script>
41
+ ```
42
+
43
+ ### ES Module Usage
44
+
45
+ ```js
46
+ import { connect } from '@gemigo/extension-sdk';
47
+
48
+ const gemigo = await connect();
49
+ const pageInfo = await gemigo.getPageInfo();
50
+ ```
51
+
52
+ ## API Reference
53
+
54
+ ### `connect(): Promise<GemigoExtension>`
55
+
56
+ Connect to the GemiGo extension host. Must be called before using any SDK methods.
57
+
58
+ ### Page APIs
59
+
60
+ | Method | Description |
61
+ |--------|-------------|
62
+ | `getPageInfo()` | Get current page URL, title, favicon |
63
+ | `getPageHTML()` | Get full page HTML content |
64
+ | `getPageText()` | Get page text content |
65
+ | `getSelection()` | Get selected text |
66
+ | `extractArticle()` | Extract article title, content, excerpt |
67
+
68
+ ### Action APIs
69
+
70
+ | Method | Description |
71
+ |--------|-------------|
72
+ | `highlight(selector, color?)` | Highlight elements on page |
73
+ | `notify(title, message)` | Send system notification |
74
+ | `captureVisible()` | Capture screenshot of visible tab |
75
+
76
+ ### Event APIs
77
+
78
+ | Method | Description |
79
+ |--------|-------------|
80
+ | `on('contextMenu', handler)` | Subscribe to context menu events |
81
+ | `off('contextMenu', handler?)` | Unsubscribe from events |
82
+ | `getContextMenuEvent()` | Get pending context menu event |
83
+
84
+ ## Example: Translation App
85
+
86
+ ```html
87
+ <!DOCTYPE html>
88
+ <html>
89
+ <head>
90
+ <title>Translation App</title>
91
+ </head>
92
+ <body>
93
+ <div id="result"></div>
94
+
95
+ <script src="https://unpkg.com/@gemigo/extension-sdk/dist/gemigo-extension-sdk.umd.js"></script>
96
+ <script>
97
+ GemigoExtensionSDK.connect().then(async (gemigo) => {
98
+ // Handle context menu "Translate" action
99
+ gemigo.on('contextMenu', async (event) => {
100
+ if (event.menuId === 'translate' && event.selectionText) {
101
+ // Your translation logic here
102
+ document.getElementById('result').textContent =
103
+ `Translating: ${event.selectionText}`;
104
+ }
105
+ });
106
+ });
107
+ </script>
108
+ </body>
109
+ </html>
110
+ ```
111
+
112
+ ## License
113
+
114
+ MIT
@@ -0,0 +1,243 @@
1
+ var MessageType;
2
+ (function(e) {
3
+ e.Call = "call", e.Reply = "reply", e.Syn = "syn", e.SynAck = "synAck", e.Ack = "ack";
4
+ })(MessageType ||= {});
5
+ var Resolution;
6
+ (function(e) {
7
+ e.Fulfilled = "fulfilled", e.Rejected = "rejected";
8
+ })(Resolution ||= {});
9
+ var ErrorCode;
10
+ (function(e) {
11
+ e.ConnectionDestroyed = "ConnectionDestroyed", e.ConnectionTimeout = "ConnectionTimeout", e.NoIframeSrc = "NoIframeSrc";
12
+ })(ErrorCode ||= {});
13
+ var NativeErrorName;
14
+ (function(e) {
15
+ e.DataCloneError = "DataCloneError";
16
+ })(NativeErrorName ||= {});
17
+ var NativeEventType;
18
+ (function(e) {
19
+ e.Message = "message";
20
+ })(NativeEventType ||= {});
21
+ var createDestructor_default = (e, f) => {
22
+ let p = [], m = !1;
23
+ return {
24
+ destroy(h) {
25
+ m || (m = !0, f(`${e}: Destroying connection`), p.forEach((e) => {
26
+ e(h);
27
+ }));
28
+ },
29
+ onDestroy(e) {
30
+ m ? e() : p.push(e);
31
+ }
32
+ };
33
+ }, createLogger_default = (e) => (...f) => {
34
+ e && console.log("[Penpal]", ...f);
35
+ };
36
+ const serializeError = ({ name: e, message: f, stack: p }) => ({
37
+ name: e,
38
+ message: f,
39
+ stack: p
40
+ }), deserializeError = (e) => {
41
+ let f = /* @__PURE__ */ Error();
42
+ return Object.keys(e).forEach((p) => f[p] = e[p]), f;
43
+ };
44
+ var connectCallReceiver_default = (p, g, _) => {
45
+ let { localName: y, local: b, remote: x, originForSending: S, originForReceiving: C } = p, w = !1, T = (p) => {
46
+ if (p.source !== x || p.data.penpal !== MessageType.Call) return;
47
+ if (C !== "*" && p.origin !== C) {
48
+ _(`${y} received message from origin ${p.origin} which did not match expected origin ${C}`);
49
+ return;
50
+ }
51
+ let { methodName: h, args: b, id: T } = p.data;
52
+ _(`${y}: Received ${h}() call`);
53
+ let E = (p) => (g) => {
54
+ if (_(`${y}: Sending ${h}() reply`), w) {
55
+ _(`${y}: Unable to send ${h}() reply due to destroyed connection`);
56
+ return;
57
+ }
58
+ let b = {
59
+ penpal: MessageType.Reply,
60
+ id: T,
61
+ resolution: p,
62
+ returnValue: g
63
+ };
64
+ p === Resolution.Rejected && g instanceof Error && (b.returnValue = serializeError(g), b.returnValueIsError = !0);
65
+ try {
66
+ x.postMessage(b, S);
67
+ } catch (p) {
68
+ if (p.name === NativeErrorName.DataCloneError) {
69
+ let m = {
70
+ penpal: MessageType.Reply,
71
+ id: T,
72
+ resolution: Resolution.Rejected,
73
+ returnValue: serializeError(p),
74
+ returnValueIsError: !0
75
+ };
76
+ x.postMessage(m, S);
77
+ }
78
+ throw p;
79
+ }
80
+ };
81
+ new Promise((e) => e(g[h].apply(g, b))).then(E(Resolution.Fulfilled), E(Resolution.Rejected));
82
+ };
83
+ return b.addEventListener(NativeEventType.Message, T), () => {
84
+ w = !0, b.removeEventListener(NativeEventType.Message, T);
85
+ };
86
+ }, id = 0, generateId_default = () => ++id, KEY_PATH_DELIMITER = ".", keyPathToSegments = (e) => e ? e.split(KEY_PATH_DELIMITER) : [], segmentsToKeyPath = (e) => e.join(KEY_PATH_DELIMITER), createKeyPath = (e, f) => {
87
+ let p = keyPathToSegments(f || "");
88
+ return p.push(e), segmentsToKeyPath(p);
89
+ };
90
+ const setAtKeyPath = (e, f, p) => {
91
+ let m = keyPathToSegments(f);
92
+ return m.reduce((e, f, h) => (e[f] === void 0 && (e[f] = {}), h === m.length - 1 && (e[f] = p), e[f]), e), e;
93
+ }, serializeMethods = (e, f) => {
94
+ let p = {};
95
+ return Object.keys(e).forEach((m) => {
96
+ let h = e[m], g = createKeyPath(m, f);
97
+ typeof h == "object" && Object.assign(p, serializeMethods(h, g)), typeof h == "function" && (p[g] = h);
98
+ }), p;
99
+ }, deserializeMethods = (e) => {
100
+ let f = {};
101
+ for (let p in e) setAtKeyPath(f, p, e[p]);
102
+ return f;
103
+ };
104
+ var connectCallSender_default = (m, g, _, v, b) => {
105
+ let { localName: x, local: C, remote: w, originForSending: T, originForReceiving: E } = g, D = !1;
106
+ b(`${x}: Connecting call sender`);
107
+ let O = (m) => (...g) => {
108
+ b(`${x}: Sending ${m}() call`);
109
+ let _;
110
+ try {
111
+ w.closed && (_ = !0);
112
+ } catch {
113
+ _ = !0;
114
+ }
115
+ if (_ && v(), D) {
116
+ let e = /* @__PURE__ */ Error(`Unable to send ${m}() call due to destroyed connection`);
117
+ throw e.code = ErrorCode.ConnectionDestroyed, e;
118
+ }
119
+ return new Promise((p, _) => {
120
+ let v = generateId_default(), D = (g) => {
121
+ if (g.source !== w || g.data.penpal !== MessageType.Reply || g.data.id !== v) return;
122
+ if (E !== "*" && g.origin !== E) {
123
+ b(`${x} received message from origin ${g.origin} which did not match expected origin ${E}`);
124
+ return;
125
+ }
126
+ let S = g.data;
127
+ b(`${x}: Received ${m}() reply`), C.removeEventListener(NativeEventType.Message, D);
128
+ let T = S.returnValue;
129
+ S.returnValueIsError && (T = deserializeError(T)), (S.resolution === Resolution.Fulfilled ? p : _)(T);
130
+ };
131
+ C.addEventListener(NativeEventType.Message, D);
132
+ let O = {
133
+ penpal: MessageType.Call,
134
+ id: v,
135
+ methodName: m,
136
+ args: g
137
+ };
138
+ w.postMessage(O, T);
139
+ });
140
+ }, k = _.reduce((e, f) => (e[f] = O(f), e), {});
141
+ return Object.assign(m, deserializeMethods(k)), () => {
142
+ D = !0;
143
+ };
144
+ }, startConnectionTimeout_default = (e, f) => {
145
+ let m;
146
+ return e !== void 0 && (m = window.setTimeout(() => {
147
+ let m = /* @__PURE__ */ Error(`Connection timed out after ${e}ms`);
148
+ m.code = ErrorCode.ConnectionTimeout, f(m);
149
+ }, e)), () => {
150
+ clearTimeout(m);
151
+ };
152
+ }, handleSynAckMessageFactory_default = (f, p, m, h) => {
153
+ let { destroy: g, onDestroy: _ } = m;
154
+ return (m) => {
155
+ if (!(f instanceof RegExp ? f.test(m.origin) : f === "*" || f === m.origin)) {
156
+ h(`Child: Handshake - Received SYN-ACK from origin ${m.origin} which did not match expected origin ${f}`);
157
+ return;
158
+ }
159
+ h("Child: Handshake - Received SYN-ACK, responding with ACK");
160
+ let v = m.origin === "null" ? "*" : m.origin, y = {
161
+ penpal: MessageType.Ack,
162
+ methodNames: Object.keys(p)
163
+ };
164
+ window.parent.postMessage(y, v);
165
+ let x = {
166
+ localName: "Child",
167
+ local: window,
168
+ remote: window.parent,
169
+ originForSending: v,
170
+ originForReceiving: m.origin
171
+ };
172
+ _(connectCallReceiver_default(x, p, h));
173
+ let S = {};
174
+ return _(connectCallSender_default(S, x, m.data.methodNames, g, h)), S;
175
+ };
176
+ }, areGlobalsAccessible = () => {
177
+ try {
178
+ clearTimeout();
179
+ } catch {
180
+ return !1;
181
+ }
182
+ return !0;
183
+ }, connectToParent_default = (f = {}) => {
184
+ let { parentOrigin: p = "*", methods: m = {}, timeout: v, debug: y = !1 } = f, b = createLogger_default(y), x = createDestructor_default("Child", b), { destroy: S, onDestroy: C } = x, w = handleSynAckMessageFactory_default(p, serializeMethods(m), x, b), T = () => {
185
+ b("Child: Handshake - Sending SYN");
186
+ let f = { penpal: MessageType.Syn }, m = p instanceof RegExp ? "*" : p;
187
+ window.parent.postMessage(f, m);
188
+ };
189
+ return {
190
+ promise: new Promise((f, p) => {
191
+ let m = startConnectionTimeout_default(v, S), g = (p) => {
192
+ if (areGlobalsAccessible() && !(p.source !== parent || !p.data) && p.data.penpal === MessageType.SynAck) {
193
+ let e = w(p);
194
+ e && (window.removeEventListener(NativeEventType.Message, g), m(), f(e));
195
+ }
196
+ };
197
+ window.addEventListener(NativeEventType.Message, g), T(), C((e) => {
198
+ window.removeEventListener(NativeEventType.Message, g), e && p(e);
199
+ });
200
+ }),
201
+ destroy() {
202
+ S();
203
+ }
204
+ };
205
+ }, eventHandlers = { contextMenu: [] }, connection = null, hostMethods = null;
206
+ async function connect() {
207
+ return hostMethods ? createSDKInstance(hostMethods) : (connection = connectToParent_default({ methods: { onContextMenuEvent(e) {
208
+ eventHandlers.contextMenu.forEach((f) => {
209
+ try {
210
+ f(e);
211
+ } catch (e) {
212
+ console.error("[GemiGo SDK] Error in contextMenu handler:", e);
213
+ }
214
+ });
215
+ } } }), hostMethods = await connection.promise, createSDKInstance(hostMethods));
216
+ }
217
+ function createSDKInstance(e) {
218
+ return {
219
+ getPageInfo: () => e.getPageInfo(),
220
+ getPageHTML: () => e.getPageHTML(),
221
+ getPageText: () => e.getPageText(),
222
+ getSelection: () => e.getSelection(),
223
+ highlight: (f, p) => e.highlight(f, p),
224
+ notify: (f, p) => e.notify({
225
+ title: f,
226
+ message: p
227
+ }),
228
+ captureVisible: () => e.captureVisible(),
229
+ extractArticle: () => e.extractArticle(),
230
+ getContextMenuEvent: () => e.getContextMenuEvent(),
231
+ on(e, f) {
232
+ e === "contextMenu" && eventHandlers.contextMenu.push(f);
233
+ },
234
+ off(e, f) {
235
+ if (e === "contextMenu") if (f) {
236
+ let e = eventHandlers.contextMenu.indexOf(f);
237
+ e > -1 && eventHandlers.contextMenu.splice(e, 1);
238
+ } else eventHandlers.contextMenu = [];
239
+ }
240
+ };
241
+ }
242
+ var src_default = { connect };
243
+ export { connect, src_default as default };
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.GemigoExtensionSDK={}))})(this,function(e){Object.defineProperty(e,`__esModule`,{value:!0});var t;(function(e){e.Call=`call`,e.Reply=`reply`,e.Syn=`syn`,e.SynAck=`synAck`,e.Ack=`ack`})(t||={});var n;(function(e){e.Fulfilled=`fulfilled`,e.Rejected=`rejected`})(n||={});var r;(function(e){e.ConnectionDestroyed=`ConnectionDestroyed`,e.ConnectionTimeout=`ConnectionTimeout`,e.NoIframeSrc=`NoIframeSrc`})(r||={});var i;(function(e){e.DataCloneError=`DataCloneError`})(i||={});var a;(function(e){e.Message=`message`})(a||={});var o=(e,t)=>{let n=[],r=!1;return{destroy(i){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(i)}))},onDestroy(e){r?e():n.push(e)}}},s=e=>(...t)=>{e&&console.log(`[Penpal]`,...t)};let c=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n}),l=e=>{let t=Error();return Object.keys(e).forEach(n=>t[n]=e[n]),t};var u=(e,r,o)=>{let{localName:s,local:l,remote:u,originForSending:d,originForReceiving:f}=e,p=!1,m=e=>{if(e.source!==u||e.data.penpal!==t.Call)return;if(f!==`*`&&e.origin!==f){o(`${s} received message from origin ${e.origin} which did not match expected origin ${f}`);return}let{methodName:a,args:l,id:m}=e.data;o(`${s}: Received ${a}() call`);let h=e=>r=>{if(o(`${s}: Sending ${a}() reply`),p){o(`${s}: Unable to send ${a}() reply due to destroyed connection`);return}let l={penpal:t.Reply,id:m,resolution:e,returnValue:r};e===n.Rejected&&r instanceof Error&&(l.returnValue=c(r),l.returnValueIsError=!0);try{u.postMessage(l,d)}catch(e){if(e.name===i.DataCloneError){let r={penpal:t.Reply,id:m,resolution:n.Rejected,returnValue:c(e),returnValueIsError:!0};u.postMessage(r,d)}throw e}};new Promise(e=>e(r[a].apply(r,l))).then(h(n.Fulfilled),h(n.Rejected))};return l.addEventListener(a.Message,m),()=>{p=!0,l.removeEventListener(a.Message,m)}},d=0,f=()=>++d,p=`.`,m=e=>e?e.split(p):[],h=e=>e.join(p),g=(e,t)=>{let n=m(t||``);return n.push(e),h(n)};let _=(e,t,n)=>{let r=m(t);return r.reduce((e,t,i)=>(e[t]===void 0&&(e[t]={}),i===r.length-1&&(e[t]=n),e[t]),e),e},v=(e,t)=>{let n={};return Object.keys(e).forEach(r=>{let i=e[r],a=g(r,t);typeof i==`object`&&Object.assign(n,v(i,a)),typeof i==`function`&&(n[a]=i)}),n},y=e=>{let t={};for(let n in e)_(t,n,e[n]);return t};var b=(e,i,o,s,c)=>{let{localName:u,local:d,remote:p,originForSending:m,originForReceiving:h}=i,g=!1;c(`${u}: Connecting call sender`);let _=e=>(...i)=>{c(`${u}: Sending ${e}() call`);let o;try{p.closed&&(o=!0)}catch{o=!0}if(o&&s(),g){let t=Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=r.ConnectionDestroyed,t}return new Promise((r,o)=>{let s=f(),g=i=>{if(i.source!==p||i.data.penpal!==t.Reply||i.data.id!==s)return;if(h!==`*`&&i.origin!==h){c(`${u} received message from origin ${i.origin} which did not match expected origin ${h}`);return}let f=i.data;c(`${u}: Received ${e}() reply`),d.removeEventListener(a.Message,g);let m=f.returnValue;f.returnValueIsError&&(m=l(m)),(f.resolution===n.Fulfilled?r:o)(m)};d.addEventListener(a.Message,g);let _={penpal:t.Call,id:s,methodName:e,args:i};p.postMessage(_,m)})},v=o.reduce((e,t)=>(e[t]=_(t),e),{});return Object.assign(e,y(v)),()=>{g=!0}},x=(e,t)=>{let n;return e!==void 0&&(n=window.setTimeout(()=>{let n=Error(`Connection timed out after ${e}ms`);n.code=r.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}},S=(e,n,r,i)=>{let{destroy:a,onDestroy:o}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):e===`*`||e===r.origin)){i(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);return}i(`Child: Handshake - Received SYN-ACK, responding with ACK`);let s=r.origin===`null`?`*`:r.origin,c={penpal:t.Ack,methodNames:Object.keys(n)};window.parent.postMessage(c,s);let l={localName:`Child`,local:window,remote:window.parent,originForSending:s,originForReceiving:r.origin};o(u(l,n,i));let d={};return o(b(d,l,r.data.methodNames,a,i)),d}},C=()=>{try{clearTimeout()}catch{return!1}return!0},w=(e={})=>{let{parentOrigin:n=`*`,methods:r={},timeout:i,debug:c=!1}=e,l=s(c),u=o(`Child`,l),{destroy:d,onDestroy:f}=u,p=S(n,v(r),u,l),m=()=>{l(`Child: Handshake - Sending SYN`);let e={penpal:t.Syn},r=n instanceof RegExp?`*`:n;window.parent.postMessage(e,r)};return{promise:new Promise((e,n)=>{let r=x(i,d),o=n=>{if(C()&&!(n.source!==parent||!n.data)&&n.data.penpal===t.SynAck){let t=p(n);t&&(window.removeEventListener(a.Message,o),r(),e(t))}};window.addEventListener(a.Message,o),m(),f(e=>{window.removeEventListener(a.Message,o),e&&n(e)})}),destroy(){d()}}},T={contextMenu:[]},E=null,D=null;async function O(){return D?k(D):(E=w({methods:{onContextMenuEvent(e){T.contextMenu.forEach(t=>{try{t(e)}catch(e){console.error(`[GemiGo SDK] Error in contextMenu handler:`,e)}})}}}),D=await E.promise,k(D))}function k(e){return{getPageInfo:()=>e.getPageInfo(),getPageHTML:()=>e.getPageHTML(),getPageText:()=>e.getPageText(),getSelection:()=>e.getSelection(),highlight:(t,n)=>e.highlight(t,n),notify:(t,n)=>e.notify({title:t,message:n}),captureVisible:()=>e.captureVisible(),extractArticle:()=>e.extractArticle(),getContextMenuEvent:()=>e.getContextMenuEvent(),on(e,t){e===`contextMenu`&&T.contextMenu.push(t)},off(e,t){if(e===`contextMenu`)if(t){let e=T.contextMenu.indexOf(t);e>-1&&T.contextMenu.splice(e,1)}else T.contextMenu=[]}}}var A={connect:O};e.connect=O,e.default=A});
@@ -0,0 +1,84 @@
1
+ /**
2
+ * GemiGo Extension SDK
3
+ *
4
+ * SDK for building apps that run inside the GemiGo browser extension.
5
+ *
6
+ * Usage (CDN):
7
+ * <script src="https://cdn.example.com/gemigo-extension-sdk.umd.js"></script>
8
+ * <script>
9
+ * GemigoExtensionSDK.connect().then((gemigo) => {
10
+ * gemigo.getPageInfo().then(console.log);
11
+ * });
12
+ * </script>
13
+ *
14
+ * Usage (ES Module):
15
+ * import { connect } from '@gemigo/extension-sdk';
16
+ * const gemigo = await connect();
17
+ * const pageInfo = await gemigo.getPageInfo();
18
+ */
19
+ type ContextMenuEventHandler = (event: {
20
+ menuId: string;
21
+ selectionText?: string;
22
+ pageUrl?: string;
23
+ }) => void;
24
+ export interface GemigoExtension {
25
+ /** Get current page information */
26
+ getPageInfo(): Promise<{
27
+ url: string;
28
+ title: string;
29
+ favIconUrl?: string;
30
+ }>;
31
+ /** Get full page HTML */
32
+ getPageHTML(): Promise<string>;
33
+ /** Get page text content */
34
+ getPageText(): Promise<string>;
35
+ /** Get selected text on page */
36
+ getSelection(): Promise<string>;
37
+ /** Highlight elements matching selector */
38
+ highlight(selector: string, color?: string): Promise<{
39
+ success: boolean;
40
+ count?: number;
41
+ }>;
42
+ /** Send system notification */
43
+ notify(title: string, message: string): Promise<{
44
+ success: boolean;
45
+ }>;
46
+ /** Capture visible tab screenshot */
47
+ captureVisible(): Promise<{
48
+ success: boolean;
49
+ dataUrl?: string;
50
+ error?: string;
51
+ }>;
52
+ /** Extract article content from page */
53
+ extractArticle(): Promise<{
54
+ success: boolean;
55
+ title?: string;
56
+ content?: string;
57
+ excerpt?: string;
58
+ url?: string;
59
+ }>;
60
+ /** Get pending context menu event (if any) */
61
+ getContextMenuEvent(): Promise<{
62
+ success: boolean;
63
+ event?: {
64
+ menuId: string;
65
+ selectionText?: string;
66
+ pageUrl?: string;
67
+ };
68
+ }>;
69
+ /** Register context menu event handler */
70
+ on(event: 'contextMenu', handler: ContextMenuEventHandler): void;
71
+ /** Unregister event handler */
72
+ off(event: 'contextMenu', handler?: ContextMenuEventHandler): void;
73
+ }
74
+ /**
75
+ * Connect to the GemiGo extension host.
76
+ * Must be called before using any SDK methods.
77
+ *
78
+ * @returns Promise resolving to the GemiGo SDK instance
79
+ */
80
+ export declare function connect(): Promise<GemigoExtension>;
81
+ declare const _default: {
82
+ connect: typeof connect;
83
+ };
84
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@gemigo/extension-sdk",
3
+ "version": "0.1.0",
4
+ "description": "GemiGo Extension SDK for browser extension apps",
5
+ "main": "dist/gemigo-extension-sdk.umd.js",
6
+ "module": "dist/gemigo-extension-sdk.es.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "dependencies": {
12
+ "penpal": "^6.2.2"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^5.7.2",
16
+ "vite": "^6.2.5",
17
+ "vite-plugin-dts": "^4.5.4"
18
+ },
19
+ "scripts": {
20
+ "build": "vite build",
21
+ "dev": "vite build --watch"
22
+ }
23
+ }