diff --git a/app/inject/_placeholder b/app/inject/_placeholder new file mode 100644 index 0000000..e69de29 diff --git a/app/src/static/preload.js b/app/src/static/preload.js index 91001c5..d4de189 100644 --- a/app/src/static/preload.js +++ b/app/src/static/preload.js @@ -2,8 +2,12 @@ Preload file that will be executed in the renderer process */ import electron from 'electron'; +import path from 'path'; +import fs from 'fs'; const {ipcRenderer, webFrame} = electron; +const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js'); + setNotificationCallback((title, opt) => { ipcRenderer.send('notification', title, opt); }); @@ -26,6 +30,7 @@ document.addEventListener('DOMContentLoaded', event => { ipcRenderer.send('contextMenuOpened', targetHref); }, false); + injectScripts(); }); ipcRenderer.on('params', (event, message) => { @@ -62,3 +67,11 @@ function clickSelector(element) { const mouseEvent = new MouseEvent('click'); element.dispatchEvent(mouseEvent); } + +function injectScripts() { + const needToInject = fs.existsSync(INJECT_JS_PATH); + if (!needToInject) { + return; + } + require(INJECT_JS_PATH); +} diff --git a/package.json b/package.json index c5340dd..e5ca7e8 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 && 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 && 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 79ec1d0..7c50271 100644 --- a/src/build/buildApp.js +++ b/src/build/buildApp.js @@ -24,7 +24,30 @@ function buildApp(src, dest, options, callback) { } fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs)); - changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl); + + maybeCopyScripts(options.inject, dest, error => { + if (error) { + callback(error); + return; + } + + changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl); + callback(); + }); + }); +} + +function maybeCopyScripts(src, dest, callback) { + if (!fs.existsSync(src)) { + callback(); + return; + } + + copy(src, path.join(dest, 'inject', 'inject.js'), error => { + if (error) { + callback(`Error Copying injection files: ${error}`); + return; + } callback(); }); } diff --git a/src/cli.js b/src/cli.js index ca82e5a..60d0c4c 100755 --- a/src/cli.js +++ b/src/cli.js @@ -31,6 +31,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') .parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/src/options/optionsMain.js b/src/options/optionsMain.js index 91d0d70..43d7068 100644 --- a/src/options/optionsMain.js +++ b/src/options/optionsMain.js @@ -48,7 +48,8 @@ function optionsFactory(inpOptions, callback) { userAgent: inpOptions.userAgent || getFakeUserAgent(), ignoreCertificate: inpOptions.ignoreCertificate || false, insecure: inpOptions.insecure || false, - flashPluginDir: inpOptions.flash || null + flashPluginDir: inpOptions.flash || null, + inject: inpOptions.inject || null }; if (inpOptions.honest) { diff --git a/test-resources/test-injection.js b/test-resources/test-injection.js new file mode 100644 index 0000000..ec33ddf --- /dev/null +++ b/test-resources/test-injection.js @@ -0,0 +1 @@ +console.log('This is a test injecton script');