Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open External Links in Default Browser #35

Merged
merged 1 commit into from
Jun 10, 2015
Merged

Open External Links in Default Browser #35

merged 1 commit into from
Jun 10, 2015

Conversation

jlord
Copy link
Owner

@jlord jlord commented Jun 10, 2015

Since Electron apps are showing webpages that you make they can also show other webpages that you link to in your app. If they're not actually a part of your app, you may prefer them to open up in the user's default browser.

That's what we're gonna do. In various places I link out to other GitHub or Git resources. Those will now open outside of the Git-it app and in the user's browser.

To do this we use the shell module and the method it has, openExternal, which takes in a url. You can read more about it on the documentation page for shell in Electron.

So in the template for each page, layout.hbs (and in the index.html) we require the script that we'll use to handle these external links:

<script>require('../handle-external-links.js')</script>

Note: I do this a couple of different ways in the app right now and will clean it up later 😁

Then in that script we wait until the page is loaded and loop through each of its links. We see if the link includes a http, meaning it's an external link, not a relative link (e.g. ../.../file.html) to another Git-it file.

With each external link we find we add a click event that uses shell to open the link in the browser.

shell.openExternal('http://github.com/jlord/git-it-electron')

Here's the rest of that code.

Fixes #7

jlord pushed a commit that referenced this pull request Jun 10, 2015
Open External Links in Default Browser
@jlord jlord merged commit 982a8d3 into master Jun 10, 2015
@jlord jlord deleted the external-links branch June 10, 2015 04:00
@jlord
Copy link
Owner Author

jlord commented Jun 10, 2015

Whoops, forgot to actually add the commit on index.html here so just pushed it to master: d6aa605

})
}
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var shell = require('shell')

document.addEventListener('DOMContentLoaded', function (event) {
  var links = document.querySelectorAll('a[href]')

  [].forEach.call(links, function (link) {
    var url = link.getAttribute('href')
    if (url.indexOf('http:') > -1) {
      link.addEventListener('click', function (e) {
        e.preventDefault()
        shell.openExternal(url)
      })
    }
  }
})

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went over this with @maxogden IRL — gonna make a new PR with updates ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants