mirror of https://github.com/jiahaog/Nativefier
Implement injection of `.js`
This commit is contained in:
parent
4351906a42
commit
4d49c01ff3
|
@ -2,8 +2,12 @@
|
||||||
Preload file that will be executed in the renderer process
|
Preload file that will be executed in the renderer process
|
||||||
*/
|
*/
|
||||||
import electron from 'electron';
|
import electron from 'electron';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
const {ipcRenderer, webFrame} = electron;
|
const {ipcRenderer, webFrame} = electron;
|
||||||
|
|
||||||
|
const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js');
|
||||||
|
|
||||||
setNotificationCallback((title, opt) => {
|
setNotificationCallback((title, opt) => {
|
||||||
ipcRenderer.send('notification', title, opt);
|
ipcRenderer.send('notification', title, opt);
|
||||||
});
|
});
|
||||||
|
@ -26,6 +30,7 @@ document.addEventListener('DOMContentLoaded', event => {
|
||||||
ipcRenderer.send('contextMenuOpened', targetHref);
|
ipcRenderer.send('contextMenuOpened', targetHref);
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
|
injectScripts();
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcRenderer.on('params', (event, message) => {
|
ipcRenderer.on('params', (event, message) => {
|
||||||
|
@ -62,3 +67,11 @@ function clickSelector(element) {
|
||||||
const mouseEvent = new MouseEvent('click');
|
const mouseEvent = new MouseEvent('click');
|
||||||
element.dispatchEvent(mouseEvent);
|
element.dispatchEvent(mouseEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function injectScripts() {
|
||||||
|
const needToInject = fs.existsSync(INJECT_JS_PATH);
|
||||||
|
if (!needToInject) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
require(INJECT_JS_PATH);
|
||||||
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
"clean": "gulp clean",
|
"clean": "gulp clean",
|
||||||
"build": "gulp build",
|
"build": "gulp build",
|
||||||
"watch": "while true ; do gulp watch ; done",
|
"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",
|
"start-placeholder": "npm run build && electron app",
|
||||||
"release": "gulp release"
|
"release": "gulp release"
|
||||||
},
|
},
|
||||||
|
|
|
@ -24,7 +24,30 @@ function buildApp(src, dest, options, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
|
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();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ if (require.main === module) {
|
||||||
.option('--ignore-certificate', 'ignore certificate related errors')
|
.option('--ignore-certificate', 'ignore certificate related errors')
|
||||||
.option('--insecure', 'enable loading of insecure content, defaults to false')
|
.option('--insecure', 'enable loading of insecure content, defaults to false')
|
||||||
.option('--flash <value>', 'path to Chrome flash plugin, find it in `Chrome://plugins`')
|
.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);
|
.parse(process.argv);
|
||||||
|
|
||||||
if (!process.argv.slice(2).length) {
|
if (!process.argv.slice(2).length) {
|
||||||
|
|
|
@ -48,7 +48,8 @@ function optionsFactory(inpOptions, callback) {
|
||||||
userAgent: inpOptions.userAgent || getFakeUserAgent(),
|
userAgent: inpOptions.userAgent || getFakeUserAgent(),
|
||||||
ignoreCertificate: inpOptions.ignoreCertificate || false,
|
ignoreCertificate: inpOptions.ignoreCertificate || false,
|
||||||
insecure: inpOptions.insecure || false,
|
insecure: inpOptions.insecure || false,
|
||||||
flashPluginDir: inpOptions.flash || null
|
flashPluginDir: inpOptions.flash || null,
|
||||||
|
inject: inpOptions.inject || null
|
||||||
};
|
};
|
||||||
|
|
||||||
if (inpOptions.honest) {
|
if (inpOptions.honest) {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
console.log('This is a test injecton script');
|
Loading…
Reference in New Issue