1

Getting ERROR: Uncaught TypeError: Cannot read property 'queryState' of undefined

how can i run the extension (https://developer.chrome.com/apps/idle ) from my code?

manifest.json:

{
  "name" : "Idle - Simple Example",
  "version" : "1.0.1",
  "description" : "Demonstrates the Idle API",
  "background" : {
    "scripts": ["background.js"]
  },
  "permissions" : [ "idle" ],
  "browser_action" : {
    "default_icon" : "sample-19.png"
  },
  "icons" : {
    "16" : "sample-16.png",
    "48" : "sample-48.png",
    "128" : "sample-128.png"
  },
  "manifest_version": 2
}

background.js:

var history_log = [];
chrome.idle.onStateChanged.addListener(function(newstate) {
  var time = new Date();
  if (history_log.length >= 20) {
    history_log.pop();
  }
  history_log.unshift({'state':newstate, 'time':time});
});
chrome.browserAction.onClicked.addListener(function() {
  window.open('history.html', 'testwindow', 'width=700,height=600');
});

FAIL: in my code to get the chrome.idle.queryState, http://localhost/index.html:

<html>
<script>
document.addEventListener('DOMContentLoaded', function() {
  chrome.idle.queryState(5, function(state) {
    var time = new Date();
    alert("extension: " + state);
  });

});
</script>
</html>

EDIT:

it only works when i use as chrome-extension:// but does not work if i try to use it from http:// or https://. My goal is to make it work from http:// or https:// (or iFrame opening chrome-extension invisibly if possible?)

enter image description here

  • i want to know on which page you are executing the DomContentLoaded event. is that page part of extension. – yogesh kumar Feb 03 '16 at 09:41
  • I am executing the DomContentLoaded event in index.html which is regular page part of site. –  Feb 03 '16 at 09:42

1 Answers1

2

Having an extension open a website does not magically grant the site rights to use Chrome Extension API.

  1. Simplest is to have index.html as part of the extension's files. Then you can use the API, but take note of the Chrome's CSP (you can't use inline scripts).

  2. If your goal is specifically to provide a web page access to the Chrome API, the webpage will need to talk to the extension.

    There are many methods for that, for example "externally_connectable", or talking via a context script and shared DOM.

In any case: chrome.idle can only be called from extension's own pages, including but not limited to the background page.

Also, please use chrome.windows.create or chrome.tabs.create instead of window.open in an extension. It doesn't require special permissions.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • I need to launch it as `https://example.com/phpetcetc` in that page i need to track the status. for that to make work what can i do please? –  Feb 03 '16 at 09:55
  • Its a KIOSK application, i must have to run it as http:// or https:// and from there i need to get the activity or idle state. is it impossible? or there is anyother way using iframe to the local extension url as chrome-extension:// ? –  Feb 03 '16 at 09:56
  • 1
    Wait, a kiosk application? Why are you trying to make it an extension then, and not an app? – Xan Feb 03 '16 at 09:58
  • Because in kiosk application i am using webRTC –  Feb 03 '16 at 10:00
  • So i must need WebRTC + idle state on the full-screen kiosk touch screen. –  Feb 03 '16 at 10:00
  • That's not an answer. WebRTC is fine in chrome apps. And if the only thing you need is idle state, why not use [pure web approaches](http://stackoverflow.com/questions/9564602/how-to-know-browser-idle-time)? What's the benefit of `chrome.idle` / what's your scenario for it? – Xan Feb 03 '16 at 10:01
  • 1) Google chrome opens a site in my kiosk/full-screen as https://apprtc.appspot.com/custom which does the WebRTC part 2) beside that there is a touch button browse website, when they click on it, it has to open external sites such as `https://facebook.com https://youtube.com`, now when iframe is opening other origin url, then we do not get activity/idle states. Therefore the chrome.idle was perfect in my case. –  Feb 03 '16 at 10:04
  • Then you should do idle detection in the background, and then message that to your web app. There are many ways, I linked to some. – Xan Feb 03 '16 at 10:05