Just FYI I found and fixed my primary issue(s), although I have absolutely no idea why it doesn't work as expected when the Background Page DevTools is open.
Apologies for any formatting nuances within this post; I don't post to these groups often. I also left out links/references to my extension and tried to keep these solution descriptions more generic in nature.
In any case, a quick overview for context: My extension will alert you when you're on a website for a set amount of time. The issue was when the browser resumed focus (from another app), a page's timer would reset, instead of continuing. But! This never happened when the Background Page DevTools were open.
Knowing I couldn't break it with BG DevTools open, I troubleshot the primary issue by:
- In the background script, I did a `console.log` of just before where the issue occurs in the code (before the `confirm` dialog), and then turned on all my other `console logs` I have setup for debugging (key entry/exit points).
- I then opened the BG DevTools (knowing it was going to work) and watched for where it went after that particular console.log.
- I found it was going into the `onFocusChanged` listener.
- In there, I had removed a little _magic_ I didn't think applied for the longest time (b/c I keep DevTools near-always open), but now understand why it does.
## Broken and Fixed Code:
```
// Changing Application Windows
//
inFocus = true;
chrome.windows.onFocusChanged.addListener( function(window) {
if (window == chrome.windows.WINDOW_ID_NONE) {
inFocus = false; // Chrome does not have focus
} else {
// Works (Fix #1)
if (inFocus) { // _Magic_ Solution to not resetting the timer when the same tab is refocused !!!
checkAndSetTimer('windows.onFocusChanged inside Else > IF -> inFocus');
}
// A while back I had removed the `if` block above and just directly ran `checkAndSetTimer()`.
// I did this because, (with the DevTools always open,) it didn't appear it was necessary.
// Broken
// checkAndSetTimer('windows.onFocusChanged inside Else');
inFocus = true;
}
```
And! I realized a half hour later, because the `confirm` dialog box also receives focus briefly when one of its two buttons are clicked, another `inFocus = false` helped to solve an issue with the timer being reset on a new tab/page load.
## Additional Fixed Code:
```
chrome.alarms.onAlarm.addListener(function(alarm) {
if (confirm('Move on [Ok] or Continue [Cancel]')) {
// ...
} else {
inFocus = false; // Sprinkled in a little more _magic_ here (Fix #2)
chrome.browserAction.getBadgeText({}, function(result) { /* ... */ }
```
In doing all this, however, I still have no idea why the BG DevTools being open or closed has any effect on this. But the fixes work. :-)
## Steps to Create
Config: Inside my extension's popup, I setup a URL to be timed as '
google.com' with a one minute delay.
- Open browser Extensions page.
- Load/reload extension (if not already).
- Bring up the page to be timed (set in extension; e.g. google.com).
> That will start the timer. - Switch to your favorite text editor (or any non-browser app).
- Let the one minute expire, and the `confirm` dialog box will appear.
> Side note: When the confirm dialog is open, although previously changed, the badge color changes back to its default color. - Click 'Cancel' to stay on the site.
> Focus will shift back to your favorite text editor.
> You will see the badge text counter increase. - Switch back to the browser window with google.com.
With BG DevTools open, or 'closed with the fix in place':
- > The timer will stay at the current count
With BG DevTools closed (and no 'if(inFocus){}' logic in place):
- > The timer will reset to "o_o".
## Steps to Create - New Tab/Page Load
For the steps above, #3 was an existing/already open tab. A new page load of a timed page would suffer the same issue, but only the very first time through. The second `inFocus = false` patch fixed that.
Not expecting much of this to make sense to anyone, but figured an explanation on my findings was due. And I'll sleep a little better tonight. :-)
-Keith