romanschejbal / electron-blog

Me learning Electron http://red-badger.com/blog/2016/04/18/building-desktop-apps-with-electron-webpack-and-redux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Links in embedded twitter widget open strange external browser window

jforaker opened this issue · comments

Hello, this is not so much an issue with the repo as it is a question, I hope you might be able to lend some insight...

I have a similar implementation to the hackernews feed, but i show a list of tweets using the twitter sdk. When I click on a link within a tweet, the menubar app closes and it opens a strange browser window with no "chrome" (no close/minimize buttons). And the only way to close it is to quit the application.

Any thoughts? I was going to post this in the menubar repo but it looks like the author is unresponsive to all activity there.

image

Are you using the electron shell to open the external link? As I'm doing here

For regular links, yes. But in this case, the links are part of the widget added by the twitter js sdk.

image

Which are ultimately rendered like this:
image

I see. Well, you could find all the anchor elements once the content's loaded and attach your own onclick handler on them. The custom handler would prevent the default behaviour and open the link via electron's shell. Let me know if you need more help.

Good idea. A bit of a challenge though because they are iframes 😱 (or some form of iframe) . I'll keep you posted. Thanks

Wowzer I fixed it!
Did some smart googling and found this which led to this glorious thread.

So, I adjusted it and added that bit here:

mb.on('after-create-window', function () {
  // https://github.com/marcbachmann/electron-rpc
  server.configure(mb.window.webContents);
  // https://github.com/romanschejbal/electron-blog/issues/2
  // and https://github.com/electron/electron/issues/1344#issuecomment-208839713
  const handleRedirect = (e, url) => {
    if (url !== mb.window.webContents.getURL()) {
      e.preventDefault();
      shell.openExternal(url)
    }
  };
  mb.window.webContents.on('will-navigate', handleRedirect);
  mb.window.webContents.on('new-window', handleRedirect);
});

And 🎉 clicking any anchor tag on an embedded tweet opens in my default browser and focuses itself on that tab. Perfecto.

Closing.

Thanks again

Great job! Thanks for sharing 👍