@plasmohq/messaging
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,2 +0,2 @@ | ||
import { a as PortName } from './types-d2c8c75d.js'; | ||
import { a as PortName } from './types-52ab4931.js'; | ||
@@ -3,0 +3,0 @@ declare const getPortMap: () => Map<PortName, chrome.runtime.Port>; |
@@ -1,7 +0,9 @@ | ||
import { P as PlasmoMessaging, M as MessageName } from './types-d2c8c75d.js'; | ||
import { P as PlasmoMessaging, M as MessageName } from './types-52ab4931.js'; | ||
/** | ||
* Used in an extension page or sandbox page to send message to background. | ||
* Used in any extension context to listen and send messages to background. | ||
*/ | ||
declare const usePageMessaging: PlasmoMessaging.MessageHook; | ||
declare const useMessage: <RequestBody, ResponseBody>(handler: PlasmoMessaging.Handler<string, RequestBody, ResponseBody>) => { | ||
data: RequestBody; | ||
}; | ||
declare const usePort: PlasmoMessaging.PortHook; | ||
@@ -11,4 +13,5 @@ /** | ||
*/ | ||
declare function useMessageRelay(name: MessageName): void; | ||
declare function useMessageRelay<RequestBody = any>(req: PlasmoMessaging.Request<MessageName, RequestBody>): void; | ||
declare const useRelay: PlasmoMessaging.RelayFx; | ||
export { useMessageRelay, usePageMessaging, usePort }; | ||
export { useMessage, useMessageRelay, usePort, useRelay }; |
@@ -1,1 +0,1 @@ | ||
import{useEffect as m,useRef as P,useState as u}from"react";var n=async()=>{let[e]=await chrome.tabs.query({active:!0,currentWindow:!0});return e},g=(e,t)=>!t.__internal&&e.source===globalThis.window&&e.data.name===t.name&&(t.relayId===void 0||e.data.relayId===t.relayId);var d=(e,t)=>{let a=async s=>{if(g(s,e)&&!s.data.relayed){let o={name:e.name,relayId:e.relayId,body:s.data.body},r=await t?.(o);window.postMessage({name:e.name,relayId:e.relayId,body:r,relayed:!0})}};return window.addEventListener("message",a),()=>window.removeEventListener("message",a)};var i=e=>new Promise((t,a)=>{if(!chrome?.runtime)throw new Error("chrome.runtime is not available");chrome.runtime.sendMessage(e,s=>{chrome.runtime.lastError?a(chrome.runtime.lastError):t(s)})}),c=e=>new Promise(async(t,a)=>{if(!chrome?.tabs)throw new Error("chrome.tabs is not available");let s=typeof e.tabId=="number"?e.tabId:(await n()).id;chrome.tabs.sendMessage(s,e,o=>{chrome.runtime.lastError?a(chrome.runtime.lastError):t(o)})}),l=e=>d(e,i);var y=new Map,p=e=>{let t=y.get(e);if(t)return t;let a=chrome.runtime.connect({name:e});return y.set(e,a),a};var f=c,A=()=>{let[e,t]=u(!1);return m(()=>{async function a(){await f({name:"__PLASMO_MESSAGING_PING__"}),(await n())?.id&&t(!0)}a()},[]),{async send(a){if(!e)throw new Error("Background Service not ready to receive message");return await i(a)}}},k=e=>{let t=P(),[a,s]=u();return m(()=>{if(!e)return null;let o=p(e);function r(M){s(M)}return o.onMessage.addListener(r),t.current=o,()=>{o.onMessage.removeListener(r)}},[e]),{data:a,send:o=>{t.current.postMessage({name:e,body:o})}}};function F(e){m(()=>l(e),[])}export{F as useMessageRelay,A as usePageMessaging,k as usePort}; | ||
import{useEffect as m,useRef as u,useState as p}from"react";var c=(e,s)=>!s.__internal&&e.source===globalThis.window&&e.data.name===s.name&&(s.relayId===void 0||e.data.relayId===s.relayId);var i=(e,s)=>{let t=async o=>{if(c(o,e)&&!o.data.relayed){let a={name:e.name,relayId:e.relayId,body:o.data.body},r=await s?.(a);window.postMessage({name:e.name,relayId:e.relayId,body:r,relayed:!0})}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)};var P=e=>new Promise((s,t)=>{if(!chrome?.runtime)throw new Error("chrome.runtime is not available");chrome.runtime.sendMessage(e,o=>{chrome.runtime.lastError?t(chrome.runtime.lastError):s(o)})});var g=e=>i(e,P);var l=new Map,y=e=>{let s=l.get(e);if(s)return s;let t=chrome.runtime.connect({name:e});return l.set(e,t),t};var F=e=>{let[s,t]=p();return m(()=>{let o=async(r,n,d)=>{t(r.body),await e?.({...r,sender:n},{send:M=>d(M)})},a=(r,n,d)=>{o(r,n,d)};return chrome.runtime.onMessage.addListener(a),()=>{chrome.runtime.onMessage.removeListener(a)}},[e]),{data:s}},T=e=>{let s=u(),[t,o]=p();return m(()=>{if(!e)return null;let a=y(e);function r(n){o(n)}return a.onMessage.addListener(r),s.current=a,()=>{a.onMessage.removeListener(r)}},[e]),{data:t,send:a=>{s.current.postMessage({name:e,body:a})}}};function V(e){m(()=>g(e),[])}var k=(e,s)=>{let t=u();return m(()=>(t.current=i(e,s),t.current),[]),()=>t.current?.()};export{F as useMessage,V as useMessageRelay,T as usePort,k as useRelay}; |
@@ -1,3 +0,3 @@ | ||
import { P as PlasmoMessaging, M as MessageName } from './types-d2c8c75d.js'; | ||
export { M as MessageName, c as MessagesMetadata, O as OriginContext, P as PlasmoMessaging, a as PortName, b as PortsMetadata } from './types-d2c8c75d.js'; | ||
import { P as PlasmoMessaging, M as MessageName } from './types-52ab4931.js'; | ||
export { M as MessageName, c as MessagesMetadata, O as OriginContext, P as PlasmoMessaging, a as PortName, b as PortsMetadata } from './types-52ab4931.js'; | ||
@@ -10,6 +10,10 @@ /** | ||
/** | ||
* Send to CS from Ext Pages | ||
* Send to CS from Ext pages or BGSW, default to active tab if no tabId is provided in the request | ||
*/ | ||
declare const sendToActiveContentScript: PlasmoMessaging.SendFx; | ||
declare const sendToContentScript: PlasmoMessaging.SendFx; | ||
/** | ||
* @deprecated Renamed to `sendToContentScript` | ||
*/ | ||
declare const sendToActiveContentScript: PlasmoMessaging.SendFx<string>; | ||
/** | ||
* Any request sent to this relay get send to background, then emitted back as a response | ||
@@ -28,2 +32,2 @@ */ | ||
export { relay, relayMessage, sendToActiveContentScript, sendToBackground, sendToBackgroundViaRelay, sendViaRelay }; | ||
export { relay, relayMessage, sendToActiveContentScript, sendToBackground, sendToBackgroundViaRelay, sendToContentScript, sendViaRelay }; |
@@ -1,1 +0,1 @@ | ||
var n=async()=>{let[e]=await chrome.tabs.query({active:!0,currentWindow:!0});return e},r=(e,a)=>!a.__internal&&e.source===globalThis.window&&e.data.name===a.name&&(a.relayId===void 0||e.data.relayId===a.relayId);var i=(e,a)=>{let t=async s=>{if(r(s,e)&&!s.data.relayed){let o={name:e.name,relayId:e.relayId,body:s.data.body},d=await a?.(o);window.postMessage({name:e.name,relayId:e.relayId,body:d,relayed:!0})}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},m=e=>new Promise((a,t)=>{window.postMessage(e),window.addEventListener("message",s=>{r(s,e)&&s.data.relayed&&a(s.data.body)})});var l=e=>new Promise((a,t)=>{if(!chrome?.runtime)throw new Error("chrome.runtime is not available");chrome.runtime.sendMessage(e,s=>{chrome.runtime.lastError?t(chrome.runtime.lastError):a(s)})}),x=e=>new Promise(async(a,t)=>{if(!chrome?.tabs)throw new Error("chrome.tabs is not available");let s=typeof e.tabId=="number"?e.tabId:(await n()).id;chrome.tabs.sendMessage(s,e,o=>{chrome.runtime.lastError?t(chrome.runtime.lastError):a(o)})}),g=e=>i(e,l),b=g,c=m,P=c;export{b as relay,g as relayMessage,x as sendToActiveContentScript,l as sendToBackground,c as sendToBackgroundViaRelay,P as sendViaRelay}; | ||
var n=async()=>{let[e]=await chrome.tabs.query({active:!0,currentWindow:!0});return e},r=(e,a)=>!a.__internal&&e.source===globalThis.window&&e.data.name===a.name&&(a.relayId===void 0||e.data.relayId===a.relayId);var i=(e,a)=>{let t=async s=>{if(r(s,e)&&!s.data.relayed){let o={name:e.name,relayId:e.relayId,body:s.data.body},d=await a?.(o);window.postMessage({name:e.name,relayId:e.relayId,body:d,relayed:!0})}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},m=e=>new Promise((a,t)=>{window.postMessage(e),window.addEventListener("message",s=>{r(s,e)&&s.data.relayed&&a(s.data.body)})});var l=e=>new Promise((a,t)=>{if(!chrome?.runtime)throw new Error("chrome.runtime is not available");chrome.runtime.sendMessage(e,s=>{chrome.runtime.lastError?t(chrome.runtime.lastError):a(s)})}),g=e=>new Promise(async(a,t)=>{if(!chrome?.tabs)throw new Error("chrome.tabs is not available");let s=typeof e.tabId=="number"?e.tabId:(await n()).id;chrome.tabs.sendMessage(s,e,o=>{chrome.runtime.lastError?t(chrome.runtime.lastError):a(o)})}),b=g,c=e=>i(e,l),P=c,y=m,f=y;export{P as relay,c as relayMessage,b as sendToActiveContentScript,l as sendToBackground,y as sendToBackgroundViaRelay,g as sendToContentScript,f as sendViaRelay}; |
@@ -1,2 +0,2 @@ | ||
import { a as PortName } from './types-d2c8c75d.js'; | ||
import { a as PortName } from './types-52ab4931.js'; | ||
@@ -3,0 +3,0 @@ declare const getPort: (name: PortName) => chrome.runtime.Port; |
@@ -1,2 +0,2 @@ | ||
import { P as PlasmoMessaging } from './types-d2c8c75d.js'; | ||
import { P as PlasmoMessaging } from './types-52ab4931.js'; | ||
@@ -3,0 +3,0 @@ /** |
{ | ||
"name": "@plasmohq/messaging", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Type-safe, zero-config messaging library for modern browser extensions", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18703
145