Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 2722614

Browse files
Fix popups/link opening in general.
Fixes #280. Fixes #255. Fixes #232. Fixes #220. Fixes #121. Co-authored-by: Ramiro Saenz <[email protected]> Reference ramboxapp@c91ef57
1 parent 51970ee commit 2722614

File tree

2 files changed

+127
-144
lines changed

2 files changed

+127
-144
lines changed

app/ux/WebView.js

Lines changed: 11 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,12 @@ Ext.define('Hamsket.ux.WebView',{
147147
let cfg;
148148
enabled = enabled || me.record.get('enabled');
149149

150-
if ( !enabled ) {
151-
cfg = [{
150+
cfg = !enabled ? [{
152151
xtype: 'container'
153152
,html: '<h3>Service Disabled</h3>'
154153
,style: 'text-align:center;'
155154
,padding: 100
156-
}];
157-
} else {
158-
cfg = [{
155+
}] : [{
159156
xtype: 'component'
160157
,hideMode: 'offsets'
161158
,autoRender: true
@@ -167,16 +164,14 @@ Ext.define('Hamsket.ux.WebView',{
167164
,partition: 'persist:' + me.record.get('type') + '_' + me.id.replace('tab_', '')
168165
,allowtransparency: 'on'
169166
,autosize: 'on'
170-
,webpreferences: 'enableRemoteModule=yes,spellcheck=yes,contextIsolation=no' //,nativeWindowOpen=true
167+
,webpreferences: 'nativeWindowOpen=yes,enableRemoteModule=yes,spellcheck=yes,contextIsolation=no' //,nativeWindowOpen=true
168+
,allowpopups: 'on'
171169
//,disablewebsecurity: 'on' // Disabled because some services (Like Google Drive) dont work with this enabled
172170
,userAgent: me.getUserAgent()
173171
,preload: './resources/js/hamsket-service-api.js'
174172
}
175173
}];
176174

177-
if ( Ext.getStore('ServicesList').getById(me.record.get('type')).get('allow_popups') ) cfg[0].autoEl.allowpopups = 'on';
178-
}
179-
180175
return cfg;
181176
}
182177

@@ -262,141 +257,13 @@ Ext.define('Hamsket.ux.WebView',{
262257
});
263258

264259
// Open links in default browser
265-
webview.addEventListener('new-window', function(e) {
266-
switch ( me.type ) {
267-
case 'skype':
268-
// hack to fix multiple browser tabs on Skype link click, re #11
269-
if ( e.url.match(/https:\/\/web.skype.com\/..\/undefined/) ) {
270-
e.preventDefault();
271-
return;
272-
} else if ( e.url.indexOf('imgpsh_fullsize') >= 0 ) {
273-
ipc.send('image:download', e.url, e.target.partition);
274-
e.preventDefault();
275-
return;
276-
}
277-
break;
278-
case 'hangouts':
279-
e.preventDefault();
280-
if ( e.url.indexOf('plus.google.com/u/0/photos/albums') >= 0 ) {
281-
ipc.send('image:popup', e.url, e.target.partition);
282-
return;
283-
} else if ( e.url.indexOf('/el/CONVERSATION/') >= 0 ) {
284-
me.add({
285-
xtype: 'window'
286-
,title: 'Video Call'
287-
,width: '80%'
288-
,height: '80%'
289-
,maximizable: true
290-
,resizable: true
291-
,draggable: true
292-
,collapsible: true
293-
,items: {
294-
xtype: 'component'
295-
,hideMode: 'offsets'
296-
,autoRender: true
297-
,autoShow: true
298-
,autoEl: {
299-
tag: 'webview'
300-
,src: e.url
301-
,style: 'width:100%;height:100%;'
302-
,partition: me.getWebView().partition
303-
,useragent: me.getUserAgent()
304-
}
305-
}
306-
}).show();
307-
return;
308-
}
309-
break;
310-
case 'slack':
311-
if ( e.url.indexOf('slack.com/call/') >= 0 ) {
312-
me.add({
313-
xtype: 'window'
314-
,title: Ext.String.htmlEncode(e.options.title)
315-
,width: e.options.width
316-
,height: e.options.height
317-
,maximizable: true
318-
,resizable: true
319-
,draggable: true
320-
,collapsible: true
321-
,items: {
322-
xtype: 'component'
323-
,hideMode: 'offsets'
324-
,autoRender: true
325-
,autoShow: true
326-
,autoEl: {
327-
tag: 'webview'
328-
,src: e.url
329-
,style: 'width:100%;height:100%;'
330-
,partition: me.getWebView().partition
331-
,useragent: me.getUserAgent()
332-
}
333-
}
334-
}).show();
335-
e.preventDefault();
336-
return;
337-
}
338-
break;
339-
case 'icloud':
340-
if ( e.url.indexOf('index.html#compose') >= 0 ) {
341-
me.add({
342-
xtype: 'window'
343-
,title: 'iCloud - Compose'
344-
,width: 700
345-
,height: 500
346-
,maximizable: true
347-
,resizable: true
348-
,draggable: true
349-
,collapsible: true
350-
,items: {
351-
xtype: 'component'
352-
,itemId: 'webview'
353-
,hideMode: 'offsets'
354-
,autoRender: true
355-
,autoShow: true
356-
,autoEl: {
357-
tag: 'webview'
358-
,src: e.url
359-
,style: 'width:100%;height:100%;'
360-
,partition: me.getWebView().partition
361-
,useragent: me.getUserAgent()
362-
,preload: './resources/js/hamsket-modal-api.js'
363-
}
364-
}
365-
,listeners: {
366-
show(win) {
367-
const webview = win.down('#webview').el.dom;
368-
webview.addEventListener('ipc-message', function(event) {
369-
const channel = event.channel;
370-
switch (channel) {
371-
case 'close':
372-
win.close();
373-
break;
374-
default:
375-
break;
376-
}
377-
});
378-
}
379-
}
380-
}).show();
381-
e.preventDefault();
382-
return;
383-
}
384-
break;
385-
case 'flowdock':
386-
if ( e.disposition === 'new-window' ) {
387-
e.preventDefault();
388-
require('electron').remote.shell.openExternal(e.url);
389-
}
390-
return;
391-
default:
392-
break;
393-
}
394-
395-
const protocol = require('url').parse(e.url).protocol;
396-
if (protocol === 'http:' || protocol === 'https:' || protocol === 'mailto:') {
397-
e.preventDefault();
398-
require('electron').remote.shell.openExternal(e.url);
399-
}
260+
webview.addEventListener('new-window', function (e) {
261+
e.preventDefault();
262+
const protocol = require('url').URL(e.url).protocol;
263+
// Block some Deep links to prevent that open its app (Ex: Slack)
264+
if (['slack:'].includes(protocol)) return;
265+
// Allow Deep links
266+
if (!['http:', 'https:', 'about:'].includes(protocol)) return require('electron').shell.openExternal(e.url);
400267
});
401268

402269
webview.addEventListener('will-navigate', function(e, url) {

electron/main.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ if (config.get('enable_hidpi_support') && (process.platform === 'win32')) {
6161
app.commandLine.appendSwitch('force-device-scale-factor', '1');
6262
}
6363

64+
// TODO: https://github.com/electron/electron/issues/25469
65+
app.commandLine.appendSwitch('disable-features', 'CrossOriginOpenerPolicy');
66+
6467
// This must match the package name in package.json
6568
app.setAppUserModelId('com.thegoddessinari.hamsket');
6669

@@ -357,6 +360,119 @@ if (!haveLock) {
357360
app.quit();
358361
}
359362

363+
const allowPopUp = [
364+
'=?print=true', // esta ultima checkea como anda imprimir un pedf desde gmail, si no va bie sacala
365+
'accounts.google.com/AccountChooser',
366+
'accounts.google.com/o/oauth2',
367+
'api.moo.do',
368+
'app.mixmax.com/_oauth/google',
369+
'app.slack.com/files/import/dropbox',
370+
'app.slack.com/files/import/gdrive',
371+
'app.slack.com/free-willy/',
372+
'auth.missiveapp.com',
373+
'dropbox.com/profile_services/start_auth_flow',
374+
'facebook.com/v3.1/dialog/oauth?',
375+
'facebook.com/v3.2/dialog/oauth?',
376+
'feedly.com/v3/auth/',
377+
'figma.com/start_google_sso',
378+
'hangouts.google.com/webchat/u/0/frame',
379+
'identity.linuxfoundation.org/cas/login',
380+
'mail.google.com/mail',
381+
'manychat.com/fb?popup',
382+
'messenger.com/videocall',
383+
'notion.so/googlepopupredirect',
384+
'officeapps.live.com',
385+
'spikenow.com/s/account',
386+
'zoom.us/office365',
387+
];
388+
389+
app.on('web-contents-created', (webContentsCreatedEvent, contents) => {
390+
if (contents.getType() !== 'webview') return;
391+
// Block some Deep links to prevent that open its app (Ex: Slack)
392+
contents.on(
393+
'will-navigate',
394+
(event, url) => url.slice(0, 8) === 'slack://' && event.preventDefault()
395+
);
396+
// New Window handler
397+
contents.on(
398+
'new-window',
399+
(
400+
event,
401+
url,
402+
frameName,
403+
disposition,
404+
options,
405+
additionalFeatures,
406+
referrer,
407+
postBody
408+
) => {
409+
// If the url is about:blank we allow the window and handle it in 'did-create-window'
410+
if (['about:blank', 'about:blank#blocked'].includes(url)) {
411+
event.preventDefault();
412+
Object.assign(options, {
413+
show: false,
414+
});
415+
const win = new BrowserWindow(options);
416+
win.center();
417+
let once = false;
418+
win.webContents.on('will-navigate', (e, nextURL) => {
419+
if (once) return;
420+
if (['about:blank', 'about:blank#blocked'].includes(nextURL)) return;
421+
once = true;
422+
let allow = false;
423+
for (const url of allowPopUp) {
424+
if (nextURL.includes(url)) {
425+
allow = true;
426+
break;
427+
}
428+
}
429+
// If the url is in aboutBlankOnlyWindow we handle this as a popup window
430+
if (allow) return win.show();
431+
shell.openExternal(nextURL);
432+
win.close();
433+
});
434+
event.newGuest = win;
435+
return;
436+
}
437+
// We check if url is in the allowPopUpLoginURLs or allowForegroundTabURLs in Firebase to open a as a popup,
438+
// if it is not we send this to the app
439+
let allow = false;
440+
for (const allowed of allowPopUp) {
441+
if (url.includes(allowed)) {
442+
allow = true;
443+
break;
444+
}
445+
}
446+
if (allow) return;
447+
shell.openExternal(url);
448+
event.preventDefault();
449+
}
450+
);
451+
contents.on('did-create-window', (win, details) => {
452+
// Here we center the new window.
453+
win.center();
454+
// The following code is for handling the about:blank cases only.
455+
if (!['about:blank', 'about:blank#blocked'].includes(details.url)) return;
456+
let once = false;
457+
win.webContents.on('will-navigate', (e, nextURL) => {
458+
if (once) return;
459+
if (['about:blank', 'about:blank#blocked'].includes(nextURL)) return;
460+
once = true;
461+
let allow = false;
462+
for (const url of allowPopUp) {
463+
if (nextURL.includes(url)) {
464+
allow = true;
465+
break;
466+
}
467+
}
468+
// If the url is in aboutBlankOnlyWindow we handle this as a popup window
469+
if (allow) return win.show();
470+
shell.openExternal(url);
471+
win.close();
472+
});
473+
});
474+
});
475+
360476
// Code for downloading images as temporal files
361477
// Credit: Ghetto Skype (https://github.com/stanfieldr/ghetto-skype)
362478
let imageCache = {};

0 commit comments

Comments
 (0)