Skip to content

Commit

Permalink
Avoid uncaught exception in iframe if WebNFC is not supported
Browse files Browse the repository at this point in the history
Part of #11269.
  • Loading branch information
zcorpan committed May 31, 2018
1 parent 730415d commit 872ff88
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions web-nfc/nfc_push.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,25 @@
promise_test(() => {
return new Promise((resolve,reject) => {
let iframe = document.createElement('iframe');
iframe.srcdoc = '<script>' +
' window.onmessage = message => {' +
' if (message.data === "Ready") {' +
' let onSuccess = () => { parent.postMessage("Failure", "*"); };' +
' let onError = error => {' +
' if (error.name == "SecurityError") {' +
' parent.postMessage("Success", "*");' +
' } else {' +
' parent.postMessage("Failure", "*");' +
' }' +
' };' +
' navigator.nfc.push("Test").then(onSuccess, onError);' +
' }' +
' };' +
'<\/script>';
iframe.srcdoc = `<script>
window.onmessage = message => {
if (message.data === "Ready") {
let onSuccess = () => { parent.postMessage("Failure", "*"); };
let onError = error => {
if (error.name == "SecurityError") {
parent.postMessage("Success", "*");
} else {
parent.postMessage("Failure", "*");
}
};
try {
navigator.nfc.push("Test").then(onSuccess, onError);
} catch(e) {
parent.postMessage("Failure", "*");
}
}
};
<\/script>`;
iframe.onload = () => iframe.contentWindow.postMessage('Ready', '*');
document.body.appendChild(iframe);
window.onmessage = message => {
Expand Down

0 comments on commit 872ff88

Please sign in to comment.