简体   繁体   English

chrome.serial / chrome.usb未定义

[英]chrome.serial / chrome.usb undefined

I'm trying to access serial/usb ports via chrome plugins/extensions. 我正在尝试通过Chrome插件/扩展名访问串行/ USB端口。 Im following the documents from chrome usb && chrome serial , when i started my plugin it throws 我跟随chrome usb && chrome serial的文档,当我启动插件时,它抛出

"Uncaught TypeError: Cannot read property 'getDevices' of undefined" “未捕获的TypeError:无法读取未定义的属性'getDevices'”

i also tried the sample codes from google chrome sample but experienced the same problem, basically chrome.usb / chrome.serial is undefined. 我还尝试了Google chrome示例的示例代码,但遇到了相同的问题,基本上chrome.usb / chrome.serial是未定义的。

Any suggestions to put me right direction is appreciated. 任何建议给我正确的方向表示赞赏。 Thanks ! 谢谢 !

Im attaching my sample codes 我附上我的示例代码

popup.html 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 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. 只有Chrome应用(而非Chrome扩展程序)可以访问硬件。 Check the steps for creating a Chrome App: https://developer.chrome.com/apps/first_app 检查创建Chrome应用的步骤: https//developer.chrome.com/apps/first_app

Both of those APIs are limited to Chrome Apps . 这两个API均限于Chrome Apps Your manifest specifies an extension. 您的清单指定了扩展名。

Chrome Extensions have an entirely different set of APIs . Chrome扩展程序具有一组完全不同的API 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. 如果您想要USB /串行API,它必须是一个应用程序。

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 . 您必须重新考虑如何与浏览器进行交互(应用程序有一些方法可以将自己显示在页面上),或者使两者同时进行并使它们彼此通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM