Chrome打包应用程序:从命令行(LaunchData)参数中检索Dart中的FileEntry

时间:2015-08-12 20:47:34

标签: file command-line dart google-chrome-app

与此主题的另一篇文章类似,我一直在尝试使用本地XML文件的文件路径从命令行运行我的应用程序,以便我的main.dart可以解析此文件以提取信息与程序操作相关。我对如何正确访问FileEntry中包含的launchData引用 - onLaunched事件的参数感到困惑。

以下是我目前的情况:

的manifest.json

...
"permissions": [
    "storage",
    "fileSystem",
    "*://*/*"
],

"file_handlers" : {
    "any" : {
        "types" : [ "*" ]
    }
},
...

background.js

chrome.app.runtime.onLaunched.addListener(function(launchData) {

  chrome.app.window.create(

  'htmlFile.html',

  {...},

  function(createdWindow) {
    createdWindow.contentWindow.launchData = launchData;
  });
});

此时,我无法从launchData访问main.dart因为尝试

  FileEntry entry =  (chrome.app.window.current().contentWindow.launchData as chrome.LaunchData).items.elementAt(0).entry;

FileEntry导致访问launchData时出错。我真的很困惑我应该如何从我的Dart代码中访问我想要的FileEntry

1 个答案:

答案 0 :(得分:3)

我最终将此作为我的解决方案:

<强>的manifest.json

...
"file_handlers": {
  "any": {
    "extensions": [
      "xml"
    ]
  }
}, 
...

<强> background.js

chrome.app.runtime.onLaunched.addListener(function(launchData) {

  chrome.app.window.create(
    'htmlSource.html',

  {
    id: 'mainWindow',
    state: "fullscreen"
  },

  function(createdWindow) {

    if(launchData.items !== undefined) {
      launchData.items[0].entry.file(

        function(result) {

          var reader = new FileReader();
          var XML;

          reader.onloadend = function(){
            XML = reader.result;
            chrome.storage.local.set({'XML': XML});
          };

          reader.readAsText(result);

        },

        function(){
          console.log("Error reading XML file");
        }

      );
    } else {
      console.log("No file was detected.");
    }
  });
});

检索XML的dart代码很简单:

String text = (await chrome.storage.local.get('XML'))['XML'];