use of com.codename1.ui.TextSelection.Span in project CodenameOne by codenameone.
the class BrowserComponentPostMessageSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World", new BorderLayout());
BrowserComponent bc = new BrowserComponent();
/* A test page to demonstrate posting messages cross-domain. The source of this page is:
<?php
// This header is really just meant to break the X-Frame-Options sameorigin header
// that is sent by default by Amazon Lightsail.
// In order ot display a page in an iFrame, you may need to fuss with
// the X-Frame-Options header.
// More at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
header('X-Frame-Options: allow-from http://localhost:58494');
?>
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
window.addEventListener("message", function(event) {
var div = jQuery('<div></div>');
jQuery(div).text(event.data);
jQuery('body').append(div);
}, false);
function postToCN1(msg) {
if (window.cn1PostMessage) {
window.cn1PostMessage(msg);
} else {
window.parent.postMessage(msg, '*');
}
}
</script>
</head>
<body>
<h1>Listening to Messages...</h1>
<p><input id='msg' type="text" value="Message To CN1"/></p>
<p><button onclick="jQuery('body').append(jQuery('<span>foo</span>')); postToCN1(jQuery('#msg').val(), '*')">Send</p>
</body>
</html>
*/
bc.setURL("https://weblite.ca/cn1tests/post_message_test.php");
// Register a listener to receive messages sent from the BrowserComponent's webpage.
// A simple javascript function to send a message to this handler from within your page
// is:
// function postToCN1(msg) {
// if (window.cn1PostMessage) {
// // Case 1: We are running in a native app in a WebView.
// window.cn1PostMessage(msg);
// } else {
// // Case 2: We are running in a Javascript app in an iframe
// window.parent.postMessage(msg, '*');
// }
// }
bc.addWebEventListener(BrowserComponent.onMessage, e -> {
CN.callSerially(() -> {
Log.p("Message: " + e.getSource());
Dialog.show("Here", (String) e.getSource(), "OK", null);
});
});
TextField message = new TextField("A sample message");
Button b = new Button("Send Message");
b.addActionListener(e -> {
// Send a message to the webpage.
// This can be received in the webpage using the 'message' event.
// E.g.
/*
window.addEventListener("message", function(event) {
var div = jQuery('<div></div>');
jQuery(div).text(event.data);
jQuery('body').append(div);
}, false);
*/
bc.postMessage(message.getText(), "https://weblite.ca");
// IMPORTANT: 2nd argument should be either '*' to target any origin
// or the origin of the web page in the BrowserComponent.
// See https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage for more information.
});
hi.add(CENTER, bc);
hi.add(BorderLayout.SOUTH, BoxLayout.encloseY(message, b));
hi.show();
}
Aggregations