1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-25 07:37:46 +02:00
keeweb/app/scripts/comp/auto-type/helper/auto-type-helper-darwin.js

75 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-04-09 20:58:22 +02:00
'use strict';
var Launcher = require('../../launcher');
var spawn = Launcher.req('child_process').spawn;
2016-04-09 22:19:56 +02:00
var ForeMostAppScript = 'tell application "System Events" to set frontApp to name of first process whose frontmost is true';
var ChromeScript = 'tell application "{}" to set appUrl to URL of active tab of front window\n' +
'tell application "{}" to set appTitle to title of active tab of front window\n' +
'return appUrl & "\n" & appTitle';
var SafariScript = 'tell application "{}" to set appUrl to URL of front document\n' +
'tell application "{}" to set appTitle to name of front document\n' +
'return appUrl & "\n" & appTitle';
var OtherAppsScript = 'tell application "System Events"\n' +
' tell process "{}"\n' +
' tell (1st window whose value of attribute "AXMain" is true)\n' +
' set windowTitle to value of attribute "AXTitle"\n' +
' end tell\n' +
' end tell\n' +
'end tell';
2016-04-09 20:58:22 +02:00
var AutoTypeHelper = function() {
};
AutoTypeHelper.prototype.getActiveWindowTitle = function(callback) {
2016-04-09 22:19:56 +02:00
AutoTypeHelper.exec(ForeMostAppScript, function(err, out) {
if (err) { return callback(err); }
var appName = out.trim();
2016-04-10 08:36:09 +02:00
// getting urls and titles from Chrome or Safari:
// - will suit in 90% cases
// - does not require assistive access
// - allows to get url
2016-04-09 22:19:56 +02:00
if (['Google Chrome', 'Chromium', 'Google Chrome Canary'].indexOf(appName) >= 0) {
AutoTypeHelper.exec(ChromeScript.replace(/\{}/g, appName), function(err, out) {
if (err) { return callback(err); }
var parts = out.split('\n');
return callback(null, parts[1].trim(), parts[0].trim());
});
} else if (['Safari', 'Webkit'].indexOf(appName) >= 0) {
AutoTypeHelper.exec(SafariScript.replace(/\{}/g, appName), function(err, out) {
if (err) { return callback(err); }
var parts = out.split('\n');
return callback(null, parts[1].trim(), parts[0].trim());
});
} else {
2016-04-10 08:36:09 +02:00
// special cases are not available. this method may ask the user about assistive access
2016-04-09 22:19:56 +02:00
AutoTypeHelper.exec(OtherAppsScript.replace(/\{}/g, appName), function(err, out) {
if (err) { return callback(err); }
return callback(null, out.trim());
});
}
});
};
2016-04-09 20:58:22 +02:00
2016-04-09 22:19:56 +02:00
AutoTypeHelper.exec = function(script, callback) {
2016-04-09 20:58:22 +02:00
var ps = spawn('osascript', ['-e', script]);
var stderr = '';
var stdout = '';
ps.stdout.on('data', function(data) {
stdout += data.toString();
});
ps.stderr.on('data', function(data) {
stderr += data.toString();
});
ps.on('close', function(code) {
if (code) {
callback('Exit code ' + code + ': ' + stderr + (stdout ? '\nout: ' + stdout : ''));
} else {
2016-04-09 22:19:56 +02:00
callback(null, stdout);
2016-04-09 20:58:22 +02:00
}
});
};
module.exports = AutoTypeHelper;