Skip to content

Instantly share code, notes, and snippets.

@luizcarraro
Created July 27, 2017 11:37
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save luizcarraro/2d04d83e66e3f03bef9b2e714ea8c0d7 to your computer and use it in GitHub Desktop.
Save luizcarraro/2d04d83e66e3f03bef9b2e714ea8c0d7 to your computer and use it in GitHub Desktop.
ELECTRON: Open link in external default OS browser
@agrublev
Copy link

Youre the man!

@reznik789
Copy link

Nice solution, thank you very much!)

@zrbecker
Copy link

zrbecker commented Dec 15, 2018

Thanks for the solution. I came up with the following derivative since I wasn't using jQuery.

const isDescendant = (parent, child) => {
    let node = child.parentNode;
    while (node != null) {
        if (node === parent) {
            return true;
        }
        node = node.parentNode;
    }
    return false;
}

const configureOpenRenderedLinksInDefaultBrowser = () => {
    document.querySelector('body').addEventListener('click', event => {
        if (event.target.tagName.toLowerCase() === 'a' && isDescendant(htmlView, event.target)) {
            event.preventDefault();
            shell.openExternal(event.target.href);
        }
    });
}
configureOpenRenderedLinksInDefaultBrowser();

@byawitz
Copy link

byawitz commented Jan 22, 2019

thank you very much, very helpful 👍

@genbliz
Copy link

genbliz commented Jan 25, 2019

Another simple solution without using jQuery.


function configureOpenRenderedLinksInDefaultBrowser() {
  const aAll = document.querySelectorAll("a.link-view");
  if (aAll && aAll.length) {
    aAll.forEach(function(a) {
      a.addEventListener("click", function(event) {
        if (event.target) {
          event.preventDefault();
          let link = event.target.href;
          require("electron").shell.openExternal(link);
        }
      });
    });
  }
}

setTimeout(function() {
  configureOpenRenderedLinksInDefaultBrowser();
}, 100);

@IzzleNizzle
Copy link

IzzleNizzle commented Mar 29, 2019

I was using this in a React App and had to play around with it a bit. Thought I'd share for anyone else since this is a cool feature.

const { shell } = window.require('electron');  // Import at top of page..

class Example extends Component {

/* State and other code stuffs */

  myClickFunction = () => {
    shell.openExternal(fancyLink);   // Here I utilize the feature. 
  }

}

electron/electron#9920 (comment)

@Oxey405
Copy link

Oxey405 commented Oct 11, 2020

Thank you !

@drewjosh
Copy link

@IzzleNizzle Thanks so much!

@kowasaur
Copy link

Here's another one without jQuery

const { shell } = require("electron")

document.body.addEventListener('click', event => {
  if (event.target.tagName.toLowerCase() === 'a' && event.target.protocol != 'file:') {
    event.preventDefault();
    shell.openExternal(event.target.href);
  }
});

@artfisica
Copy link

Hi!
where should I put this in my app? I mean, the file?

@xhyabunny
Copy link

same question, where?

@ali90taz
Copy link

Helps me to understand, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment