0

クロムプラグイン/拡張機能経由でシリアル/ USBポートにアクセスしようとしています。私はそれは私も基本的にchrome.usb/chrome.serial未定義である、google chrome sampleからサンプルコードを試してみましたが、同じ問題を経験したchrome.serial/chrome.usb undefined

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

スロー私のプラグインを開始し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); 

マニフェスト

を取り付ける

イム

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

答えて

2

Chromeアプリケーション(Chrome拡張機能ではない)のみがハードウェアにアクセスできます。 Chromeアプリケーションを作成する手順を確認してください:https://developer.chrome.com/apps/first_app

1

これらのAPIはどちらもlimited to Chrome Appsです。マニフェストで拡張機能が指定されています。

Chrome拡張機能は、完全にdifferent set of APIsです。それらは重複しますが、一方は別のスーパーセットではありません。

コンテンツスクリプト(読み込み:ブラウザ自体と対話する)が必要な場合は、それを拡張子にする必要があります。 USB /シリアルAPIが必要な場合は、アプリケーションでなければなりません。

ブラウザと対話する方法を再考するか(アプリにはページに自分自身を公開する方法がいくつかあります)、またはその両方を行い、talk to each otherとする必要があります。

+0

上記のガイドラインは参考になり、私はbtwnアプリケーションと拡張機能の違いを理解しています。 – optimus