UNPKG

8.31 kBMarkdownView Raw
1<h3 align="center">
2 <img src="graphics/logo.png?raw=true" alt="Plattar Logo" width="600">
3</h3>
4
5[![install size](https://packagephobia.com/badge?p=@plattar/context-messenger)](https://packagephobia.com/result?p=@plattar/context-messenger)
6[![Minified](https://badgen.net/bundlephobia/min/@plattar/context-messenger)](https://bundlephobia.com/result?p=@plattar/context-messenger)
7[![MinZipped](https://badgen.net/bundlephobia/minzip/@plattar/context-messenger)](https://bundlephobia.com/result?p=@plattar/context-messenger)
8[![NPM](https://img.shields.io/npm/v/@plattar/context-messenger)](https://www.npmjs.com/package/@plattar/context-messenger)
9[![Codacy Badge](https://api.codacy.com/project/badge/Grade/95f7fb8235314e93b2f462e13dfb4034)](https://app.codacy.com/gh/Plattar/context-messenger?utm_source=github.com&utm_medium=referral&utm_content=Plattar/context-messenger&utm_campaign=Badge_Grade)
10[![License](https://img.shields.io/npm/l/@plattar/context-messenger)](https://www.npmjs.com/package/@plattar/context-messenger)
11
12_context-messenger_ allows defining and calling functions and variables across multiple iframes.
13
14### _Installation_
15
16- Install using [npm](https://www.npmjs.com/package/@plattar/context-messenger)
17
18```console
19npm install @plattar/context-messenger
20```
21
22### _Examples_
23
24- Instructions on how to run example code in [Examples Folder](https://github.com/Plattar/context-messenger/tree/master/examples)
25
26### _How to execute functions from an iframe on the parent page_
27
28- Define a function in the parent page that executes and returns a result
29
30This function will then become available to be executed from other _context-messenger_ frameworks from either the parent or child iframes.
31
32```javascript
33Plattar.messenger.self.sum = (arg1, arg2) => {
34 return arg1 + arg2;
35};
36```
37
38From within the iframe itself that has the _context-messenger_ framework, the above function can be executed as follows. Notice that the function is executed asynchronously and handled via a Promise chain.
39
40```javascript
41Plattar.messenger.parent.sum(1,4).then((result) => {
42 console.log(result); // this will print 5
43}).catch((err) => {
44 console.error(err);
45});
46```
47
48- Define a function in the current context that executes and returns a result asynchronously
49
50The _context-messenger_ framework can also handle asynchronous functions using Promises. Below we define a function that performs a sum asynchronously.
51
52```javascript
53Plattar.messenger.self.sumDelayed = (arg1, arg2) => {
54 // perform the sum after 3 seconds and return the result
55 return new Promise((accept, reject) => {
56 setTimeout(() => {
57 accept(arg1 + arg2);
58 }, 3000);
59 });
60};
61```
62
63From within an iframe itself that has the _context-messenger_ framework, the above function can be executed as follows. Notice that nothing changes with the function execution and the results are still handled using a Promise chain. In this instance, the Promise will execute after 3 seconds.
64
65```javascript
66Plattar.messenger.parent.sumDelayed(1,4).then((result) => {
67 console.log(result); // this will print 5 after 3 seconds
68}).catch((err) => {
69 console.error(err);
70});
71```
72
73### _How to execute functions from the parent page inside of the iframe_
74
75- Define an iframe on the parent page that has the _context-messenger_ framework and provide some ID
76
77```html
78<iframe id="frame1" src="./your-nested-page.html"></iframe>
79```
80
81- Define a function inside _your-nested-page.html_ file using the _context-messenger_ that returns the page background color
82
83```javascript
84Plattar.messenger.self.getBackgroundColor = () => {
85 return document.body.style.backgroundColor;
86};
87```
88
89- To execute the function inside of _frame1_ do the following
90
91```javascript
92Plattar.messenger.frame1.getBackgroundColor().then((result) => {
93 console.log(result); // prints the background color of the iframe
94}).catch((err) => {
95 console.error(err);
96});
97```
98
99_context-messenger_ is designed to automatically initialise itself from other instances of _context-messenger_ that exist inside iframes and/or the parent page. Because this process is asynchronous, sometimes the iframe is not ready before calling its functions. To fix this, listen for the onload event for your desired iframe as follows.
100
101```javascript
102Plattar.messenger.onload("frame1", () => {
103 // iframe with ID of frame1 has finished loading. If this is not called
104 // then the iframe might not have a Messenger framework
105});
106```
107
108The same can be done for the parent page to ensure its loaded before iframes can call parent functions
109
110```javascript
111Plattar.messenger.onload("parent", () => {
112 // the parent page has finished loading. If this is not called
113 // then the page might not have a parent page or the parent does not
114 // have a Messenger framework
115});
116```
117
118### _How to execute functions from the parent page inside of multiple iframes_
119
120- Define multiple iframes on the parent page that has the _context-messenger_ framework and provide some ID
121
122```html
123<iframe id="frame1" src="./your-nested-page.html"></iframe>
124<iframe id="frame2" src="./your-nested-page.html"></iframe>
125<iframe id="frame3" src="./your-nested-page.html"></iframe>
126```
127
128- Define a function inside _your-nested-page.html_ file using the _context-messenger_ that returns the page background color
129
130```javascript
131Plattar.messenger.self.getBackgroundColor = () => {
132 return document.body.style.backgroundColor;
133};
134```
135
136Sometimes its easier to just call the same function on all available iframes on the page at the same time. The _context-messenger_ provides a broadcast functinality that can handle the function call and resolution for you.
137
138```javascript
139// broadcast will call getBackgroundColor() on frame1, frame2 and frame3 automatically.
140// the Promise will resolve when all iframes resolve or fail the function call
141Plattar.messenger.broadcast.getBackgroundColor().then((results) => {
142 // results contains the returned data from all 3 iframes
143 results.forEach((result) => {
144 if (result.status == "fulfilled") {
145 console.log(result.value); // prints the returned color
146 }
147 });
148});
149```
150
151For more details on how this is handled see [Promise.allSettled](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)
152
153### _How to store and read variables from iframes_
154
155Storing of variables is done using _context-messenger_ `memory` module. All variables are available to be accessed in all contexts. Note that the memory module does not allow storing functions. Use messenger module for that.
156
157- To store a temporary variable use the following. Temporary variables are not persistent
158 and will be cleared when the javascript context ends.
159
160```javascript
161Plattar.memory.temp.my_variable = "hello world!";
162```
163
164- Access the variable as follows from any context that has _context-messenger_
165
166```javascript
167console.log(Plattar.memory.temp.my_variable); // prints hello world!
168```
169
170- To store a persistent variable use the following. Persistent memory uses [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) behind the scenes.
171
172```javascript
173Plattar.memory.perm.my_variable = "hello world!";
174```
175
176- Access the variable as follows from any context that has _context-messenger_
177
178```javascript
179console.log(Plattar.memory.perm.my_variable); // prints hello world!
180```
181
182### _How to watch for variable changes_
183
184_context-messenger_ `memory` module provides a `watch` function that allows detecting when the variable has changed.
185
186- Watch a `temp` or `perm` variable example
187
188```javascript
189
190// watch temp variable changes for my_temp_variable
191Plattar.memory.temp.watch("my_temp_variable", (oldVar, newVar) => {
192 console.log("old variable was " + oldVar);
193 console.log("new variable was " + newVar);
194});
195
196// watch perm variable changes for my_perm_variable
197Plattar.memory.perm.watch("my_perm_variable", (oldVar, newVar) => {
198 console.log("old variable was " + oldVar);
199 console.log("new variable was " + newVar);
200});
201
202// set initial variables
203Plattar.memory.temp.my_temp_variable = "hello world!";
204Plattar.memory.perm.my_perm_variable = "hello world!";
205
206// initiate a variable change
207Plattar.memory.temp.my_temp_variable = "hello world again!";
208Plattar.memory.perm.my_perm_variable = "hello world again!";
209```
\No newline at end of file