Skip to content

Instantly share code, notes, and snippets.

@sreekanthgoud
Created July 6, 2023 09:31
Show Gist options
  • Select an option

  • Save sreekanthgoud/89a5718f92a69f50be4df2d77aebd2fb to your computer and use it in GitHub Desktop.

Select an option

Save sreekanthgoud/89a5718f92a69f50be4df2d77aebd2fb to your computer and use it in GitHub Desktop.
Javascript get alertbox
// Wrap the original window.alert function...
const windowAlert = window.alert;
window.alert = function(message) {
console.log(`window.alert called with message: ${message}`);
return windowAlert(message);
};
alert('FOO');
// Console Output:
// window.alert called with message: FOO
// ========================================================
// Wrap the original window.prompt function...
const windowPrompt = window.prompt;
window.prompt = function(message) {
console.log(`window.prompt called with message: ${message}`);
const input = windowPrompt(message);
console.log(`user entered: ${input}`);
return input;
};
prompt('BAR');
// Console Output:
// window.prompt called with message: BAR
// user entered: xxx
// ========================================================
// Wrap the original window.confirm function...
const windowConfirm = window.confirm;
window.confirm = function(message) {
console.log(`window.confirm called with message: ${message}`);
const choice = windowConfirm(message) ? 'ok' : 'cancel';
console.log(`user clicked: ${choice}`);
return choice;
};
confirm('BAZ');
// Console Output:
// window.confirm called with message: BAZ
// user clicked: ok (or 'cancel' if you click 'cancel')
// reference by https://stackoverflow.com/questions/22578897/how-to-get-text-from-alert-box
@sreekanthgoud
Copy link
Author

window.workspace = window.alert;
window.alert = function(msg) {
console.log('Alert message:', msg);
if (msg.toLowerCase().includes('valid')) {
//write code
};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment