0

I am trying to use the sample chrome extension provided here to get an understanding of how the chrome idle API works: https://developer.chrome.com/extensions/samples#search:idle

I have also looked at this similar question: Google chrome - extension chrome idle not working but it didn't seem to help either. However, I am running into difficulties actually getting it to work. Without changing anything, I loaded the extension files into developer mode on Chrome. Upon clicking the extension icon, the popup appears which is meant to query the state every second. However, the state is not being updated if even if I click inside the popup window. Upon inspection of the console, I found the following error:

Uncaught TypeError: Cannot set property 'innerHTML' of null at renderHistory (history.js:66) at HTMLDocument. (history.js:82)

Is anyone else able to run this sample extension? I would appreciate some help in trying to figure this out.

bawse
  • 201
  • 3
  • 13

1 Answers1

1

It's just a programming error: a race condition in the sample code.

var dom_history = document.querySelector('#idle-history');

This assignment happens outside of DOMContentLoaded, and the script is loaded in the <head> - so it actually doesn't find the element at runtime even if it exists later.

It's possible that before timings worked differently and the extension was working correctly.

To fix locally, declare the variable globally but assign inside the DOMContentLoaded listener (not saying it's the best possible solution, just minimal changes):

var dom_history;
document.addEventListener('DOMContentLoaded', function() {
  dom_history = document.querySelector('#idle-history');
  /* ... */
});

To fix it for everyone.. You could always try and submit a patch for this code in Chromium repository. It's an involved process, though.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Thanks for that, your suggestion gets rid of the error. However, the extension is still not working as expected. Although there is no error, the current state does not get updated when I click inside the extension popup. The `Last state change` counter just keeps incrementing and doesn't reset when i click. – bawse Oct 04 '17 at 20:02