0

SCENARIO


I'm on Windows 10 x64 with Firefox v51.0, and I'm completely newbie on the Mozilla's extension development world (and also with JavaScript lang.)

For writing my very first "Hello world" extension I followed the Borderify example on Mozilla's website, which worked fine on my side.

PROBLEM


Now I'm trying to modify the original sample to write a simple script that must run a external application when the user is on a mozilla.org webpage. To accomplish this task I'm trying to use the nsiProcess interface. The problem is that the application doesn't execute.

manifest.json

{

  "manifest_version": 2,
  "name": "Test",
  "version": "1.0",

  "description": "Runs a external application.",

  "icons": {
    "48": "icons/border-48.png"
  },

  "content_scripts": [
    {
      "matches": ["*://*.mozilla.org/*"],
      "js": ["test.js"]
    }
  ]

}

test.js

// create an nsIFile for the executable
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsIFile);
file.initWithPath("c:\\myapp.exe");

// create an nsIProcess
var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(file);

// Run the process.
process.run(false);

myapp.exe (which yes, it is located in C:\ root) is an application that just has an empty/default form, it is developed in .NET platform (C#), targetting .NET Framework 4.5 on WinForms technology. I also tried to run other applications like Notepad (specifying the full filepath) with no results. I also tried to change the "://.mozilla.org/*" pattern to match other websites.

QUESTION


I'm doing something wrong?, how can I run that executable?.

Additionally and optionally, I will apreciate so much if someone could explain me how could I debug what is happening in my code (to discover errors/exceptions) after the script is loaded in Firefox, because I'm totally blind in that manner, I just can say the script "does nothing" (because it does not run the external execuaable) but I have no control over the code at all because I'm missing where to find the debugging instrumental.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Related/partial duplicate of: [Unable to use Components in WebExtensions: get "ReferenceError: Cu is not defined"](https://stackoverflow.com/a/44608767) – Makyen Jul 27 '17 at 06:33
  • Another partial duplicate: [Google Chrome / Firefox do not see extension output in console](https://stackoverflow.com/q/38913799) – Makyen Jul 27 '17 at 06:38
  • Please keep your Questions to one question per Question. Questions which contain multiple questions which are not *very* tightly related are considered too broad and tend to be closed. The reason for this is that the Stack Overflow format is intended to provide a base of questions and answers which are useful to people in the future, not just the person currently asking. Questions with multiple issues tend to be too specific to be useful to others searching for help to their problems. Often, to solve a larger issue it is necessary to combine the answers from multiple questions. – Makyen Jul 27 '17 at 06:45
  • In addition, having one Question per question allows us to use questions and answers as targets to close other questions as duplicates of them. While a decent answer explaining installation or use of native messaging could be written, the additional issues in your question make using as a duplicate-target less desirable. – Makyen Jul 27 '17 at 06:51
  • The partial duplicate links are directly relevant to your question. They explicitly answer two of the three issues you have in your question. – Makyen Jul 27 '17 at 11:38
  • Possible duplicate of: [How to execute a command line program in Firefox Webextensions?](https://stackoverflow.com/q/37769533) – Makyen Jul 27 '17 at 12:25
  • Related: [Add a firefox webextension when installing the native host application](https://stackoverflow.com/q/44760861), [FireFox Addon WebExtension API - open local file / appliction](https://stackoverflow.com/q/41680643), [Firefox add-on: (Native app + Content Script + Background script) messaging](https://stackoverflow.com/q/45262174), [Javascript shell command execution from Firefox web extensions](https://stackoverflow.com/q/44183413), [this question](https://stackoverflow.com/q/40041149), [this one](https://stackoverflow.com/q/41541943), and [this](https://stackoverflow.com/q/42384875). – Makyen Jul 27 '17 at 12:34

3 Answers3

7

WebExtensions do not have access to XPCOM (or more specifically, to Components which your code references). The WebExtensions replacement for nsIProcess is native messaging: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging

Documentation on how to debug WebExtensions is here: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Debugging

Andrew Swan
  • 1,268
  • 6
  • 7
  • Thankyou for answer. It seems very complicated, firstly I only would need to run the graphical application, I don't pretend to read the stdIn/stdOut buffer,and secondly I don't have any clear which must be the filename of the "App manifest" that explains in the article you linked, there is any information in all the article about that neither how to do the installation of that "App manifest" from the extension itself.Almost all the steps are unclear for me,please,¿could you provide a simple premade example/extension that just connect (run) a external graphical application on Windows?.Thankyou. – ElektroStudios Jul 26 '17 at 04:02
  • 1
    Distribution and installation of native applications is deliberately separated from extensions so you'll need to arrange to install the app manifest in a separate (native) installer. I'm sorry I don't know much about Windows, perhaps another question in a different category... – Andrew Swan Jul 27 '17 at 04:15
  • 1
    [This](https://github.com/mdn/webextensions-examples/tree/master/native-messaging) is a Mozilla official example for Native Messaging. – zhm Aug 01 '17 at 01:41
  • As Andrew Swan told, ElektroStudios, you need install it, the old legacy Mozilla extensions had security issues, imagine if with only an extension you can run anything, also it's possible to run malware... Even before to migrate to WebExtensions Mozilla was disabling some features because the security issues, and fixing others. Also the behaviour that you wanted is PUA for many anti-malwares. –  Aug 02 '17 at 22:52
  • The feature is supported in the way explained in the answer, I only told the reasons why that way to support it and not the old way, because the way you think is the correct is a big security hole. –  Aug 06 '17 at 01:28
  • I analyze software and to analyze and tell is malware is more easy than analyze and tell is not malware. In last months, the wannacry case is in the news, it's a example how easy can be a high risk things that looks easy. –  Aug 06 '17 at 01:28
  • Consent is not enough to allow a big risk, and users often not to read, not to know about computer like a developer, and I told is difficult to be sure some thing is not malware, for a user is more difficult. If you want to run a external software you can do with native messaging, your user install your software and then running both it can communicate, –  Aug 06 '17 at 01:29
  • but allow to run arbitrary commands from a extension is like open all the computer to internet, it's a very bad idea. If it to be allowed is easy execute powershell and get the control of your computer, only clicking in install extension and thinking is only a extension in your browser... –  Aug 06 '17 at 01:29
  • Then, the correct is that browser limit what you can run from a extension. –  Aug 06 '17 at 01:29
1

It's possible, but only indirectly through Native Messaging.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Examples

You'll need an external program supporting this type of interfacing.

The example in question: https://github.com/mdn/webextensions-examples/tree/master/native-messaging

I'm not quite sure, though, how/if that works in a page's script context.

user1050755
  • 11,218
  • 4
  • 45
  • 56
-1

This is not supported for safety reason. And altough you manage doing it today, it won't work anymore in few months next to a firefox release... if you want to launch a binary, I would suggest create a webservice you communicate with http requests . the webservice will be responsible to launch the application. a minimal webservice can be done with python BaseHttpServer.Furthermore it will work on any browser ..

sancelot
  • 1,905
  • 12
  • 31