From 4e01724c71a5a121f867cdda9a43e1b6ad84b9a7 Mon Sep 17 00:00:00 2001 From: Ramiro Saenz Date: Tue, 26 Jul 2016 20:45:33 -0300 Subject: [PATCH] Fixed links, images and attachments in Skype Fixes #116 --- app/package.json | 4 +++- app/ux/WebView.js | 9 +++++++++ electron/main.js | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +++- 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/app/package.json b/app/package.json index 035ae0eb..3f3653b7 100644 --- a/app/package.json +++ b/app/package.json @@ -28,6 +28,8 @@ "auto-launch": "^2.1.0", "electron-window-state": "^3.0.3", "firebase": "^3.0.5", - "firebase-token-generator": "^2.0.0" + "firebase-token-generator": "^2.0.0", + "tmp": "0.0.28", + "mime": "^1.3.4" } } diff --git a/app/ux/WebView.js b/app/ux/WebView.js index 831aa037..1e90a208 100644 --- a/app/ux/WebView.js +++ b/app/ux/WebView.js @@ -120,6 +120,15 @@ Ext.define('Rambox.ux.WebView',{ // Open links in default browser webview.addEventListener('new-window', function(e) { + // hack to fix multiple browser tabs on Skype link click, re #11 + if ( e.url.match('https:\/\/web.skype.com\/..\/undefined') ) { + e.preventDefault(); + return; + } else if ( e.url.indexOf('imgpsh_fullsize') >= 0 ) { + require('electron').ipcRenderer.send('image:download', e.url, e.target.partition); + e.preventDefault(); + return; + } const protocol = require('url').parse(e.url).protocol; if (protocol === 'http:' || protocol === 'https:' || protocol === 'mailto:') { e.preventDefault(); diff --git a/electron/main.js b/electron/main.js index a9bf9a81..a6dcf90f 100644 --- a/electron/main.js +++ b/electron/main.js @@ -213,6 +213,48 @@ app.on('certificate-error', function(event, webContents, url, error, certificate } }); + +// Code for downloading images as temporal files +// Credit: Ghetto Skype (https://github.com/stanfieldr/ghetto-skype) +const tmp = require('tmp'); +const mime = require('mime'); +var imageCache = {}; +electron.ipcMain.on('image:download', function(event, url, partition) { + let file = imageCache[url]; + if (file) { + if (file.complete) { + electron.shell.openItem(file.path); + } + + // Pending downloads intentionally do not proceed + return; + } + + let tmpWindow = new BrowserWindow({ + show: false + ,webPreferences: { + partition: partition + } + }); + + tmpWindow.webContents.session.once('will-download', (event, downloadItem) => { + imageCache[url] = file = { + path: tmp.tmpNameSync() + '.' + mime.extension(downloadItem.getMimeType()) + ,complete: false + }; + + downloadItem.setSavePath(file.path); + downloadItem.once('done', () => { + tmpWindow.destroy(); + tmpWindow = null; + electron.shell.openItem(file.path); + file.complete = true; + }); + }); + + tmpWindow.webContents.downloadURL(url); +}); + // This method will be called when Electron has finished // initialization and is ready to create browser windows. app.on('ready', createWindow); diff --git a/package.json b/package.json index 354f722e..6f040e0a 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,8 @@ "auto-launch": "^2.1.0", "electron-window-state": "^3.0.3", "firebase": "^3.0.5", - "firebase-token-generator": "^2.0.0" + "firebase-token-generator": "^2.0.0", + "tmp": "0.0.28", + "mime": "^1.3.4" } }