7

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?

Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35

3 Answers3

1

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.

Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
1

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.

Xan
  • 74,770
  • 16
  • 179
  • 206
1

The best thing to do would be create a C# application (Or whatever else that supports access to native messaging with chrome extensions) like shown in this google example

This allows you to send a message from chrome to your application and retrieve results (like JSON data or whatever else that ca be passed).

What I did when in a similar situation to you in needing to query a local database was create a NodeJS application that you could query like localhost:4949/usb?slot=1 from the extention and the NodeJS application itself accesses the USB or whatever else that is otherwise limited, gets whatever data is needed and returns it as a result using a REST API.

I didnt like the idea of Native Messaging between extention and application so the NodeJS server was a nice solution. Works over the local network with an IPv4 IP aswell like 192.168.0.16:4949

I can give a better example of how to do this if needed

Cacoon
  • 2,467
  • 6
  • 28
  • 61
  • Hmm.. It'll be really disappointing if this proves to be the best answer. I'd hoped someone would have found a way to make something portable, like WebUSB, `chrome.hid`, or `chrome.usb` work in a chrome extension context. – James_pic Jun 30 '18 at 19:04
  • Very unlikely, youd have to post an issue on chronium as chrome is based on the open source project chronium. Setting up a NodeJS backend is actually very easy and in my opinion the easiest way to do this – Cacoon Jul 01 '18 at 03:16
  • I did: https://bugs.chromium.org/p/chromium/issues/detail?id=823736. They decided WONTFIX. The context of this is adding Ledger support to MetaMask, and the option you propose is one we'd already considered and decided was not appropriate (https://github.com/MetaMask/metamask-extension/issues/4204#issuecomment-394193271). I added a bounty to this question hoping someone knew of a better way (there are already a couple of ways you can use USB from web pages, and it's insane that none of them work in extensions), but it looks like there may not be. – James_pic Jul 01 '18 at 17:30
  • 1
    Yeah the support for these useful things is really lacking IMO. And support seems be continually being withdrawn – Cacoon Jul 03 '18 at 19:19
  • 1
    This project uses Chrome Extensions + Native Messaging: https://github.com/gnaudio/jabra-browser-integration – Morten Frederiksen Mar 07 '19 at 08:35