From 9be3323859e38d367a7b77925d3bcc8e8afbcd8a Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Mon, 21 Jan 2019 12:56:29 +0500 Subject: [PATCH] Own handler for links. All external links (not belonging to mail.yandex.ru domain) should open in external browser now. Related to #3. --- main.js | 1 - renderer.js | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index e92e49d..dae95ed 100644 --- a/main.js +++ b/main.js @@ -36,7 +36,6 @@ function createWindow() { //mainWindow.webContents.openDevTools(); mainWindow.loadURL('https://mail.yandex.ru/'); - //mainWindow.loadFile(path.join(__dirname, "index.html")); mainWindow.on('closed', function () { mainWindow = null; }) diff --git a/renderer.js b/renderer.js index 59cbc6d..8e151b1 100644 --- a/renderer.js +++ b/renderer.js @@ -1,8 +1,12 @@ -console.log(window); -var ipc = require("electron").ipcRenderer; +// This file is loaded by Chromium engine and executes in browser. +console.log(window); +const ipc = require('electron').ipcRenderer; +const { shell } = require('electron') + +// Unread mails check. function checkForUnreads() { - unread = parseInt(document.title.split(" ")[0]); + unread = parseInt(document.title.split(' ')[0]); if (typeof (unread) != NaN && unread > 0) { ipc.send('has-unread', unread); } else { @@ -10,4 +14,35 @@ function checkForUnreads() { }; } -setInterval(checkForUnreads, 1000); \ No newline at end of file +setInterval(checkForUnreads, 1000); + +// Handle all clicks on links. +function clickHandler(event) { + console.log(event); + + // We should check not only specified element, but also all other + // parent elements until we found a.daria-goto-anchor. + var linkFound = false; + var link = ""; + if (!event.target.hasOwnProperty('href')) { + for (var i = 0; i < event.path.length; i++) { + if (event.path[i].tagName == 'A' && (event.path[i].className.indexOf('daria-goto-anchor') !== -1 || event.path[i].className.indexOf('mail-ui-IconList-Item') !== -1 && event.path[i].hostname != "mail.yandex.ru")) { + linkFound = true; + link = event.path[i].href; + break; + } + } + } else { + linkFound = true; + link = event.target.href; + } + + if (linkFound) { + event.preventDefault(); + shell.openExternal(link); + } else { + console.log("No external link found, doing nothing"); + } +} + +document.addEventListener('click', clickHandler); \ No newline at end of file