Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access USB devices from Chrome Extension OR Communicate with Chrome App from Web Page

I know that we can access chrome.hid.getDevices() in chrome app. but I have also heard that chrome.hid.getDevices() can only be used in chrome app.

But can we somehow access chrome.hid.getDevices() in chrome extensions. Because I need to inject scripts using Content-script so that I can use my extension functions from Web page and according to my knowledge Content-script can only be used in Chrome Extension.

Is there any example to communicate with Chrome app with web pages?

like image 546
Waqar Ahmed Avatar asked Jul 21 '16 11:07

Waqar Ahmed


2 Answers

What about maintaining both extension and apps, since apps have access to USB devices while extensions can inject scripts to web pages, you could use Message Passing, such as chrome.runtime.sendMessage and chrome.runtime.connect to establish connection between extension and apps.

The first parameter for above two methods is extensionId, which could be set external extension/app id.

like image 70
Haibara Ai Avatar answered Sep 30 '22 13:09

Haibara Ai


If you intend to directly call your functions from website code, you don't need an extension/content script as a proxy.

A specific domain can be whitelisted in "externally_connectable" manifest key to be able to initiate messaging with your app.

After that:

  • The webpage can call chrome.runtime.sendMessage() or .connect() with app's ID.
  • App will receive a chrome.runtime.onMessageExternal or onConnnectExternal event.

If listeners to those events are registered in the event page, it will be woken up to handle them even if the app is not launched.

like image 23
Xan Avatar answered Sep 30 '22 11:09

Xan