diff --git a/app/inject/inject.css b/app/inject/inject.css new file mode 100644 index 0000000..d8ccc9d --- /dev/null +++ b/app/inject/inject.css @@ -0,0 +1,3 @@ +* { + color: red; +} diff --git a/app/src/components/mainWindow/mainWindow.js b/app/src/components/mainWindow/mainWindow.js index 562eeb4..2d381c3 100644 --- a/app/src/components/mainWindow/mainWindow.js +++ b/app/src/components/mainWindow/mainWindow.js @@ -1,3 +1,4 @@ +import fs from 'fs'; import path from 'path'; import electron from 'electron'; import windowStateKeeper from 'electron-window-state'; @@ -6,7 +7,7 @@ import createMenu from './../menu/menu'; import initContextMenu from './../contextMenu/contextMenu'; const {BrowserWindow, shell, ipcMain, dialog} = electron; -const {isOSX, linkIsInternal} = helpers; +const {isOSX, linkIsInternal, getCssToInject} = helpers; const ZOOM_INTERVAL = 0.1; @@ -106,6 +107,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) { mainWindow.webContents.on('did-finish-load', () => { mainWindow.webContents.send('params', JSON.stringify(options)); + mainWindow.webContents.insertCSS(getCssToInject()); }); if (options.counter) { diff --git a/app/src/helpers/helpers.js b/app/src/helpers/helpers.js index 3683679..463f0c1 100644 --- a/app/src/helpers/helpers.js +++ b/app/src/helpers/helpers.js @@ -1,5 +1,9 @@ import wurl from 'wurl'; import os from 'os'; +import fs from 'fs'; +import path from 'path'; + +const INJECT_CSS_PATH = path.join(__dirname, '..', 'inject/inject.css'); function isOSX() { return os.platform() === 'darwin'; @@ -19,9 +23,18 @@ function linkIsInternal(currentUrl, newUrl) { return currentDomain === newDomain; } +function getCssToInject() { + const needToInject = fs.existsSync(INJECT_CSS_PATH); + if (!needToInject) { + return ''; + } + return fs.readFileSync(INJECT_CSS_PATH).toString(); +} + export default { isOSX, isLinux, isWindows, - linkIsInternal + linkIsInternal, + getCssToInject }; diff --git a/package.json b/package.json index e5ca7e8..c766142 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "clean": "gulp clean", "build": "gulp build", "watch": "while true ; do gulp watch ; done", - "package-placeholder": "npm run build && node lib/cli.js http://www.bennish.net/web-notifications.html ~/Desktop --overwrite --name notification-test --icon ./test-resources/iconSampleGrey.png --inject ./test-resources/test-injection.js && open ~/Desktop/notification-test-darwin-x64/notification-test.app", + "package-placeholder": "npm run build && node lib/cli.js http://www.bennish.net/web-notifications.html ~/Desktop --overwrite --name notification-test --icon ./test-resources/iconSampleGrey.png --inject ./test-resources/test-injection.js --inject ./test-resources/test-injection.css && open ~/Desktop/notification-test-darwin-x64/notification-test.app", "start-placeholder": "npm run build && electron app", "release": "gulp release" }, diff --git a/src/build/buildApp.js b/src/build/buildApp.js index 7c50271..b488567 100644 --- a/src/build/buildApp.js +++ b/src/build/buildApp.js @@ -25,30 +25,53 @@ function buildApp(src, dest, options, callback) { fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs)); - maybeCopyScripts(options.inject, dest, error => { - if (error) { + maybeCopyScripts(options.inject, dest) + .then(() => { + changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl); + callback(); + }) + .catch(error => { callback(error); - return; - } - - changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl); - callback(); - }); + }); }); } -function maybeCopyScripts(src, dest, callback) { - if (!fs.existsSync(src)) { - callback(); - return; - } +function maybeCopyScripts(srcs, dest) { + const promises = srcs.map(src => { + return new Promise((resolve, reject) => { + if (!fs.existsSync(src)) { + resolve(); + return; + } - copy(src, path.join(dest, 'inject', 'inject.js'), error => { - if (error) { - callback(`Error Copying injection files: ${error}`); - return; - } - callback(); + let destFileName; + if (path.extname(src) === '.js') { + destFileName = 'inject.js'; + } else if (path.extname(src) === '.css') { + destFileName = 'inject.css'; + } else { + resolve(); + return; + } + + copy(src, path.join(dest, 'inject', destFileName), error => { + if (error) { + reject(`Error Copying injection files: ${error}`); + return; + } + resolve(); + }); + }); + }); + + return new Promise((resolve, reject) => { + Promise.all(promises) + .then(() => { + resolve(); + }) + .catch(error => { + reject(error); + }); }); } diff --git a/src/cli.js b/src/cli.js index 60d0c4c..88ef0f2 100755 --- a/src/cli.js +++ b/src/cli.js @@ -7,7 +7,13 @@ import program from 'commander'; import nativefier from './index'; const packageJson = require(path.join('..', 'package')); +function collect(val, memo) { + memo.push(val); + return memo; +} + if (require.main === module) { + program .version(packageJson.version) .arguments(' [dest]') @@ -31,7 +37,7 @@ if (require.main === module) { .option('--ignore-certificate', 'ignore certificate related errors') .option('--insecure', 'enable loading of insecure content, defaults to false') .option('--flash ', 'path to Chrome flash plugin, find it in `Chrome://plugins`') - .option('--inject ', 'path to a javascript file to be injected') + .option('--inject ', 'path to a file to be injected', collect, []) .parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/test-resources/test-injection.css b/test-resources/test-injection.css new file mode 100644 index 0000000..437ddb3 --- /dev/null +++ b/test-resources/test-injection.css @@ -0,0 +1,3 @@ +* { + color: blue; +}