onairos 0.1.400 → 1.0.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.
@@ -13,56 +13,66 @@ exports.sendDataToIframe = sendDataToIframe;
13
13
  * in the top right corner of the screen.
14
14
  */
15
15
 
16
+ /**
17
+ * Gets the URL for the iframe HTML file
18
+ * Works in both extension and web contexts
19
+ */
20
+ function getIframeUrl() {
21
+ if (typeof chrome !== 'undefined' && chrome.runtime) {
22
+ return chrome.runtime.getURL('data_request_iframe.html');
23
+ }
24
+ // For web context, use relative path
25
+ return '/data_request_iframe.html';
26
+ }
27
+
16
28
  /**
17
29
  * Opens a data request iframe in a new window positioned in the top right corner of the screen
18
- * This will replace the current window.message approach for data requests
30
+ * @returns {Window|null} The opened window or null if failed
19
31
  */
20
32
  function openDataRequestIframe() {
21
- // Notify that the terminal has been opened (will be used for internal tracking)
22
- window.postMessage({
23
- action: 'terminalOpened'
24
- }, '*');
33
+ try {
34
+ // Create iframe element
35
+ const iframe = document.createElement('iframe');
36
+ iframe.setAttribute('src', getIframeUrl());
37
+ iframe.setAttribute('title', 'Onairos Terminal');
38
+ iframe.classList.add('iframe-class');
25
39
 
26
- // Create iframe element
27
- const iframe = document.createElement('iframe');
28
- iframe.setAttribute('src', chrome.runtime.getURL('data_request_iframe.html'));
29
- iframe.setAttribute('title', 'Onairos Terminal');
30
- iframe.classList.add('iframe-class');
40
+ // Style the iframe
41
+ iframe.style.width = '100%';
42
+ iframe.style.height = '600px';
43
+ iframe.style.border = 'none';
44
+ iframe.style.backgroundColor = 'transparent';
31
45
 
32
- // Style the iframe
33
- iframe.style.width = '100%';
34
- iframe.style.height = '600px';
35
- iframe.style.border = 'none';
36
- iframe.style.backgroundColor = 'transparent'; // Changed from red to transparent for production
46
+ // Calculate position (top right corner)
47
+ const width = 400;
48
+ const height = 600;
49
+ const top = 10;
50
+ const left = window.innerWidth - width - 10;
37
51
 
38
- // Calculate position (top right corner)
39
- const width = 400;
40
- const height = 600;
41
- const top = 10;
42
- const left = window.innerWidth - width - 10;
43
-
44
- // Open a new window and append the iframe to it
45
- const newWindow = window.open(chrome.runtime.getURL('data_request_iframe.html'), 'Onairos Terminal', `width=${width},height=${height},top=${top},left=${left},resizable=no`);
52
+ // Open a new window and append the iframe to it
53
+ const newWindow = window.open(getIframeUrl(), 'Onairos Terminal', `width=${width},height=${height},top=${top},left=${left},resizable=no`);
54
+ if (!newWindow) {
55
+ throw new Error('Failed to open window - popup blocked');
56
+ }
46
57
 
47
- // If window opened successfully, append the iframe
48
- if (newWindow) {
49
58
  // Set some basic styles for the popup window
50
59
  newWindow.document.body.style.margin = '0';
51
60
  newWindow.document.body.style.padding = '0';
52
61
  newWindow.document.body.style.overflow = 'hidden';
53
62
 
54
- // Add event listener for window close to notify the main application
63
+ // Add event listener for window close
55
64
  newWindow.addEventListener('beforeunload', () => {
56
65
  window.postMessage({
57
66
  action: 'terminalClosed'
58
67
  }, '*');
59
68
  });
60
- } else {
61
- console.error('Failed to open Onairos Terminal window. Popup might be blocked by the browser.');
62
- // Fallback: Try to show a notification to the user about enabling popups
63
- alert('Please allow popups for this site to use the Onairos Terminal.');
69
+ return newWindow;
70
+ } catch (error) {
71
+ console.error('Error opening iframe:', error);
72
+ // Show user-friendly error message
73
+ alert('Unable to open Onairos Terminal. Please ensure popups are allowed for this site.');
74
+ return null;
64
75
  }
65
- return newWindow;
66
76
  }
67
77
 
68
78
  /**
@@ -71,33 +81,67 @@ function openDataRequestIframe() {
71
81
  */
72
82
  function closeDataRequestIframe(windowRef) {
73
83
  if (windowRef && !windowRef.closed) {
74
- windowRef.close();
75
- window.postMessage({
76
- action: 'terminalClosed'
77
- }, '*');
84
+ try {
85
+ windowRef.close();
86
+ window.postMessage({
87
+ action: 'terminalClosed'
88
+ }, '*');
89
+ } catch (error) {
90
+ console.error('Error closing iframe:', error);
91
+ }
78
92
  }
79
93
  }
80
94
 
81
95
  /**
82
- * Send data to the iframe window
96
+ * Send data to the iframe window with timeout
83
97
  * @param {Window} windowRef - Reference to the window object returned by openDataRequestIframe
84
98
  * @param {Object} data - Data to send to the iframe
99
+ * @returns {Promise} Promise that resolves when data is sent or rejects on timeout
85
100
  */
86
101
  function sendDataToIframe(windowRef, data) {
87
- if (windowRef && !windowRef.closed) {
88
- windowRef.postMessage(data, '*');
89
- }
102
+ return new Promise((resolve, reject) => {
103
+ if (!windowRef || windowRef.closed) {
104
+ reject(new Error('Iframe window is not available'));
105
+ return;
106
+ }
107
+ try {
108
+ windowRef.postMessage(data, '*');
109
+
110
+ // Set timeout for response
111
+ const timeout = setTimeout(() => {
112
+ reject(new Error('Timeout waiting for iframe response'));
113
+ }, 5000);
114
+
115
+ // Listen for acknowledgment
116
+ const messageHandler = event => {
117
+ if (event.data && event.data.action === 'dataReceived') {
118
+ clearTimeout(timeout);
119
+ window.removeEventListener('message', messageHandler);
120
+ resolve();
121
+ }
122
+ };
123
+ window.addEventListener('message', messageHandler);
124
+ } catch (error) {
125
+ reject(error);
126
+ }
127
+ });
90
128
  }
91
129
 
92
130
  /**
93
131
  * Set up a listener for messages from the iframe
94
132
  * @param {Function} callback - Function to handle messages from the iframe
133
+ * @returns {Function} Cleanup function to remove the listener
95
134
  */
96
135
  function listenForIframeMessages(callback) {
97
- window.addEventListener('message', event => {
98
- // Make sure the message is from our iframe
136
+ const messageHandler = event => {
99
137
  if (event.data && event.data.source === 'onairosIframe') {
100
138
  callback(event.data);
101
139
  }
102
- });
140
+ };
141
+ window.addEventListener('message', messageHandler);
142
+
143
+ // Return cleanup function
144
+ return () => {
145
+ window.removeEventListener('message', messageHandler);
146
+ };
103
147
  }