javascript - Electron app.on("open-url") alternative for custom protocol in Windows - Stack Overflow

I'm developing an app in Electron and I need to handle a custom protocol inside this app.I'm

I'm developing an app in Electron and I need to handle a custom protocol inside this app.
I'm using app.setAsDefaultProtocolClient(PROTOCOL) for that.

I am using "open-url" for macOS to handle the URL with my custom protocol and it is working smoothly but I can't figure it out on Windows. I'm sending some data in the URL so just opening the window won't work.

I checked this answer, but this was answered in 2016 and the method makeSingleInstance is now deprecated. In the docs, it suggests to use requestSingleInstanceLock but it doesn't take any callbacks or return URL.

So how can I enable the same feature in both macOS and Windows?


Code

index.js

app.on('ready', () => createWindow(`file://${__dirname}/views/wele.html`));

app.on('activate', () => {
  // eslint-disable-next-line no-shadow,global-require
  const { mainWindow } = require('./utils/createWindow');
  // On OS X it's mon to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow(`file://${__dirname}/views/wele.html`);
  }
});

app.on('open-url', handleOpenURL);

app.setAsDefaultProtocolClient(PROTOCOL);

handleOpenURL.js

module.exports = (e, data) => {
  e.preventDefault();
  // Some other Logic
  createWindow(URL);
}

I'm developing an app in Electron and I need to handle a custom protocol inside this app.
I'm using app.setAsDefaultProtocolClient(PROTOCOL) for that.

I am using "open-url" for macOS to handle the URL with my custom protocol and it is working smoothly but I can't figure it out on Windows. I'm sending some data in the URL so just opening the window won't work.

I checked this answer, but this was answered in 2016 and the method makeSingleInstance is now deprecated. In the docs, it suggests to use requestSingleInstanceLock but it doesn't take any callbacks or return URL.

So how can I enable the same feature in both macOS and Windows?


Code

index.js

app.on('ready', () => createWindow(`file://${__dirname}/views/wele.html`));

app.on('activate', () => {
  // eslint-disable-next-line no-shadow,global-require
  const { mainWindow } = require('./utils/createWindow');
  // On OS X it's mon to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow(`file://${__dirname}/views/wele.html`);
  }
});

app.on('open-url', handleOpenURL);

app.setAsDefaultProtocolClient(PROTOCOL);

handleOpenURL.js

module.exports = (e, data) => {
  e.preventDefault();
  // Some other Logic
  createWindow(URL);
}
Share Improve this question edited Oct 30, 2019 at 10:29 Apal Shah asked Oct 30, 2019 at 10:09 Apal ShahApal Shah 7008 silver badges17 bronze badges 6
  • 2 I think there is no tutorial available for windows to handle the same situation. You can register the protocol. and look for the process arguements to handle this. using process.argv . – Sharvin K Commented Oct 30, 2019 at 10:42
  • 2 Also you can try this event for the same, github./electron/electron/blob/master/docs/api/… – Sharvin K Commented Oct 30, 2019 at 10:46
  • I tried this example: electronjs/docs/all#apprequestsingleinstancelock. I tried printing event, mandLine, workingDirectory, but this event is never getting executed. – Apal Shah Commented Oct 30, 2019 at 11:08
  • ok then you can try using the process.argv to get the passed argements – Sharvin K Commented Oct 30, 2019 at 11:10
  • Not able to figure out which event can handle a newly opened window where I can use process.argv – Apal Shah Commented Oct 30, 2019 at 11:20
 |  Show 1 more ment

1 Answer 1

Reset to default 8

Take a look at this example it is built with angular and electron.

You just need to make sure of the following for the custom uri to work on windows:

First, only one instant is running, by checking app.requestSingleInstanceLock() if it is true then you need to quit the app app.quit(). because we only need one instance to run. Second, you should handle the second-instance event app.on('second-instance', (event, args) => {})

const customSchemeName = 'x-pany-app';
const primaryInstance = app.requestSingleInstanceLock();
if (!primaryInstance) {
        app.quit();
        return;
}

// The primary instance of the application will run this code, not the new  instance
app.on('second-instance', (event, args) => {
    // handle custom uri
}

...
// Register private URI scheme for the current user when running for the first time
app.setAsDefaultProtocolClient(customSchemeName);

// Handle custom uri requests against the running app on Mac OS
app.on('open-url', (event, customSchemeData) => {
    event.preventDefault();
    // handle the data
});
...

I had the same issue on windows, this fixed it for me, I have tested it and it works. credits goes to gary archer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742285396a4415230.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信