gpt4 book ai didi

javascript - chrome.serial/chrome.usb 未定义

转载 作者:行者123 更新时间:2023-11-28 18:33:33 25 4
gpt4 key购买 nike

我正在尝试通过 Chrome 插件/扩展访问串行/USB 端口。我正在关注 chrome usb 的文档&& chrome serial ,当我启动我的插件时它会抛出

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

我还尝试了 google chrome sample 中的示例代码但遇到了同样的问题,基本上 chrome.usb/chrome.serial 是未定义的。

任何能让我正确方向的建议都是值得赞赏的。谢谢 !

我附上我的示例代码

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);

list

{
"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"
]
}

最佳答案

只有 Chrome 应用程序(而非 Chrome 扩展程序)才能访问硬件。检查创建 Chrome 应用程序的步骤:https://developer.chrome.com/apps/first_app

关于javascript - chrome.serial/chrome.usb 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37528689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com