I would like to know, whether I'm currently debugging my javascript code?
Background: (Why I need to know)
I have created a page, that spawns a Worker.
When I run this page and press F12 to debug, I set a breakpoint in the Worker. This breakpoint will then get hit when I press my button to run the Worker (so far all good).
Now to prevent that the Worker runs forever I have created a simple timer in the main page, that terminates the Worker after 2 seconds, like this:
timer = setTimeout(function ()
{
worker.terminate();
worker = NewWorker();
}, 2000);
Now the problem: When the breakpoint is hit in Worker, the timer fires 2 seconds later (since the main page thread is still running independantly) and terminates the Worker. So I can't debug the Worker.
My plan is to simply disable (or not start) the timer, when I'm debugging.
So, Is there a way to know, if a debugger is attached?
Update:
My current workaround is to simply comment out the timer code, when I want to debug the Worker, but I would like to do a test.
Update2:
May be a debugger is always attached in the browser, but it's only active when it's visible. Breakpoints are only hit when you have the debugger window open.
So, is there a way to test that?
Well, it seems, there's no answer to this question.
Ideally I would want the browser to break all threads when one thread hit a breakpoint (Visual Studio can do this, but not in javascript).
May be one day browsers will support this.
Update:
Now Visual Studio supports debugging Worker. It's still an experimental feature. You enable it using the weirdly named option in 'Options', 'Debugger':
Enable using the multi-target Javascript debugger for debugging Javascript in applicable targets (requires debugging restart)
To debug a Worker, start debugging your page, then Visual Studio Solution Explorer will show show the Worker.js file. double-click and breakpoints.
However it has not really solved the problem with timers in the main thread firing when you hit a breakpoint in a Worker.
Related
Everytime I try to access this website and open google-chrome-devtools I am unable to inspect any of the elements through the Inspector as the UI is having an overlay along with a message Paused in debugger.
The upvoted and accepted answer of this discussion says to check the Source tab, check under the Event Listener Breakpoints panel if you've set any breakpoints under 'Mouse'. I have cross checked that none of the Sources -> EventListenerBreakpoint are set.
The upvoted and accepted answer of this discussion says to check if the little octagonal stop/pause sign (at lower left of Chrome "Sources") is colored (blue or purple). I am not sure why do I need to do that additionally for selected websites.
Snapshot:
The upvoted and accepted answer of this discussion speaks about the Manual Steps.
All the solutions seem to point towards the manual process. But this issue seems to me the root cause behind Selenium being unable to getPageSource().
Code trials:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
Output: Chrome opens but doesn't navigates to the url.
So my questions are:
In which case can Paused in debugger error occurs?
Is it an error from the frontend development?
How can I bypass this error during the Automated Tests through Selenium?
In which cases can the Paused in debugger error occur?
Anytime you are accessing this page with the dev tools open. The debugger; line will pause javascript execution, but browsers will ignore it if the dev tools are closed.
Is it an error from the frontend development?
In this case, no--they're deliberately trying to keep you out. The purpose of this function is to pause execution and then redirect your browser to a different page if it takes longer than 100ms to resume. I would speculate that this is designed to interfere with automated crawlers like selenium, because a regular user wouldn't be affected and a human developer can just hack around it.
How can I bypass this error during the Automated Tests through Selenium?
My first recommendation would be to try running Selenium headlessly, if that's an option. If not, use the hotkey to resume execution (F8). You can use whatever method you like for generating a keypress; with the java.awt package it will look something like this:
Robot robot = null;
try
{
robot = new Robot();
}
catch(Exception e)
{
//handle failure
}
robot.keyPress(KeyEvent.VK_F8);
Remember that you have to trigger this within 100ms, so use whatever logic you like to detect the block and respond quickly. If you just want something quick and dirty, I would just make it spam F8 keypresses every 50ms for a period of time until you're certain the page has loaded.
EDIT: On further investigation, this page is extremely messy and hostile to anyone with the dev tools open. There is not one but several functions that trigger debugger;and they get called repeatedly on a timer for as long as you're on the page. Running headlessly seems like the best choice, unless you want to continue spamming F8 for the entire session.
I have some memory-intensive WebGL code which runs in both desktop and mobile devices. The user is able to add/remove WebGL modules thereby changing the memory requirements for the page. On low-power devices its possible for the user to get themselves into a bad state. They try to load the page, but the page runs out of memory due to too many modules loading. The page crashes. I am working on providing the user with a means of recovering from this scenario.
Some high-level pseudocode for my recovery solution looks like:
During init, write a flag to sessionStorage for each module. The flag indicates the given module has not loaded yet.
If a module already has a flag in storage - don't try to load it.
For every other module - attempt to load.
As each module loads, clear its flag.
Overall, this code has the effect of preventing the browser from attempting to load all modules every time.
However, I also clear a module's load flag if the user refreshes the page. A manual page refresh is not a browser crash -- and thus all modules should continue to try and load:
window.addEventListener('unload', function() { /* clear flags here */ });
This all seems to work fine in desktop environments, but in testing mobile devices I've found that often times the browser refreshes the page automatically instead of crashing. In Safari, I see an error message:
"A problem occurred with this webpage so it was reloaded."
Is it possible to detect a manual page refresh vs a 'browser error recovery' refresh?
To the best of my knowledge the answer is, 'No.' as I only have a window unload event to work with in both scenarios.
At our company we are using a web application with shared licenses. Unfortunately if someone simply closes the tab the application is running in it wont release the license lock. I am wondering whether it is possible to run/trigger a scipt when a Firefox tab is closed, so I could automatically release the licenses? I think greasemonkey might be able to do this, but I haven't found a solution yet.
There is both window.onbeforeunload and window.onunload, which are used differently depending on the browser. You can assing them either by setting the window properties to functions, or using the .addEventListener:
window.onbeforeunload = function(){
// Do something
}
// OR
window.addEventListener("beforeunload", function(e){
// Do something
}, false);
Usually, onbeforeunload is used if you need to stop the user from leaving the page (ex. the user is working on some unsaved data, so he/she should save before leaving).
You can try to release locks in unload events, as Bcfm suggested in his answer, but what if browser or computer simply crashes? Or script takes too long to execute and gets killed by browser anyway?
Another approach would be to make the site constantly ping license server (i.e. every 10 seconds) so that lock is hold until there is no ping for proportional amount of time (i.e. 30 seconds). This way the license lock is freed in all cases.
Of course this may be not relevant to your scenario, it's just a suggestion.
A few months ago while developing application for Chromecast and Android devices I have used
window.location.reload(true)
a lot. It was used to reload JavaScript and images in Receiver.
Recently I got back to develop app and now when I use this command, I get undefined in console. Nothing reloads, Sender still can communicate with Receiver. I launch this command again and then, debug is detached, Chromecast crashes and it needs to be reloaded...After it reloads, I launch Sender which launches Receiver and still cache is not cleared. So I use command again and it works. But only one time. Second time, the same situation.
Any ideas what is wrong?
My Chromecast uses 220620 firmware version.
EDIT
Further investigation. Problem appears when I don't do anything for some time, for instance 50 seconds. If I do it right away or in a few seconds, it's OK.
Maybe it could be the timeout when debugging. Try to add the following in your js:
castReceiverManager.start({maxInactivity: 600});
And that will wait 10 minutes.
I was reading this question again, and I should say that what I use the most is to just hit F5 on devtools. Also make sure you have the option "Disable cache (while DevTools is open)" enable for better results.
I'm writing a very simple Chrome extension to automatically clear my history as this isn't a built in feature. I've got it functional but just want to confirm I've done things that make sense before I wrap it up.
First - is there a reason to do a chrome.history.deleteAll? This doesn't actually appear to interface with URL browsing history, instead you need to use chrome.browsingData.* for that. Should I do the former anyway?
Next - after searching and searching, there appears to be no way to execute a function on shutdown. The closest I got to real info on this was someone saying when Chrome quits it terminates all extensions without prejudice. Am I missing a way to clean up history on shutdown?
Finally - as I can't cleanup on shutdown, I'm cleaning up on startup, using window.onload in the background page. chrome.runtime.onStartup did not reliably work for me while window.onload did - is this an okay way to do things?
Thanks in advance for any help.
you are right, there's no way to execute anything on Chrome shutdown. Chrome doesn't have an onClose event. Not only literally, but metaphorically speaking: When Chrome closes, it doesn't wait for any extension to close.
You can hang from chrome.windows.onRemoved and wait for the last window to close, but if Chrome is shutting down, it's not guaranteed your extension will have time to run it. Same thing with onSuspend, or whatever you find.
So, if your implementation works, it won't be guaranteed to work in other Chrome.
About chrome.history.deleteAll, I have the same problem, and will try with chrome.browsingData, a more modern and complete way to deal with browsing data, including history.
This document lists an onSuspend event:
Clean-up before app closes
The app runtime sends the onSuspend() event to the event page before
unloading it. Your event page can listen out for this event and do
clean-up tasks before the app closes.
Once this event is fired, the app runtime starts the process of
closing the app: all events stop firing and JavaScript execution is
halted. Any asynchronous operations started while handling this event
are not guaranteed to complete. Keep the clean-up tasks synchronous
and simple.
chrome.runtime.onSuspend.addListener(function() { // Do some simple
clean-up tasks. });
This document says the onSuspend is also available for extensions