Skip to content

Instantly share code, notes, and snippets.

@freaktechnik
Last active April 27, 2016 16:00
Show Gist options
  • Select an option

  • Save freaktechnik/dfccb3094907373c21be94d02045d8c1 to your computer and use it in GitHub Desktop.

Select an option

Save freaktechnik/dfccb3094907373c21be94d02045d8c1 to your computer and use it in GitHub Desktop.
Running native commands in WebExt
chrome.browserAction.onClick.addListener(() => {
var port = chrome.runtime.invokeNativeCommand("livestreamer", ["https://twitch.tv/pax"]);
port.onDisconnect.addListener(() => {
// TODO could we get the exit code here?
console.log("Livestreamer quit");
});
});

chrome.runtime.invokeNativeCommand(command, arguments)

Method to run a native command without having to write a native host. Requires a permission nativeCommands.

Arguments

  • {string} command Command to execute. Has to be a command binary in the system path and not a path to a binary.
  • {Array.<string>} [arguments] Optionally pass some arguments to the command

Returns

runtime.Port though modified to be plain stdio, without message sizes.

Todo

  • Some kind of error if the command doesn't exist, maybe an onNotFound event sink on the port.
  • Figure out what kind of encoding the stdio communication should have. I'd figure using system encoding or utf-8 should be fine, use connectNative for binary operations.
  • Forbid the use of system commands (so all unix and dos commands, as well as ps)
{
"name": "Open Livestreamer",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": [ "example.js" ]
},
"browser_action": {
"default_icon": {
"32": "icon.png"
}
},
"icons": {
"32": "icon.png"
},
"permissions": [ "nativeCommands" ],
"applications": {
"gecko": {
"id": "native-command@example.com",
"strict_min_version": "90.0"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment