diff --git a/app/main.js b/app/main.js index b9adf36..a87a87e 100644 --- a/app/main.js +++ b/app/main.js @@ -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')(); diff --git a/app/renderer.js b/app/renderer.js index 45fd50a..0fec262 100644 --- a/app/renderer.js +++ b/app/renderer.js @@ -1,10 +1,11 @@ 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({ @@ -12,9 +13,9 @@ const notifier = new NotificationCenter({ }); 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'; @@ -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; @@ -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); }); } diff --git a/app/src/scripts.js b/app/src/scripts.js index 8b0704a..e926327 100644 --- a/app/src/scripts.js +++ b/app/src/scripts.js @@ -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); @@ -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); } @@ -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); } });