Skip to content

Commit

Permalink
Replace XMLHttpRequest with request-promise node module implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
doshprompt committed Apr 11, 2017
1 parent 074cf73 commit 48b1448
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 102 deletions.
27 changes: 3 additions & 24 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,14 @@ const path = require('path');

const {ipcMain} = require('electron');

const version = require('./package').version;
const mb = require('menubar')({
height: 735, // 700 + 35
dir: __dirname,
icon: path.join(__dirname, 'icons', 'IconTemplate.png')
});

ipcMain.on('NOTIFICATION_CLICKED', () => mb.showWindow());

// if (!require('electron-is-dev')) {
// const {autoUpdater, dialog} = require('electron');

// autoUpdater.setFeedURL('http://localhost:3000/updates/latest?v=' + version);

// mb.on('after-create-window', () =>
// autoUpdater.on('update-downloaded', () =>
// dialog.showMessageBox({
// buttons: [ 'Now', 'Later' ],
// icon: path.join(__dirname, 'images', 'logo-vert.png'),
// message: 'Update available!',
// detail: 'Please choose whether you would like to install the new application and restart immediately or not.',
// type: 'info'
// }, response => {
// if (response === 0) {
// autoUpdater.quitAndInstall();
// }
// })
// )
// );
// }
ipcMain.on('NOTIFICATION_CLICKED', () => {
mb.showWindow();
});

require('electron-debug')();
124 changes: 60 additions & 64 deletions app/renderer.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const open = require('open');
const path = require('path');

const {ipcRenderer, remote} = require('electron');
const { ipcRenderer, remote } = require('electron');

const osLocale = require('os-locale');
const isOnline = require('is-online');
const rp = require('request-promise');

const {NotificationCenter} = require('node-notifier');
const notifier = new NotificationCenter({
customPath: __dirname + '/vendor/fast.com .app/Contents/MacOS/fast.com '
});

const OOKLA_TEST_URL = 'https://www.speedtest.net';
const APP_URL = 'https://fast.com';
const FAST_URL = 'https://fast.com';

check = () =>
const check = () => {
isOnline().then(online => {
if (!online) {
getElementsByClassName(document, 'logo-block')[0].style.display = 'none';
Expand All @@ -24,10 +25,11 @@ check = () =>
getElementsByClassName(document, 'error-block')[0].style.display = 'none';

osLocale().then(locale => {
loadPageAsync(APP_URL, locale.replace('_', '-'));
loadPageAsync(FAST_URL, locale.replace('_', '-'));
});
}
});
}

let appLocationTimer;
let appLocationScripts;
Expand All @@ -39,67 +41,61 @@ function loadPageAsync(url, language) {
removeNodes(appLocationScripts);
removeNodes(newLocationStylesheets);

var oReq = new XMLHttpRequest();

oReq.onreadystatechange = function (aEvt) {
if (oReq.readyState === 4) {
if (oReq.status === 200) {
let parser = new DOMParser();
let originalDoc = document;
let newDocument = parser.parseFromString(this.responseText, 'text/html');
let newScripts = importNodes(newDocument, originalDoc, 'script', 'src', url);
let newStylesheets = importNodes(newDocument, originalDoc, 'link', 'href', url);

setTimeout(function() {
// Brutal import
injectNodes(newStylesheets, originalDoc, 'head');
importBody(newDocument, originalDoc);

// Execute JS
injectNodes(newScripts, originalDoc, 'body');

appLocationScripts = newScripts;
newLocationStylesheets = newStylesheets;

// replace links to FAQ documents
replaceLinks(originalDoc, 'span', 'target-language-path', url);

// hide loading indicator
setTimeout(() => getElementsByClassName(originalDoc, 'splash-screen')[0].style.display = 'none', 250);

getElementById(originalDoc, 'test-help-btn').addEventListener('click', () => {
let helpContent = getElementById(originalDoc, 'help-content');
let windowContent = getElementsByClassName(originalDoc, 'window-content')[0];

if (helpContent.style.display === 'block') {
window.scrollTo(0, 0)
} else {
setTimeout(function() {
helpContent.scrollIntoView();
});
}
});


addClassNameListener('#speed-progress-indicator', newClasses => {
if (newClasses.indexOf('in-progress') === -1 && isHidden()) {
notifier.notify({
title: 'Speedtest Complete!',
message: 'Your Speed: ' + document.getElementById('speed-value').innerText + ' ' + document.getElementById('speed-units').innerText,
timeout: true
}).on('click', () => ipcRenderer.send('NOTIFICATION_CLICKED'));
}
});
}, 250);
} else {
check();
}
rp({
url: url,
headers: {
'accept-language': language
}
};

oReq.open('GET', url);
oReq.setRequestHeader('accept-language', language);
oReq.send();
}).then(htmlString => {
let parser = new DOMParser();
let originalDoc = document;
let newDocument = parser.parseFromString(htmlString, 'text/html');
let newScripts = importNodes(newDocument, originalDoc, 'script', 'src', url);
let newStylesheets = importNodes(newDocument, originalDoc, 'link', 'href', url);

setTimeout(function() {
// Brutal import
injectNodes(newStylesheets, originalDoc, 'head');
importBody(newDocument, originalDoc);

// Execute JS
injectNodes(newScripts, originalDoc, 'body');

appLocationScripts = newScripts;
newLocationStylesheets = newStylesheets;

// replace links to FAQ documents
replaceLinks(originalDoc, 'span', 'target-language-path', url);

// hide loading indicator
setTimeout(() => {
getElementsByClassName(originalDoc, 'splash-screen')[0].style.display = 'none';
}, 250);

getElementById(originalDoc, 'test-help-btn').addEventListener('click', () => {
let helpContent = getElementById(originalDoc, 'help-content');
let windowContent = getElementsByClassName(originalDoc, 'window-content')[0];

if (helpContent.style.display === 'block') {
window.scrollTo(0, 0)
} else {
setTimeout(function() {
helpContent.scrollIntoView();
});
}
});

addClassNameListener('#speed-progress-indicator', newClasses => {
if (newClasses.indexOf('in-progress') === -1 && isHidden(document)) {
notifier.notify({
title: 'Speedtest Complete!',
message: 'Your Speed: ' + getElementById(document, 'speed-value').innerText + ' ' + getElementById(document, 'speed-units').innerText,
timeout: true
}).on('click', () => ipcRenderer.send('NOTIFICATION_CLICKED'));
}
});
}, 250);
}).catch(check);
});
}

Expand Down
17 changes: 3 additions & 14 deletions app/src/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ addClassNameListener = (selector, callback) => { // cb
}

// http://stackoverflow.com/a/12537298
isHidden = () => document.hidden || document.msHidden || document.webkitHidden || document.mozHidden;
isHidden = (doc) => doc.hidden || doc.msHidden || doc.webkitHidden || doc.mozHidden;

forEach = (iterable, callback) => Array.prototype.slice.call(iterable || []).forEach(callback);

Expand All @@ -36,12 +36,7 @@ importNodes = (srDoc, destDoc, tagName, attrLocation, documentLocation) => {
let newNode;

// change relative paths to absolute paths using given document location
if (
value &&
value.indexOf('http://') !== 0 &&
value.indexOf('https://') !== 0 &&
value.indexOf(documentLocation) !== 0
) {
if (value && ~~value.indexOf('http://') && ~~value.indexOf('https://') && ~~value.indexOf(documentLocation)) {
node.setAttribute(attrLocation, documentLocation + '/' + value);
}

Expand All @@ -67,15 +62,9 @@ replaceLinks = (doc, tagName, attrLocation, documentLocation) => {

forEach(nodes, node => {
let value = node.getAttribute(attrLocation);
let newNode;

// change relative paths to absolute paths using given document location
if (
value &&
value.indexOf('http://') !== 0 &&
value.indexOf('https://') !== 0 &&
value.indexOf(documentLocation) !== 0
) {
if (value && ~~value.indexOf('http://') && ~~value.indexOf('https://') && ~~value.indexOf(documentLocation)) {
node.setAttribute(attrLocation, documentLocation + '/' + value);
}
});
Expand Down

0 comments on commit 48b1448

Please sign in to comment.