How to reroute console.log to document.write [duplicate] - javascript

This question already has answers here:
JavaScript Document.Write Replaces All Body Content When Using AJAX
(7 answers)
Closed 9 years ago.
I would like to create a "Console" app for JavaScript so that messages are written to the screen.
console.log("Rerouting log messages to the screen");
console.log = function (message) { document.writeln(message); };
console.log("Log messages are now routed to the screen.");
This works, except that each time something is written to the screen, it wipes out any existing content.
Is there a way to do this?

This is how document.write works. To avoid this you should use document.createElement and document.body.appendChild for example.
For example you can try this code:
console.log = function (message) {
var p = document.createElement( 'p' );
p.innerHTML = message;
document.body.appendChild( p );
};
Read more about document.write at MDN.

Related

Trying to write in text area using selenium [duplicate]

This question already has answers here:
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard: while sending text to FirstName field in Facebook
(5 answers)
Closed 2 years ago.
I'm trying to comment in Instagram post by using selenium (java). Instagram Comment box in textArea, How can I add comment?
I did this...
WebElement cmdbox = driver.findElement(By.tagName("textarea"));
cmdbox.clear();
Thread.sleep(2000);
cmdbox.sendKeys("sample text");
Thread.sleep(3000);
I got this error
ElementNotInteractableException is Thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.
aka it is in the DOM but you cannot click on it yet. There are a few ways to solve this using ExpectedConditions.elementToBeClickable() is one way. A helper method could help before calling click.
public static void waitForElementClickable(WebDriver webdriver, By by, long timeout) {
WebDriverWait wait = new WebDriverWait(webdriver, timeout);
wait.until(ExpectedConditions.elementToBeClickable(by));
}
WebDriverWait wait = new WebDriverWait(webdriver, timeout); //15 can be ideal for timeout
//the code to clear area
wait.until(ExpectedConditions.elementToBeClickable(By.tagName("textarea"))).clear();
//the code to sendKeys
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("textarea"))).sendKeys("sample text");
if this does not work, you can use By.CssSelector("textarea.Ypffh") as another alternative to the By.tagName("textarea")

Is there a way to ban alerts? [duplicate]

This question already has answers here:
JavaScript: Overriding alert()
(12 answers)
Closed 2 years ago.
I have many JavaScript functions for a program, all of which contain alerts notifying the user of certain things. I wanted to give the user the option to ban all of those alerts, without having to rewrite all of the functions without the alerts. Is there a way to ban alert? Maybe something like this:
<button onclick = "alertBan()">Alert Ban</button>
function alertBan(){
alerts = false;
}
Just assign an empty function to alert.
function alertBan(){
alert = function(){};
}
If you might need to reenable alerts later on, you can store window.alert in a variable when the page loads.
const oldAlert = alert;
function alertBan(){
alert = function(){};
}
function enableAlerts(){
alert = oldAlert;
}
Store the original alert for perhaps later use. Replace the alert through empty function or with setted parameter to write on console.
If need the normal alert back take the stored one back.
alertOrig = alert;
function alertBan(consoleLog){
alert = function(e) {
if (consoleLog) console.log(e);
}
}
function alertEnable(){
if ( alertOrig )
alert=alertOrig;
}
alertBan();
alert('No console + no alert');
alertBan(true);
alert('With console');
alertEnable();
alert('Normal alert');

Simulate long script on page load [duplicate]

This question already has answers here:
Calling functions with setTimeout()
(6 answers)
Closed 3 years ago.
I am trying to simulate a script that takes a long time to run on page load.
I tried this:
window.onload = function() {
setTimeout(alert("Page Rendered"), 200000);
};
But alert message happens instantly.
What am I doing wrong?
Check the function(). docs
window.onload = function() {
setTimeout(function(){alert("Page Rendered")}, 200000);
};

Intercept request and modify response [duplicate]

This question already has answers here:
Modify HTTP responses from a Chrome extension
(8 answers)
Closed 4 years ago.
I want to make a Chrome extension that when the user requests mysite/file.js it will replace the alert('hi') to alert('bye'). I can't figure this out I am new at Chrome Extensions. Thanks.
I think there might be better ways to achieve what you need, depending on your use case.
For example, you could inject a content script that overrides alert on the page world.
context.js
const script = document.createElement('script');
script.innerHTML = `
const originalAlert = window.alert;
window.alert = (msg) => {
originalAlert(msg === 'hello' ? 'bye' : msg);
};
`;
document.body.appendChild(script);

JavaScript only works with a breakpoint set in browser debugger [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I have a basic function in JavaScript that simply takes some pre-set values and puts them onto the screen by the use of a pre-made function. When I breakpoint the first line of what it is I'm doing, the page loads fine, as expected, but as soon as I remove that breakpoint, none of the information is set. and the page is blank.
this.QuizSelection = function () {
// Fill the ID's with the right info
app.SetBackground('head', this.HeroImage);
console.log('1 ' + this.HeroImage);
app.LoadInnerHTML('breadcrumbs', 'Home / ' + this.Title);
app.LoadInnerHTML('quizSelectionTitle',this.Title);
console.log('2 ' + this.Title);
app.LoadInnerHTML('quizSelectionIntro',this.Introduction);
console.log('3 ' + this.Introduction);
// Show the Quiz Selection and Heading
app.ShowSection('head');
app.ShowSection('quizSelection');
console.log('Quiz Selection');
}.bind(this);
The functions inside that (SetBackground and LoadInnerHTML) are just small one line functions that change the inner html and the set a background image.
// Change Inner HTML
this.LoadInnerHTML = function (id, html) {
var d = document.getElementById(id);
d.innerHTML = html;
}
// Set Background Image
this.SetBackground = function (id, image) {
document.getElementById(id).style.backgroundImage = 'url(image)';
}
I can't understand why it wouldn't work when the breakpoint isn't on. Clearly it does work, because everything is fine with the breakpoint on, but then when it's off the result I get output to the console is:
1
2
3 undefined
Quiz Selection
You have a race condition.
The act of hitting a breakpoint makes your code wait for the async JSON load to complete. Without the breakpoint, the code trying to read the JSON is executing before the JSON has actually loaded.
See How do I return the response from an asynchronous call? for how to fix this issue.
You have console.log statements in your code. When the debugger is not on, console object does not exist (this is true for IE not for Chrome to the best of my knowledge), thus your javascript code execution fails.

Categories

Resources