keeweb/app/scripts/auto-type/index.js

286 lines
9.9 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import Backbone from 'backbone';
import { AutoTypeFilter } from 'auto-type/auto-type-filter';
import { AutoTypeHelperFactory } from 'auto-type/auto-type-helper-factory';
import { AutoTypeParser } from 'auto-type/auto-type-parser';
import { Launcher } from 'comp/launcher';
import { Alerts } from 'comp/ui/alerts';
import { Timeouts } from 'const/timeouts';
import { AppSettingsModel } from 'models/app-settings-model';
import { Locale } from 'util/locale';
import { Logger } from 'util/logger';
import { AutoTypeSelectView } from 'views/auto-type/auto-type-select-view';
2017-01-31 07:50:28 +01:00
const logger = new Logger('auto-type');
const clearTextAutoTypeLog = localStorage.autoTypeDebug;
const AutoType = {
2016-04-09 20:58:22 +02:00
helper: AutoTypeHelperFactory.create(),
enabled: !!(Launcher && Launcher.autoTypeSupported),
2016-07-24 22:57:12 +02:00
selectEntryView: false,
2016-08-07 19:34:31 +02:00
pendingEvent: null,
2016-08-18 22:01:08 +02:00
running: false,
2016-04-23 16:50:40 +02:00
2016-08-07 19:34:31 +02:00
init(appModel) {
2016-07-24 22:57:12 +02:00
if (!this.enabled) {
return;
}
this.appModel = appModel;
2016-08-07 19:34:31 +02:00
Backbone.on('auto-type', this.handleEvent, this);
Backbone.on('main-window-blur main-window-will-close', this.resetPendingEvent, this);
2016-07-24 19:11:25 +02:00
},
2016-08-07 19:34:31 +02:00
handleEvent(e) {
2019-08-16 23:05:39 +02:00
const entry = (e && e.entry) || null;
2016-07-24 19:11:25 +02:00
logger.debug('Auto type event', entry);
2016-08-18 22:01:08 +02:00
if (this.running) {
logger.debug('Already running, skipping event');
return;
}
2016-07-24 19:11:25 +02:00
if (entry) {
2019-08-16 23:05:39 +02:00
this.hideWindow(() => {
this.runAndHandleResult({ entry });
});
2016-07-24 19:11:25 +02:00
} else {
if (this.selectEntryView) {
return;
}
if (Launcher.isAppFocused()) {
return Alerts.error({
header: Locale.autoTypeError,
body: Locale.autoTypeErrorGlobal,
skipIfAlertDisplayed: true
});
}
this.selectEntryAndRun();
}
},
2018-10-13 11:04:58 +02:00
runAndHandleResult(result) {
this.run(result, err => {
2016-07-24 19:11:25 +02:00
if (err) {
Alerts.error({
header: Locale.autoTypeError,
body: Locale.autoTypeErrorGeneric.replace('{}', err.toString())
});
}
});
2017-04-09 10:31:05 +02:00
if (AppSettingsModel.instance.get('lockOnAutoType')) {
Backbone.trigger('lock-workspace');
}
2016-07-24 19:11:25 +02:00
},
2018-10-13 11:04:58 +02:00
run(result, callback) {
2016-08-18 22:01:08 +02:00
this.running = true;
2019-09-12 19:59:35 +02:00
const sequence = result.sequence || result.entry.getEffectiveAutoTypeSeq();
2016-04-08 17:40:00 +02:00
logger.debug('Start', sequence);
2017-01-31 07:50:28 +01:00
const ts = logger.ts();
2016-04-08 17:40:00 +02:00
try {
2017-01-31 07:50:28 +01:00
const parser = new AutoTypeParser(sequence);
const runner = parser.parse();
2016-07-17 13:30:38 +02:00
logger.debug('Parsed', this.printOps(runner.ops));
2018-10-13 11:04:58 +02:00
runner.resolve(result.entry, err => {
2016-04-08 17:40:00 +02:00
if (err) {
2016-08-18 22:01:08 +02:00
this.running = false;
2016-04-08 23:14:09 +02:00
logger.error('Resolve error', err);
2016-04-23 17:05:33 +02:00
return callback && callback(err);
2016-04-08 17:40:00 +02:00
}
2016-07-17 13:30:38 +02:00
logger.debug('Resolved', this.printOps(runner.ops));
2018-10-13 11:04:58 +02:00
if (result.entry.autoTypeObfuscation) {
2016-04-09 14:55:27 +02:00
try {
runner.obfuscate();
} catch (e) {
2016-08-18 22:01:08 +02:00
this.running = false;
2016-04-09 14:55:27 +02:00
logger.error('Obfuscate error', e);
2016-04-23 17:05:33 +02:00
return callback && callback(e);
2016-04-09 14:55:27 +02:00
}
2016-04-10 09:31:08 +02:00
logger.debug('Obfuscated');
2016-04-09 10:41:52 +02:00
}
2016-07-17 13:30:38 +02:00
runner.run(err => {
2016-08-18 22:01:08 +02:00
this.running = false;
2016-04-08 17:40:00 +02:00
if (err) {
logger.error('Run error', err);
2016-04-23 17:05:33 +02:00
return callback && callback(err);
2016-04-08 17:40:00 +02:00
}
2016-04-23 17:05:33 +02:00
logger.debug('Complete', logger.ts(ts));
return callback && callback();
2016-04-08 17:40:00 +02:00
});
});
} catch (ex) {
2016-08-18 22:01:08 +02:00
this.running = false;
2016-04-08 17:40:00 +02:00
logger.error('Parse error', ex);
2016-04-23 17:05:33 +02:00
return callback && callback(ex);
2016-04-08 17:40:00 +02:00
}
2016-04-09 11:17:01 +02:00
},
2016-08-07 19:34:31 +02:00
validate(entry, sequence, callback) {
2016-04-23 16:50:40 +02:00
try {
2017-01-31 07:50:28 +01:00
const parser = new AutoTypeParser(sequence);
const runner = parser.parse();
2016-04-23 16:50:40 +02:00
runner.resolve(entry, callback);
} catch (ex) {
return callback(ex);
}
},
2016-08-07 19:34:31 +02:00
printOps(ops) {
2016-04-09 11:17:01 +02:00
return '[' + ops.map(this.printOp, this).join(',') + ']';
},
2016-08-07 19:34:31 +02:00
printOp(op) {
2017-01-31 07:50:28 +01:00
const mod = op.mod ? Object.keys(op.mod).join('') : '';
2016-04-09 11:17:01 +02:00
if (op.type === 'group') {
return mod + this.printOps(op.value);
}
if (op.type === 'text') {
2017-01-31 07:50:28 +01:00
let value = op.value;
2016-04-09 11:28:14 +02:00
if (!clearTextAutoTypeLog) {
value = value.replace(/./g, '*');
}
return mod + value;
2016-04-09 11:17:01 +02:00
}
return mod + op.type + ':' + op.value;
2016-04-09 20:58:22 +02:00
},
2016-08-07 19:34:31 +02:00
hideWindow(callback) {
2016-04-09 20:58:22 +02:00
logger.debug('Hide window');
2016-07-24 19:11:25 +02:00
if (Launcher.isAppFocused()) {
Launcher.hideApp();
2016-04-09 20:58:22 +02:00
setTimeout(callback, Timeouts.AutoTypeAfterHide);
} else {
callback();
}
},
getActiveWindowInfo(callback) {
logger.debug('Getting window info');
return this.helper.getActiveWindowInfo((err, windowInfo) => {
2016-04-09 20:58:22 +02:00
if (err) {
logger.error('Error getting window info', err);
2016-04-09 20:58:22 +02:00
} else {
if (!windowInfo.url) {
// try to find a URL in the title
const urlMatcher = new RegExp(
'https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)'
);
const urlMatches = urlMatcher.exec(windowInfo.title);
windowInfo.url = urlMatches && urlMatches.length > 0 ? urlMatches[0] : null;
}
logger.debug('Window info', windowInfo.id, windowInfo.title, windowInfo.url);
2016-04-09 20:58:22 +02:00
}
return callback(err, windowInfo);
});
},
activeWindowMatches(windowInfo, callback) {
if (!windowInfo || !windowInfo.id) {
logger.debug('Skipped active window check because window id is unknown');
return callback(true);
}
this.getActiveWindowInfo((err, activeWindowInfo) => {
if (!activeWindowInfo) {
logger.debug('Error during active window check, something is wrong', err);
return callback(false);
}
if (activeWindowInfo.id !== windowInfo.id) {
logger.info(
`Active window doesn't match: ID is different. ` +
`Expected ${windowInfo.id}, got ${activeWindowInfo.id}`
);
return callback(false, activeWindowInfo);
}
if (activeWindowInfo.url !== windowInfo.url) {
logger.info(
`Active window doesn't match: url is different. ` +
`Expected "${windowInfo.url}", got "${activeWindowInfo.url}"`
);
return callback(false, activeWindowInfo);
}
logger.info('Active window matches');
callback(true, activeWindowInfo);
2016-07-24 19:11:25 +02:00
});
},
2016-08-07 19:34:31 +02:00
selectEntryAndRun() {
this.getActiveWindowInfo((e, windowInfo) => {
const filter = new AutoTypeFilter(windowInfo, this.appModel);
const evt = { filter, windowInfo };
2016-08-07 19:34:31 +02:00
if (!this.appModel.files.hasOpenFiles()) {
this.pendingEvent = evt;
this.appModel.files.once('update', this.processPendingEvent, this);
logger.debug('auto-type event delayed');
2016-08-18 23:26:35 +02:00
this.focusMainWindow();
2016-08-07 19:34:31 +02:00
} else {
this.processEventWithFilter(evt);
2016-07-24 19:11:25 +02:00
}
2016-08-07 19:34:31 +02:00
});
},
2016-08-18 23:26:35 +02:00
focusMainWindow() {
setTimeout(() => Launcher.showMainWindow(), Timeouts.RedrawInactiveWindow);
},
2016-08-07 19:34:31 +02:00
processEventWithFilter(evt) {
2017-01-31 07:50:28 +01:00
const entries = evt.filter.getEntries();
2019-03-03 12:53:47 +01:00
if (entries.length === 1 && AppSettingsModel.instance.get('directAutotype')) {
this.hideWindow(() => {
2019-01-06 18:33:48 +01:00
this.runAndHandleResult({ entry: entries.at(0) });
});
2016-08-07 19:34:31 +02:00
return;
}
2016-08-18 23:26:35 +02:00
this.focusMainWindow();
2017-05-07 19:45:53 +02:00
evt.filter.ignoreWindowInfo = true;
2016-08-07 19:34:31 +02:00
this.selectEntryView = new AutoTypeSelectView({
model: { filter: evt.filter }
}).render();
this.selectEntryView.on('result', result => {
logger.debug('Entry selected', result);
this.selectEntryView.off('result');
this.selectEntryView.remove();
this.selectEntryView = null;
this.hideWindow(() => {
if (result) {
this.activeWindowMatches(evt.windowInfo, (matches, activeWindowInfo) => {
if (matches) {
this.runAndHandleResult(result);
}
});
2016-07-24 22:57:12 +02:00
}
2016-07-24 19:11:25 +02:00
});
2016-04-09 20:58:22 +02:00
});
2019-03-31 14:30:14 +02:00
this.selectEntryView.on('show-open-files', () => {
this.selectEntryView.hide();
Backbone.trigger('open-file');
2019-08-16 23:05:39 +02:00
Backbone.once(
'closed-open-view',
() => {
this.selectEntryView.show();
this.selectEntryView.setupKeys();
},
this
);
2019-03-31 14:30:14 +02:00
});
2016-08-07 19:34:31 +02:00
},
resetPendingEvent() {
if (this.pendingEvent) {
this.pendingEvent = null;
this.appModel.files.off('update', this.processPendingEvent, this);
logger.debug('auto-type event cancelled');
}
},
processPendingEvent() {
if (!this.pendingEvent) {
return;
}
logger.debug('processing pending auto-type event');
2017-01-31 07:50:28 +01:00
const evt = this.pendingEvent;
2016-08-07 19:34:31 +02:00
this.appModel.files.off('update', this.processPendingEvent, this);
this.pendingEvent = null;
this.processEventWithFilter(evt);
2016-04-08 17:40:00 +02:00
}
};
2019-09-15 14:16:32 +02:00
export { AutoType };