This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
121 lines (104 loc) · 3.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var system = require('system'),
args = system.args,
fs = require('fs'),
dirWork;
if (args && args.length >= 2) {
dirWork = args[1];
} else {
dirWork = fs.workingDirectory;
}
phantom.casperPath = dirWork + '/node_modules/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');
var casper = require('casper').create({
pageSettings: {
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false // use these settings
},
logLevel: "warning", // Only "info" level messages will be logged
verbose: true // log messages will be printed out to the console
}),
$ = require('./node_modules/jquery/dist/jquery.js');
// make temporary directories
var tempDir = '.ns-exports',
ssDir = tempDir + '/ss',
metaDir = tempDir + '/meta',
configFile = tempDir+'/nsconfig.json';
if (!fs.exists(configFile)) {
console.log('Cannot load Netsuite Auth configuration. See http://github.com/suiteplus/nsconfig');
}
var config = JSON.parse(fs.read(configFile, 'utf-8'));
if (fs.exists(tempDir)) fs.removeTree(tempDir);
[tempDir, ssDir, metaDir].forEach(function(dir) {
if (!fs.exists(dir)) fs.makeDirectory(dir);
});
var realm = config.realm || 'netsuite.com',
base = config.hostname || 'system.' + realm,
sysURL = 'https://' + base;
casper.start();
var records = require('./lib/manager-queues')(casper, config, dirWork);
console.log('############################################');
console.log('========>>> ns-export starting <<<==========');
console.log('--------------------------------------------');
console.log('account: ' + config.account);
console.log('user: ' + config.email);
console.log('hostname: ' + sysURL);
if (config.bundle) {
console.log('bundle: ' + config.bundle);
}
console.log('############################################');
casper.open(sysURL + '/app/login/nllogin.nl', {
method: 'post',
data: {
'email' : config.email,
'password' : config.password,
'jsenabled': 'T'
}
}).then( function () {
this.viewport(1280, 1024);
var ssQuestion = ssDir + '/question.jpg';
this.capture(ssQuestion);
var html = this.getHTML().toString().toLowerCase();
var quiz = config.quiz,
has = false;
for (var i = 0; i < quiz.length; i++) {
var q = quiz[i];
//console.log('quiz', i, q.question, q.answer, !!~html.indexOf(q.question));
if (html.indexOf(q.question) >= 0) {
this.fillXPath('form', {'//input[@name="answer"]': q.answer}, true);
has = true;
}
}
if (!has) {
casper.log('Question not found', 'error');
casper.log('See the question in: ' + ssQuestion, 'error');
casper.exit();
}
casper.wait(15000, function () {
this.capture(ssDir + '/afterLogin.jpg');
var html = this.getHTML().toString(),
$html = $(html),
$rows = $html.find('tr.uir-list-row-tr');
for (var r = 0; r < $rows.length; r++) {
var $row = $($rows[r]),
rowTd = $row.find('td'),
rowText = rowTd.filter('.listtext'),
account = rowText.filter(':eq(1)').text();
if (~account.indexOf(config.account)) {
var tt = account.split('-'),
acc = tt[tt.length - 1].trim();
this.click('tr.uir-list-row-tr a[href$=' + acc + ']');
break;
}
}
casper.wait(1000, function () {
this.capture(ssDir + '/home.jpg');
records.loadAll({
url: sysURL,
config: config,
dir: dirWork
});
});
});
});
casper.run();