Implement injection of `.js`

This commit is contained in:
Jia Hao 2016-02-25 14:11:48 +08:00
parent 4351906a42
commit 4d49c01ff3
7 changed files with 42 additions and 3 deletions

0
app/inject/_placeholder Normal file
View File

View File

@ -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);
}

View File

@ -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"
},

View File

@ -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();
});
}

View File

@ -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 <value>', 'path to Chrome flash plugin, find it in `Chrome://plugins`')
.option('--inject <value>', 'path to a javascript file to be injected')
.parse(process.argv);
if (!process.argv.slice(2).length) {

View File

@ -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) {

View File

@ -0,0 +1 @@
console.log('This is a test injecton script');