chrome.serial / chrome.usb undefined

I'm trying to access serial/usb ports via chrome plugins/extensions. Im following the documents from chrome usb && chrome serial , when i started my plugin it throws

"Uncaught TypeError: Cannot read property 'getDevices' of undefined"

i also tried the sample codes from google chrome sample but experienced the same problem, basically chrome.usb / chrome.serial is undefined.

Any suggestions to put me right direction is appreciated. Thanks !

Im attaching my sample codes

popup.html

<!DOCTYPE html>
<html>
  <body style="width: 300px">
    Open <a href="http://stackoverflow.com" target="_blank">this page</a> and then 
    <button id="clickme">click me</button>
    <script type="text/javascript" src="popup.js"></script>
  </body>
</html>

popup.js

function readPorts() {
    console.log("Reading ports . . . . . ")
     chrome.usb.getDevices(function(devices) {
        console.warn('chrome.usb.getDevices error: ' +
                 chrome.runtime.lastError.message);
        for (var i = 0; i < devices.length; i++) {
           console.log('Device : ' + devices[i].path + '"===' + devices[i].path + '=====');
        }
    });
}

document.getElementById('clickme').addEventListener('click', readPorts);

manifest

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "This extension will read the com ports from your machine",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_title": "Read com ports!"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*","https://*/*"],
      "js": ["myscript.js"]
    }
  ],
  "permissions": [
    "serial",
    "usb"
  ]
}

Only Chrome Apps (not Chrome Extensions) have access to the hardware. Check the steps for creating a Chrome App: https://developer.chrome.com/apps/first_app


Both of those APIs are limited to Chrome Apps. Your manifest specifies an extension.

Chrome Extensions have an entirely different set of APIs. They overlap, but one is not a superset of another.

If you want a content script (read: interact with the browser itself), it must be an extension. If you want USB/Serial API, it must be an App.

You must either rethink how to interact with the browser (Apps have some ways to expose themselves to pages) or make both and let them talk to each other.

链接地址: http://www.djcxy.com/p/63724.html

上一篇: WPF到powershell erros

下一篇: chrome.serial / chrome.usb未定义