-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold-qubixia.v1.js
153 lines (134 loc) · 4.67 KB
/
old-qubixia.v1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// In the parent window
//MAIN
const MainUrl = "https://qubixia.bubbleapps.io/version-test/"
function update_page_content(url){
document.getElementById("page-content").src = MainUrl+url;
}
function onIncompletePaymentFound(payment) {
alert('incomplete:'+payment);
};
function user_login_complete(data){
var message = {
command: "loginResult", // A command to help you identify the response
data: data
};
// Send the message to the iframe using the contentWindow property
document.getElementById("page-content").contentWindow.postMessage(message, "*");
}
function pi_login(){
Pi.authenticate(['payments','username'], onIncompletePaymentFound).then(function(auth) {
//Pi.authenticate(['payments'], onIncompletePaymentFound).then(function(auth) {
//alert('authenticate:'+auth);
Pi.LoggedInUser = auth;
user_login_complete(auth);
// Load the main page content using fetch and innerHTML
//update_page_content("home");
}).catch(function(error) {
Pi.LoggedInUser = null;
if (confirm('Failed to authenticate, please refresh the page to try again:'+error)) {
window.location.reload();
}
});
}
// Create a function to be executed when the post message is received
function executeFunction(data) {
// Get the function name and the arguments
const functionName = data.function;
const args = data.args;
// Check if the function exists in the parent window
if (typeof window[functionName] === "function") {
// If so, execute it with the given arguments
window[functionName].apply(null, args);
}
}
// Create a post message listener
window.addEventListener("message", function (event) {
// Check if the message is from the iframe
if (event.origin == "*") {
// Check if the message has a 'command' property
if (event.data.hasOwnProperty("command")) {
// Get the command and the data
const command = event.data.command;
const data = event.data.data || {};
// Execute the appropriate function
switch (command) {
case "executeFunction":
executeFunction(data)
break;
default:
// Handle other commands as needed
}
}
}
});
// Initialize the Pi SDK
Pi.init({ version: "2.0", sandbox: false });
// A generic function to send and receive post messages
function postMessageHandler(window, targetWindow, targetOrigin, message, callback) {
// Check if the window and the target window are valid
if (window && targetWindow) {
// Send the message to the target window
targetWindow.postMessage(message, targetOrigin);
// Listen for the message from the target window
window.addEventListener("message", function (event) {
// Check if the message is from the target origin
if (event.origin == targetOrigin) {
// Check if the message has a 'command' property
if (event.data.hasOwnProperty("command")) {
// Get the command and the data
const command = event.data.command;
const data = event.data.data || {};
// Handle different commands or actions
switch (command) {
case "loginResult":
// Do something with the login result
console.log(data);
break;
case "sendImage":
// Do something with the image data
console.log(data);
break;
case "executeFunction":
// Do something with the function name and the arguments
console.log(data);
break;
default:
// Handle other commands as needed
}
// Execute the callback function if provided
if (typeof callback === "function") {
callback(data);
}
}
}
});
}
}
// Get the reference to the iframe element
var iframe = document.getElementById("myIframe");
// Wait for the iframe's document to load before sending the message
iframe.contentDocument.addEventListener("DOMContentLoaded", function() {
// Create a message object with the command and the data
var message = {
command: "executeFunction",
data: {
// Specify the function name and the arguments
function: "pi_login",
args: [
// Add any arguments you want to pass to the function
]
}
};
// Use the post message handler function to send and receive the message
postMessageHandler(
window, // The current window object
iframe.contentWindow, // The target window object
"*", // The target origin
message, // The message object
function (data) {
// A callback function to execute after receiving a message
// Do something with the data
console.log(data);
}
);
});