diff --git a/CHANGELOG.md b/CHANGELOG.md index 232fcaf..efd549a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,13 @@ Markbot adheres to [Semantic Versioning](http://semver.org/). ### Changed - Move the `DEBUG` & the `serverPort` settings into the `package.json` file for simpler editing. +- Rewrote the `require()` statements to better work off the main process. +- Rewrote how the `markbot-main` module finds the main window to be more reliable. + +### Fixed + +- Fixed a bug in the screenshots where it didn’t tally which screenshots were completed properly. +- Added back the `asar` packing for the application. --- diff --git a/app/checks/all-files/html-unique.js b/app/checks/all-files/html-unique.js index 3df31b9..bfd4aef 100644 --- a/app/checks/all-files/html-unique.js +++ b/app/checks/all-files/html-unique.js @@ -3,7 +3,7 @@ const fs = require('fs'); const path = require('path'); const cheerio = require('cheerio'); -const exists = require('../../file-exists'); +const exists = require(__dirname + '/../../file-exists'); module.exports.find = function (folderPath, file, uniqueElems) { const fullPath = path.resolve(folderPath + '/' + file.path); diff --git a/app/checks/all-files/task.js b/app/checks/all-files/task.js index 32c6b98..3c496ed 100644 --- a/app/checks/all-files/task.js +++ b/app/checks/all-files/task.js @@ -2,9 +2,8 @@ 'use strict'; const path = require('path'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); - const htmlUnique = main.require('./app/checks/all-files/html-unique'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const htmlUnique = require(__dirname + '/checks/all-files/html-unique'); const group = taskDetails.group; const id = 'html-unique'; diff --git a/app/checks/css/best-practices.js b/app/checks/css/best-practices.js index c6a80ea..16e396f 100644 --- a/app/checks/css/best-practices.js +++ b/app/checks/css/best-practices.js @@ -2,8 +2,8 @@ const util = require('util'); const linter = require('stylelint'); -const viewportChecker = require('./best-practices/viewport'); -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); +const viewportChecker = require(__dirname + '/best-practices/viewport'); const shouldIncludeError = function (message, line, lines, fileContents) { /* SVG overflow: hidden CSS */ diff --git a/app/checks/css/properties.js b/app/checks/css/properties.js index 4d89b7f..c62a91d 100644 --- a/app/checks/css/properties.js +++ b/app/checks/css/properties.js @@ -1,7 +1,7 @@ 'use strict'; const css = require('css'); -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const bypass = function (checkGroup, checkId, checkLabel) { markbotMain.send('check-group:item-bypass', checkGroup, checkId, checkLabel, ['Skipped because of previous errors']); diff --git a/app/checks/css/task.js b/app/checks/css/task.js index 825ad0c..9ef3692 100644 --- a/app/checks/css/task.js +++ b/app/checks/css/task.js @@ -3,13 +3,12 @@ const fs = require('fs'); const path = require('path'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); - const exists = main.require('./app/file-exists'); - const validation = main.require('./app/checks/css/validation'); - const bestPractices = main.require('./app/checks/css/best-practices'); - const properties = main.require('./app/checks/css/properties'); - const content = main.require('./app/checks/content'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const exists = require(__dirname + '/file-exists'); + const validation = require(__dirname + '/checks/css/validation'); + const bestPractices = require(__dirname + '/checks/css/best-practices'); + const properties = require(__dirname + '/checks/css/properties'); + const content = require(__dirname + '/checks/content'); const group = taskDetails.group; const file = taskDetails.options.file; diff --git a/app/checks/css/validation.js b/app/checks/css/validation.js index f38fdda..4b4eb58 100644 --- a/app/checks/css/validation.js +++ b/app/checks/css/validation.js @@ -4,7 +4,7 @@ const path = require('path'); const util = require('util'); const exec = require('child_process').exec; const xmlParser = require('xml2js').parseString; -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); /** * This function is mainly to work around Windows issues diff --git a/app/checks/files/task.js b/app/checks/files/task.js index c398df0..ded138a 100644 --- a/app/checks/files/task.js +++ b/app/checks/files/task.js @@ -1,18 +1,17 @@ (function () { 'use strict'; - const main = require('electron').remote; const fs = require('fs'); const path = require('path'); const util = require('util'); - const calipers = main.require('calipers')('png', 'jpeg'); - const exif = main.require('exif').ExifImage; - const pngitxt = main.require('png-itxt'); - const merge = main.require('merge-objects'); - const markbotMain = main.require('./app/markbot-main'); - const stripPath = main.require('./app/strip-path'); - const exists = main.require('./app/file-exists'); - const listDir = main.require('./app/list-dir'); + const calipers = require('calipers')('png', 'jpeg'); + const exif = require('exif').ExifImage; + const pngitxt = require('png-itxt'); + const merge = require('merge-objects'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const stripPath = require(__dirname + '/strip-path'); + const exists = require(__dirname + '/file-exists'); + const listDir = require(__dirname + '/list-dir'); const group = taskDetails.group; let totalFiles = 0; diff --git a/app/checks/functionality/injection.js b/app/checks/functionality/injection.js index ea0b606..ac4ddba 100644 --- a/app/checks/functionality/injection.js +++ b/app/checks/functionality/injection.js @@ -100,12 +100,10 @@ const __MarkbotInjectedFunctions = { }, pass: function () { - console.log('Pass!', __MarkbotInjectedFunctions.failed); if (!__MarkbotInjectedFunctions.failed) __MarkbotInjectedFunctions.taskRunner.send(__MarkbotInjectedFunctions.passLabel); }, fail: function (reason) { - console.log('Fail!'); __MarkbotInjectedFunctions.failed = true; __MarkbotInjectedFunctions.taskRunner.send(__MarkbotInjectedFunctions.failLabel, reason); }, diff --git a/app/checks/functionality/task.js b/app/checks/functionality/task.js index e83f756..45b35f8 100644 --- a/app/checks/functionality/task.js +++ b/app/checks/functionality/task.js @@ -3,13 +3,12 @@ const fs = require('fs'); const path = require('path'); - const main = require('electron').remote; const ipcRenderer = require('electron').ipcRenderer; - const markbotMain = main.require('./app/markbot-main'); - const fileExists = main.require('./app/file-exists'); - const classify = main.require('./app/classify'); - const webLoader = main.require('./app/web-loader'); - const defaultsService = main.require('./app/checks/functionality/defaults-service'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const fileExists = require(__dirname + '/file-exists'); + const classify = require(__dirname + '/classify'); + const webLoader = require(__dirname + '/web-loader'); + const defaultsService = require(__dirname + '/checks/functionality/defaults-service'); const injectionJs = defaultsService.get('injection.js'); const group = taskDetails.group; @@ -114,7 +113,7 @@ markbotMain.debug(...e); }); - webLoader.load(file.path, {}, function (theWindow) { + webLoader.load(taskRunnerId, file.path, {}, function (theWindow) { win = theWindow; if (file.tests) runTest(win, file.tests.shift(), currentTestIndex, listenerLabel); diff --git a/app/checks/git/commits.js b/app/checks/git/commits.js index 32150d7..390e5d9 100644 --- a/app/checks/git/commits.js +++ b/app/checks/git/commits.js @@ -4,8 +4,8 @@ const fs = require('fs'); const path = require('path'); const util = require('util'); const gitCommits = require('git-commits'); -const exists = require('../../file-exists'); -const markbotMain = require('../../markbot-main'); +const exists = require(__dirname + '/../../file-exists'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const matchesProfEmail = function (email, profEmails) { return !profEmails.indexOf(email); diff --git a/app/checks/git/status.js b/app/checks/git/status.js index bf430c0..6a3faf0 100644 --- a/app/checks/git/status.js +++ b/app/checks/git/status.js @@ -4,7 +4,7 @@ const fs = require('fs'); const path = require('path'); const exec = require('child_process').exec; const gitStatus = require('git-get-status'); -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); module.exports.check = function (fullPath, gitOpts, group, next) { const allSynced = 'all-synced'; diff --git a/app/checks/git/task-generator.js b/app/checks/git/task-generator.js index 1f4bf6a..7056179 100644 --- a/app/checks/git/task-generator.js +++ b/app/checks/git/task-generator.js @@ -1,6 +1,6 @@ 'use strict'; -let config = require('../../../config.json'); +let config = require(__dirname + '/../../../config.json'); module.exports.generateTaskList = function (markbotFile) { var tasks = []; diff --git a/app/checks/git/task.js b/app/checks/git/task.js index 4486bfa..6493d9f 100644 --- a/app/checks/git/task.js +++ b/app/checks/git/task.js @@ -2,10 +2,9 @@ 'use strict'; const path = require('path'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); - const commits = main.require('./app/checks/git/commits'); - const status = main.require('./app/checks/git/status'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const commits = require(__dirname + '/checks/git/commits'); + const status = require(__dirname + '/checks/git/status'); const fullPath = path.resolve(taskDetails.cwd); diff --git a/app/checks/html/best-practices.js b/app/checks/html/best-practices.js index 4a3dfe9..287147a 100644 --- a/app/checks/html/best-practices.js +++ b/app/checks/html/best-practices.js @@ -1,17 +1,17 @@ 'use strict'; const util = require('util'); -const documentTagChecker = require('./best-practices/document-tags'); -const lineBreakChecker = require('./best-practices/force-line-breaks'); -const pTagCloseChecker = require('./best-practices/close-p-on-same-line'); -const missingOptionalTagChecker = require('./best-practices/missing-optional-closing-tags'); -const codeStyleChecker = require('./best-practices/code-style'); -const emptyLineChecker = require('./best-practices/max-empty-lines'); -const viewportChecker = require('./best-practices/viewport'); -const doublespaceChecker = require('./best-practices/double-space'); -const spaceBeforeCloseGTChecker = require('./best-practices/space-before-close-greater-than'); -const indentationChecker = require('./best-practices/indentation'); -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); +const documentTagChecker = require(__dirname + '/best-practices/document-tags'); +const lineBreakChecker = require(__dirname + '/best-practices/force-line-breaks'); +const pTagCloseChecker = require(__dirname + '/best-practices/close-p-on-same-line'); +const missingOptionalTagChecker = require(__dirname + '/best-practices/missing-optional-closing-tags'); +const codeStyleChecker = require(__dirname + '/best-practices/code-style'); +const emptyLineChecker = require(__dirname + '/best-practices/max-empty-lines'); +const viewportChecker = require(__dirname + '/best-practices/viewport'); +const doublespaceChecker = require(__dirname + '/best-practices/double-space'); +const spaceBeforeCloseGTChecker = require(__dirname + '/best-practices/space-before-close-greater-than'); +const indentationChecker = require(__dirname + '/best-practices/indentation'); const bypass = function (checkGroup, checkId, checkLabel) { markbotMain.send('check-group:item-bypass', checkGroup, checkId, checkLabel, ['Skipped because of previous errors']); diff --git a/app/checks/html/elements.js b/app/checks/html/elements.js index 1c5ebc4..5f67f54 100644 --- a/app/checks/html/elements.js +++ b/app/checks/html/elements.js @@ -2,7 +2,7 @@ const util = require('util'); const cheerio = require('cheerio'); -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const bypass = function (checkGroup, checkId, checkLabel) { markbotMain.send('check-group:item-bypass', checkGroup, checkId, checkLabel, ['Skipped because of previous errors']); diff --git a/app/checks/html/outline.js b/app/checks/html/outline.js index 74625fd..8ff8a47 100644 --- a/app/checks/html/outline.js +++ b/app/checks/html/outline.js @@ -2,7 +2,7 @@ const util = require('util'); const cheerio = require('cheerio'); -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const getLevel = function (elem) { return parseInt(elem.name.slice(1), 10); diff --git a/app/checks/html/task.js b/app/checks/html/task.js index 1c31d03..6545a4f 100644 --- a/app/checks/html/task.js +++ b/app/checks/html/task.js @@ -3,14 +3,13 @@ const fs = require('fs'); const path = require('path'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); - const exists = main.require('./app/file-exists'); - const validation = main.require('./app/checks/html/validation'); - const bestPractices = main.require('./app/checks/html/best-practices'); - const outline = main.require('./app/checks/html/outline'); - const elements = main.require('./app/checks/html/elements'); - const content = main.require('./app/checks/content'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const exists = require(__dirname + '/file-exists'); + const validation = require(__dirname + '/checks/html/validation'); + const bestPractices = require(__dirname + '/checks/html/best-practices'); + const outline = require(__dirname + '/checks/html/outline'); + const elements = require(__dirname + '/checks/html/elements'); + const content = require(__dirname + '/checks/content'); const group = taskDetails.group; const file = taskDetails.options.file; diff --git a/app/checks/html/validation.js b/app/checks/html/validation.js index bfa4f89..236df79 100644 --- a/app/checks/html/validation.js +++ b/app/checks/html/validation.js @@ -3,7 +3,7 @@ const util = require('util'); const path = require('path'); const exec = require('child_process').exec; -const markbotMain = require('../../markbot-main'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const escapeShell = function (cmd) { return '"' + cmd.replace(/(["'$`\\])/g, '\\$1') + '"'; diff --git a/app/checks/javascript/best-practices.js b/app/checks/javascript/best-practices.js index d355916..8517614 100644 --- a/app/checks/javascript/best-practices.js +++ b/app/checks/javascript/best-practices.js @@ -2,8 +2,8 @@ const util = require('util'); const linter = require('eslint').linter; -const linterConfig = require('./best-practices/eslint.json'); -const markbotMain = require('../../markbot-main'); +const linterConfig = require(__dirname + '/best-practices/eslint.json'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const bypass = function (checkGroup, checkId, checkLabel) { markbotMain.send('check-group:item-bypass', checkGroup, checkId, checkLabel, ['Skipped because of previous errors']); diff --git a/app/checks/javascript/task.js b/app/checks/javascript/task.js index d5d270f..7d44cc0 100644 --- a/app/checks/javascript/task.js +++ b/app/checks/javascript/task.js @@ -3,12 +3,11 @@ const fs = require('fs'); const path = require('path'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); - const exists = main.require('./app/file-exists'); - const validation = main.require('./app/checks/javascript/validation'); - const bestPractices = main.require('./app/checks/javascript/best-practices'); - const content = main.require('./app/checks/content'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const exists = require(__dirname + '/file-exists'); + const validation = require(__dirname + '/checks/javascript/validation'); + const bestPractices = require(__dirname + '/checks/javascript/best-practices'); + const content = require(__dirname + '/checks/content'); const group = taskDetails.group; const file = taskDetails.options.file; diff --git a/app/checks/javascript/validation.js b/app/checks/javascript/validation.js index 065c614..724eaa6 100644 --- a/app/checks/javascript/validation.js +++ b/app/checks/javascript/validation.js @@ -3,8 +3,8 @@ const util = require('util'); const path = require('path'); const linter = require('eslint').linter; -const linterConfig = require('./validation/eslint.json'); -const markbotMain = require('../../markbot-main'); +const linterConfig = require(__dirname + '/validation/eslint.json'); +const markbotMain = require('electron').remote.require('./app/markbot-main'); const bypass = function (checkGroup, checkId, checkLabel) { markbotMain.send('check-group:item-bypass', checkGroup, checkId, checkLabel, ['Skipped because of previous errors']); diff --git a/app/checks/live-website/task.js b/app/checks/live-website/task.js index d70c0c3..eb4f4d0 100644 --- a/app/checks/live-website/task.js +++ b/app/checks/live-website/task.js @@ -2,8 +2,7 @@ 'use strict'; const https = require('https'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); const group = taskDetails.group; const repo = taskDetails.options.repo; diff --git a/app/checks/naming-conventions/task.js b/app/checks/naming-conventions/task.js index 05c289b..f0a8cca 100644 --- a/app/checks/naming-conventions/task.js +++ b/app/checks/naming-conventions/task.js @@ -2,16 +2,15 @@ 'use strict'; const path = require('path'); - const main = require('electron').remote; - const markbotMain = main.require('./app/markbot-main'); - const listDir = main.require('./app/list-dir'); - const stripPath = main.require('./app/strip-path'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const listDir = require(__dirname + '/list-dir'); + const stripPath = require(__dirname + '/strip-path'); - const extsBlackList = main.require('./app/checks/naming-conventions/extension-blacklist.json'); + const extsBlackList = require(__dirname + '/checks/naming-conventions/extension-blacklist.json'); const extsBlackListSearch = `(${extsBlackList.join('|')})$`; - const fileBlackList = main.require('./app/checks/naming-conventions/file-blacklist.json'); + const fileBlackList = require(__dirname + '/checks/naming-conventions/file-blacklist.json'); const fileBlackListSearch = `(${fileBlackList.join('|')})`; - const pathsWhiteList = main.require('./app/checks/naming-conventions/path-whitelist.json'); + const pathsWhiteList = require(__dirname + '/checks/naming-conventions/path-whitelist.json'); const pathsWhiteListSearch = `^(${pathsWhiteList.join('|').replace(/\./ig, '\.')})`; const fullPath = path.resolve(taskDetails.cwd); diff --git a/app/checks/performance/task.js b/app/checks/performance/task.js index f207559..e9c868d 100644 --- a/app/checks/performance/task.js +++ b/app/checks/performance/task.js @@ -1,22 +1,21 @@ (function () { 'use strict'; - const main = require('electron').remote; const path = require('path'); - const merge = main.require('merge-objects'); - const webcoach = main.require('webcoach'); + const merge = require('merge-objects'); + const webcoach = require('webcoach'); const ipcRenderer = require('electron').ipcRenderer; - const exists = main.require('./app/file-exists'); - const markbotMain = main.require('./app/markbot-main'); - const webLoader = main.require('./app/web-loader'); - const webServer = main.require('./app/web-server'); - const adviceIgnoreIds = main.require('./app/checks/performance/ignore-advice-ids.json'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const exists = require(__dirname + '/file-exists'); + const webLoader = require(__dirname + '/web-loader'); + const webServer = require(__dirname + '/web-server'); + const adviceIgnoreIds = require(__dirname + '/checks/performance/ignore-advice-ids.json'); const group = taskDetails.group; const fullPath = taskDetails.cwd; let totalFiles = 0; - +console.log(markbotMain); const perfDefaults = { speed: 'WIFI', budget: { @@ -179,7 +178,7 @@ return done(); } - webLoader.load(file.path, {speed: perf.speed}, function (theWindow, theHar) { + webLoader.load(taskRunnerId, file.path, {speed: perf.speed}, function (theWindow, theHar) { win = theWindow; har = theHar; diff --git a/app/checks/screenshots/naming-service.js b/app/checks/screenshots/naming-service.js index 00ec174..8e878d2 100644 --- a/app/checks/screenshots/naming-service.js +++ b/app/checks/screenshots/naming-service.js @@ -2,7 +2,7 @@ const path = require('path'); const is = require('electron-is'); -const classify = require('../../classify'); +const classify = require(__dirname + '/../../classify'); const SCREENSHOT_PREFIX = 'markbot'; const REFERENCE_SCREENSHOT_FOLDER = 'screenshots'; diff --git a/app/checks/screenshots/task.js b/app/checks/screenshots/task.js index f858b07..740adc4 100644 --- a/app/checks/screenshots/task.js +++ b/app/checks/screenshots/task.js @@ -5,19 +5,18 @@ const MAX_WINDOW_HEIGHT = 6000; const MAX_WINDOW_WIDTH = 3000; - const main = require('electron').remote; const fs = require('fs'); const path = require('path'); const fork = require('child_process').fork; - const jimp = main.require('jimp'); + const jimp = require('jimp'); + const BrowserWindow = require('electron').remote.BrowserWindow; const ipcRenderer = require('electron').ipcRenderer; - const markbotMain = main.require('./app/markbot-main'); - const BrowserWindow = main.BrowserWindow; - const fileExists = main.require('./app/file-exists'); - const webLoader = main.require('./app/web-loader'); - const classify = main.require('./app/classify'); - const screenshotNamingService = main.require('./app/checks/screenshots/naming-service'); - const defaultsService = main.require('./app/checks/screenshots/defaults-service'); + const markbotMain = require('electron').remote.require('./app/markbot-main'); + const fileExists = require(__dirname + '/file-exists'); + const webLoader = require(__dirname + '/web-loader'); + const classify = require(__dirname + '/classify'); + const screenshotNamingService = require(__dirname + '/checks/screenshots/naming-service'); + const defaultsService = require(__dirname + '/checks/screenshots/defaults-service'); const defaultScreenshotCSS = defaultsService.get('default.css'); const defaultScreenshotJS = defaultsService.get('default.js'); @@ -115,7 +114,8 @@ const ipcListenerResizeChannel = `__markbot-screenshots-resized-${ipcListenerLabel}`; const listenerId = function (size) { return `${file.path}-${size}`; }; const listenerLabel = function (size) { return `${file.path} — ${size}px`; }; - let screenshotSizes = file.sizes.slice(); + let screenshotSizes = file.sizes.slice(0); + let screenshotSizesDiffing = []; let screenshotSizesDone = []; const nextScreenshot = function (windowId) { @@ -143,13 +143,17 @@ }; ipcRenderer.on(ipcListenerResizeChannel, function (event, windowId, width, height) { + if (screenshotSizesDiffing.indexOf(width) >= 0) return; + + screenshotSizesDiffing.push(width); + takeScreenshotAtSize(windowId, width, height, function (img) { let imagePath = screenshotNamingService.getScreenshotPath(fullPath, file.path, width); saveScreenshot(imagePath, width, img, function () { if (fileExists.check(screenshotNamingService.getScreenshotPath(fullPath, file.path, width, true))) { diffScreenshot(fullPath, group, file.path, width, function (w) { - screenshotSizesDone.push(w); + if (screenshotSizesDone.indexOf(w) < 0) screenshotSizesDone.push(w); checkAllDiffsDone(); }); } else { @@ -179,7 +183,7 @@ }); } - webLoader.load(file.path, {width: MAX_WINDOW_WIDTH, height: MAX_WINDOW_HEIGHT}, function (theWindow) { + webLoader.load(taskRunnerId, file.path, {width: MAX_WINDOW_WIDTH, height: MAX_WINDOW_HEIGHT}, function (theWindow) { theWindow.webContents.insertCSS(defaultScreenshotCSS); theWindow.webContents.executeJavaScript(getResizeInjectionJs(theWindow.id, taskRunnerId, ipcListenerResizeChannel), function (windowId) { nextScreenshot(windowId); @@ -197,4 +201,5 @@ totalFiles++; check(folderPath, file, checkIfDone); }); + }()); diff --git a/app/hidden-browser-window-preload.js b/app/hidden-browser-window-preload.js index e72ed09..0bed104 100644 --- a/app/hidden-browser-window-preload.js +++ b/app/hidden-browser-window-preload.js @@ -5,8 +5,12 @@ delete window.require; delete window.exports; delete window.module; +window.__markbotGetBrowserWindow = function () { + return nodeRequire('electron').remote.BrowserWindow.fromId(window.__markbot_hidden_browser_window_id).webContents; +}; + window.addEventListener('error', function (err) { - nodeRequire('electron').ipcRenderer.send('__markbot-functionality-error', err.message, err.lineno, err.filename); + __markbotGetBrowserWindow().send('__markbot-functionality-error', err.message, err.lineno, err.filename); }); window.addEventListener('load', function (ev) { @@ -15,7 +19,7 @@ window.addEventListener('load', function (ev) { window.requestAnimationFrame(function () { window.requestAnimationFrame(function () { process.nextTick(function () { - nodeRequire('electron').ipcRenderer.send('__markbot-hidden-browser-window-loaded', true); + window.__markbotGetBrowserWindow().send('__markbot-hidden-browser-window-loaded', {location: window.location.href}); }); }); }); @@ -29,7 +33,7 @@ document.fonts.ready.then(function () { window.requestAnimationFrame(function () { window.requestAnimationFrame(function () { process.nextTick(function () { - nodeRequire('electron').ipcRenderer.send('__markbot-hidden-browser-window-fonts-loaded', true); + window.__markbotGetBrowserWindow().send('__markbot-hidden-browser-window-fonts-loaded', {location: window.location.href}); }); }); }); diff --git a/app/markbot-main.js b/app/markbot-main.js index 666c8c9..de0fb0d 100644 --- a/app/markbot-main.js +++ b/app/markbot-main.js @@ -1,13 +1,29 @@ 'use strict'; +const is = require('electron-is'); + let markbotMain; const init = function (main) { - markbotMain = main; + let electron; + + if (is.renderer()) { + electron = require('electron').remote; + markbotMain = electron.BrowserWindow.fromId(electron.getGlobal('markbotMainWindow')).webContents; + } else { + electron = require('electron'); + markbotMain = electron.BrowserWindow.fromId(global.markbotMainWindow).webContents; + } }; +const destroy = function () { + markbotMain = null; +} + const send = function (label, ...messages) { + init(); markbotMain.send(label, ...messages); + destroy(); }; const debug = function (...messages) { @@ -15,7 +31,6 @@ const debug = function (...messages) { }; module.exports = { - init: init, send: send, debug: debug, }; diff --git a/app/web-loader.js b/app/web-loader.js index 7a4470d..cd124ac 100644 --- a/app/web-loader.js +++ b/app/web-loader.js @@ -4,12 +4,12 @@ const PRELOAD_JS = __dirname + '/hidden-browser-window-preload.js'; const PRELOAD_PATH = 'chrome://ensure-electron-resolution/'; const path = require('path'); -const ipcMain = require('electron').ipcMain; -const BrowserWindow = require('electron').BrowserWindow; -const networks = require('./networks'); -const webServer = require('./web-server'); -const markbotMain = require('./markbot-main'); -const appPkg = require('../package.json'); +const ipcRenderer = require('electron').ipcRenderer; +const BrowserWindow = require('electron').remote.BrowserWindow; +const markbotMain = require('electron').remote.require('./app/markbot-main'); +const networks = require(__dirname + '/networks'); +const webServer = require(__dirname + '/web-server'); +const appPkg = require(__dirname + '/../package.json'); const DEBUG = appPkg.config.DEBUG; @@ -49,7 +49,7 @@ const destroy = function (win) { } }; -const load = function (url, opts, next) { +const load = function (listenerId, url, opts, next) { let win; let speed = false; let networkName; @@ -60,33 +60,33 @@ const load = function (url, opts, next) { let onFinishLoadFired = false; const cleanup = function () { - ipcMain.removeAllListeners('__markbot-hidden-browser-devtools-loaded'); - ipcMain.removeAllListeners('__markbot-hidden-browser-window-loaded'); - ipcMain.removeAllListeners('__markbot-hidden-browser-window-fonts-loaded'); - ipcMain.removeAllListeners('__markbot-hidden-browser-har-generation-succeeded'); + ipcRenderer.removeAllListeners('__markbot-hidden-browser-devtools-loaded'); + ipcRenderer.removeAllListeners('__markbot-hidden-browser-window-loaded'); + ipcRenderer.removeAllListeners('__markbot-hidden-browser-window-fonts-loaded'); + ipcRenderer.removeAllListeners('__markbot-hidden-browser-har-generation-succeeded'); win.closeDevTools(); }; - const notifyDevToolsExtensionOfLoad = function (e) { - if (e.sender.getURL() != PRELOAD_PATH) { + const notifyDevToolsExtensionOfLoad = function (loc) { + if (loc != PRELOAD_PATH) { win.webContents.executeJavaScript('new Image().src = "https://did-finish-load/"'); } }; - const waitForFinalLoad = function (e) { + const waitForFinalLoad = function (loc) { // The `did-finish-load` & `dom-ready` events often fire too soon to execute JS in the window const isLoading = setInterval(function () { if (!win.webContents.isLoading()) { clearInterval(isLoading); - notifyDevToolsExtensionOfLoad(e); + notifyDevToolsExtensionOfLoad(loc); } }, 20); }; - const checkForFinalLoad = function (e) { - if (e.sender.getURL() != PRELOAD_PATH && didFinishLoad && domReady && windowLoaded && fontsReady && !onFinishLoadFired) { + const checkForFinalLoad = function (loc) { + if (loc != PRELOAD_PATH && didFinishLoad && domReady && windowLoaded && fontsReady && !onFinishLoadFired) { onFinishLoadFired = true; - waitForFinalLoad(e); + waitForFinalLoad(loc); } }; @@ -104,38 +104,39 @@ const load = function (url, opts, next) { win.webContents.on('did-finish-load', function (e) { if (e.sender.getURL() != PRELOAD_PATH) { didFinishLoad = true; - checkForFinalLoad(e); + checkForFinalLoad(e.sender.getURL()); } }); win.webContents.on('dom-ready', function (e) { if (e.sender.getURL() != PRELOAD_PATH) { domReady = true; - checkForFinalLoad(e); + checkForFinalLoad(e.sender.getURL()); } }); - ipcMain.on('__markbot-hidden-browser-devtools-loaded', function (e) { + ipcRenderer.on('__markbot-hidden-browser-devtools-loaded', function (e) { process.nextTick(function () { win.loadURL(getUrl(url), {'extraHeaders': 'Pragma: no-cache\n'}); + win.webContents.executeJavaScript(`window.__markbot_hidden_browser_window_id = ${listenerId};`); }); }); - ipcMain.on('__markbot-hidden-browser-window-fonts-loaded', function (e, details) { - if (e.sender.getURL() != PRELOAD_PATH) { + ipcRenderer.on('__markbot-hidden-browser-window-fonts-loaded', function (e, details) { + if (details.location != PRELOAD_PATH) { fontsReady = true; - checkForFinalLoad(e); + checkForFinalLoad(details.location); } }); - ipcMain.on('__markbot-hidden-browser-window-loaded', function (e, details) { - if (e.sender.getURL() != PRELOAD_PATH) { + ipcRenderer.on('__markbot-hidden-browser-window-loaded', function (e, details) { + if (details.location != PRELOAD_PATH) { windowLoaded = true; - checkForFinalLoad(e); + checkForFinalLoad(details.location); } }); - ipcMain.once('__markbot-hidden-browser-har-generation-succeeded', function (e, har) { + ipcRenderer.once('__markbot-hidden-browser-har-generation-succeeded', function (e, har) { cleanup(); next(win, har); }); @@ -148,6 +149,7 @@ const load = function (url, opts, next) { } win.loadURL(PRELOAD_PATH, {'extraHeaders': 'Pragma: no-cache\n'}); + win.webContents.executeJavaScript(`window.__markbot_hidden_browser_window_id = ${listenerId};`); }; module.exports = { diff --git a/devtools-har-extension/index.js b/devtools-har-extension/index.js index d4baf73..119653e 100755 --- a/devtools-har-extension/index.js +++ b/devtools-har-extension/index.js @@ -6,9 +6,9 @@ chrome.devtools.network.onRequestFinished.addListener(function (request) { har.entries = har.entries.filter(function (e) { return e.request.url !== LOAD_INDICATOR; }); - chrome.devtools.inspectedWindow.eval(`nodeRequire("electron").ipcRenderer.send("__markbot-hidden-browser-har-generation-succeeded", ${JSON.stringify({log:har})});`); + chrome.devtools.inspectedWindow.eval(`window.__markbotGetBrowserWindow().send("__markbot-hidden-browser-har-generation-succeeded", ${JSON.stringify({log:har})});`); }); } }); -chrome.devtools.inspectedWindow.eval('nodeRequire("electron").ipcRenderer.send("__markbot-hidden-browser-devtools-loaded");'); +chrome.devtools.inspectedWindow.eval('window.__markbotGetBrowserWindow().send("__markbot-hidden-browser-devtools-loaded");'); diff --git a/markbot.js b/markbot.js index 4407db1..e7f0bd6 100644 --- a/markbot.js +++ b/markbot.js @@ -88,7 +88,8 @@ const createMainWindow = function () { mainWindow.show(); }); - markbotMain.init(mainWindow.webContents); + global.markbotMainWindow = mainWindow.id; + if (appPkg.config.DEBUG) console.log(`Main window: ${mainWindow.id}`); }; const createDebugWindow = function () {