I am currently working on accessing serial and USB ports through Chrome plugins/extensions. I have been following the guidelines provided by Chrome USB && Chrome Serial. However, upon starting my plugin, I encountered an error message:
"Uncaught TypeError: Cannot read property 'getDevices' of undefined"
Even after attempting to use the sample codes from Google Chrome Samples, I faced the same issue where chrome.usb / chrome.serial appeared to be undefined.
Any advice or suggestions to steer me in the right direction would be greatly appreciated. Thank you!
Below are snippets of my sample code:
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"
]
}