Chrome extension action on shutdown - javascript

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

Related

Is the debugger attached?

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.

How to deal with the overlay Paused in debugger while automated test execution using Selenium

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.

Chrome timeout for execution onbeforeunload handler

I need to implement a feature that checks for changed content before window close and display a predefined warning message if so.
What I did is simply register my handler as follows:
window.addEventListener("beforeunload", checkValueChanged, true);
In the very handler I initiate a synchronous request (using jsf.ajax.request()) to submit all pending data and calculate changes.
The whole solution seems to work pretty well for most cases, but when request takes a little bit longer (300ms?) the browser brutally kills the script without waiting for the response.
It happens only for Chrome (tested on 48.0.2564.116 m) - FF and IE waits for the request to complete. What`s more when I enable developer tools in Chrome it also works.
I suspect that this is an intended Chrome behavior based on a fixed timeout for onbeforeunload handlers execution time designed to protect user from various tricks and hacks on some sites (especially in porno ones :)).
My question is: have you ever encountered and handled somehow such situation? Is this "feature" documented somewhere in Chrome documentation or maybe its a bug? If its a feature can it be locally reconfigured or disabled?
I will be grateful for any hints :)

How to run a function on close in a Chrome App?

I'm having an issue where closing my Chrome App too quickly after completing a stage corrupts the save data. This is because it's in the middle of saving some data.
Is there any way to keep the Chrome App open for a few seconds after the user presses close or alt+f4? Or another solution that maybe has a popup telling the user that the app is saving and will close automatically?
You can use the chrome.runtime.onSuspend.addListener( function ) method to specify some code to run when the user quits the app. As stated in the chrome documentation, any code running or triggered in the background/event page will keep the program alive for a short time to allow these processes to end. Remember to register this event listener method in your background/event page before the user has a chance to quit.
Additional information here:
https://developer.chrome.com/extensions/runtime#event-onSuspend

How to run script in Firefox when a tab was closed?

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.

Categories

Resources