Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registering custom protocol at installation process in electron app

Hi I am new to electron and was wondering how i can register a custom protocol for the app at the time of installation process of the app.

I am using electron-builder for building the app. Here is the build build code

"build": {
"appId": "com.test.testapp",
"productName": "testapp",
"asar": true,
"protocols": [{
  "name": "testapp",
  "schemes": [ "testapp" ]
}],
"nsis": {
  "oneClick": false,
  "perMachine": true,
  "allowToChangeInstallationDirectory": true,
  "runAfterFinish": false,
  "createDesktopShortcut": true
},
"squirrelWindows": {
  "msi": true
},
"directories": {
  "output": "distribution"
}

I know that by adding the below line registers the custom protocol

 app.setAsDefaultProtocolClient("testapp");

but it only does if i run the app at least the first time.

Which i don't want there is no guarantee that the user will launch the app after installation.

So is there a way that i can register the custom protocol in the installation process using electron-builder

like image 460
Jyotirmoy Avatar asked Aug 22 '17 04:08

Jyotirmoy


2 Answers

I'm still new to Electron and electron-builder but solved this problem for NSIS-target already. First of all I should note that app.setAsDefaultProtocolClient used to handle custom protocols inside the application as far as I do understand. You need to register this custom protocol using electron-builder itself.

Secondly I need to choose between NSIS and squirrelWindows. NSIS is the preferable as long I understand because Squirrel is less supported and has some problems. Again I'm not an expert but I read something about it. So squirrelWindows section is redundant. You don't specify win.target and it is "nsis" by default.

There is a problem with the custom protocol registration for NSIS target. You can read more here: Protocol (scheme) for windows but there is a workaround. You need to create a file named installer.nsh in your build folder with such a content:

!macro customInstall DetailPrint "Register evehq-ng URI Handler" DeleteRegKey HKCR "evehq-ng" WriteRegStr HKCR "evehq-ng" "" "URL:evehq-ng" WriteRegStr HKCR "evehq-ng" "EveHQ NG SSO authentication Protocol" "" WriteRegStr HKCR "evehq-ng\DefaultIcon" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" WriteRegStr HKCR "evehq-ng\shell" "" "" WriteRegStr HKCR "evehq-ng\shell\Open" "" "" WriteRegStr HKCR "evehq-ng\shell\Open\command" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME} %1" !macroend

Replace evehq-ng with your protocol string and EveHQ NG SSO authentication Protocol with your protocol description.

After that you have to set nsis.perMachine to true.

I haven't yet solved this problem for Linux but work in this direction. You can see my code in my proof of concepts project here: EveHQ-NG proof of concepts application.

If you will solve this problem for Linux and MacOS write me a message somehow here or on GitHub.

Hope it helps.

like image 117
Mike Eshva Avatar answered Sep 30 '22 23:09

Mike Eshva


Since you are using electron-builder you can do the following:

"build": {
    ... other top level electron-builder keys ...
    "protocols": [
       {
           "name": "Custom Protocol Name",
           "schemes": [
               "customProtocol"
           ]
       }
    ]
}

In case you wanted to handle customProtocol://some-link with your application.

More details: https://www.electron.build/configuration/configuration

(Search for protocols; as of now, the documentaion is a little mis-formatted. It's supposed to be the same top-level key as the fileAssociations key above it.)

like image 27
Akshay Anurag Avatar answered Sep 30 '22 23:09

Akshay Anurag