Skip to content

Commit 60d10d8

Browse files
aashish-akakashnimare
authored andcommitted
preload: use page_params only when it exists.
This PR adds a params-util.js file which checks wheather the page_params exists or not. Fixes: #517.
1 parent 124a842 commit 60d10d8

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

app/renderer/js/notification/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
remote: { app }
55
} = require('electron');
66

7+
const params = require('../utils/params-util.js');
78
const DefaultNotification = require('./default-notification');
89
const { appId, loadBots } = require('./helpers');
910

@@ -19,9 +20,8 @@ if (process.platform === 'darwin') {
1920
}
2021

2122
window.addEventListener('load', () => {
22-
// Call this function only when user is logged in
2323
// eslint-disable-next-line no-undef, camelcase
24-
if (page_params.realm_uri) {
24+
if (params.isPageParams() && page_params.realm_uri) {
2525
loadBots();
2626
}
2727
});

app/renderer/js/preload.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const SetupSpellChecker = require('./spellchecker');
55

66
const ConfigUtil = require(__dirname + '/utils/config-util.js');
77
const LinkUtil = require(__dirname + '/utils/link-util.js');
8+
const params = require(__dirname + '/utils/params-util.js');
89

910
// eslint-disable-next-line import/no-unassigned-import
1011
require('./notification');
@@ -42,46 +43,44 @@ process.once('loaded', () => {
4243

4344
// To prevent failing this script on linux we need to load it after the document loaded
4445
document.addEventListener('DOMContentLoaded', () => {
46+
if (params.isPageParams()) {
4547
// Get the default language of the server
46-
const serverLanguage = page_params.default_language; // eslint-disable-line no-undef, camelcase
47-
48-
if (serverLanguage) {
49-
// Set spellcheker language
50-
ConfigUtil.setConfigItem('spellcheckerLanguage', serverLanguage);
51-
// Init spellchecker
52-
SetupSpellChecker.init();
53-
}
54-
55-
// redirect users to network troubleshooting page
56-
const getRestartButton = document.querySelector('.restart_get_events_button');
57-
if (getRestartButton) {
58-
getRestartButton.addEventListener('click', () => {
59-
ipcRenderer.send('forward-message', 'reload-viewer');
48+
const serverLanguage = page_params.default_language; // eslint-disable-line no-undef, camelcase
49+
if (serverLanguage) {
50+
// Set spellcheker language
51+
ConfigUtil.setConfigItem('spellcheckerLanguage', serverLanguage);
52+
// Init spellchecker
53+
SetupSpellChecker.init();
54+
}
55+
// redirect users to network troubleshooting page
56+
const getRestartButton = document.querySelector('.restart_get_events_button');
57+
if (getRestartButton) {
58+
getRestartButton.addEventListener('click', () => {
59+
ipcRenderer.send('forward-message', 'reload-viewer');
60+
});
61+
}
62+
// Open image attachment link in the lightbox instead of opening in the default browser
63+
const { $, lightbox } = window;
64+
$('#main_div').on('click', '.message_content p a', function (e) {
65+
const url = $(this).attr('href');
66+
67+
if (LinkUtil.isImage(url)) {
68+
const $img = $(this).parent().siblings('.message_inline_image').find('img');
69+
70+
// prevent the image link from opening in a new page.
71+
e.preventDefault();
72+
// prevent the message compose dialog from happening.
73+
e.stopPropagation();
74+
75+
// Open image in the default browser if image preview is unavailable
76+
if (!$img[0]) {
77+
shell.openExternal(window.location.origin + url);
78+
}
79+
// Open image in lightbox
80+
lightbox.open($img);
81+
}
6082
});
6183
}
62-
63-
// Open image attachment link in the lightbox instead of opening in the default browser
64-
const { $, lightbox } = window;
65-
66-
$('#main_div').on('click', '.message_content p a', function (e) {
67-
const url = $(this).attr('href');
68-
69-
if (LinkUtil.isImage(url)) {
70-
const $img = $(this).parent().siblings('.message_inline_image').find('img');
71-
72-
// prevent the image link from opening in a new page.
73-
e.preventDefault();
74-
// prevent the message compose dialog from happening.
75-
e.stopPropagation();
76-
77-
// Open image in the default browser if image preview is unavailable
78-
if (!$img[0]) {
79-
shell.openExternal(window.location.origin + url);
80-
}
81-
// Open image in lightbox
82-
lightbox.open($img);
83-
}
84-
});
8584
});
8685

8786
// Clean up spellchecker events after you navigate away from this page;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This util function returns the page params if they're present else returns null
2+
function isPageParams() {
3+
let webpageParams = null;
4+
try {
5+
// eslint-disable-next-line no-undef, camelcase
6+
webpageParams = page_params;
7+
} catch (err) {
8+
webpageParams = null;
9+
}
10+
return webpageParams;
11+
}
12+
13+
module.exports = {
14+
isPageParams
15+
};

0 commit comments

Comments
 (0)