(function() { window.spawn = window.spawn || function(gen) { function continuer(verb, arg) { var result; try { result = generator[verb](arg); } catch (err) { return Promise.reject(err); } if (result.done) { return result.value; } else { return Promise.resolve(result.value).then(onFulfilled, onRejected); } } var generator = gen(); var onFulfilled = continuer.bind(continuer, 'next'); var onRejected = continuer.bind(continuer, 'throw'); return onFulfilled(); }; window.showModalDialog = window.showModalDialog || function(url, arg, opt) { url = url || ''; //URL of a dialog arg = arg || null; //arguments to a dialog opt = opt || 'dialogWidth:300px;dialogHeight:200px'; //options: dialogTop;dialogLeft;dialogWidth;dialogHeight or CSS styles var caller = showModalDialog.caller.toString(); var dialog = document.body.appendChild(document.createElement('dialog')); dialog.setAttribute('style', opt.replace(/dialog/gi, '')); dialog.innerHTML = '×'; document.getElementById('dialog-body').contentWindow.dialogArguments = arg; document.getElementById('dialog-close').addEventListener('click', function(e) { e.preventDefault(); dialog.close(); }); dialog.showModal(); //if using yield or async/await if(caller.indexOf('yield') >= 0 || caller.indexOf('await') >= 0) { return new Promise(function(resolve, reject) { dialog.addEventListener('close', function() { var returnValue = document.getElementById('dialog-body').contentWindow.returnValue; document.body.removeChild(dialog); resolve(returnValue); }); }); } //if using eval var isNext = false; var nextStmts = caller.split('\n').filter(function(stmt) { if(isNext || stmt.indexOf('showModalDialog(') >= 0) return isNext = true; return false; }); dialog.addEventListener('close', function() { var returnValue = document.getElementById('dialog-body').contentWindow.returnValue; document.body.removeChild(dialog); nextStmts[0] = nextStmts[0].replace(/(window\.)?showModalDialog\(.*\)/g, JSON.stringify(returnValue)); eval('{\n' + nextStmts.join('\n')); }); throw 'Execution stopped until showModalDialog is closed'; }; })();