nsinject.js
[app.wkmirror] / nsinject.js
1 NS = {
2     timeout : 20,
3     
4     rungrab : function(id)
5     {
6         //id = 282;
7         this.id = id;
8         var timeout = 10;
9         var frameload = function()
10         {
11              
12             
13             
14             
15         }
16         console.log("document.location" + document.location);
17         
18         if (!(window.location + '').match(/netsuite/)) {
19             // try
20             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=" + id;
21             return;
22         }
23         // do stuff..
24         // we really just want to send a series of download requests..
25         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
26         
27         
28         
29         var chk = document.getElementById('lines');
30         console.log("lines element?");
31         console.log(chk)
32         console.log('checking form values');
33         
34         
35         if (chk.value !='T') {
36             console.log('SETTING VALUES:chk');
37             chk.value= 'T';
38             console.log('SETTING VALUES:daterange');
39             document.forms[0].daterange.value = 'CUSTOM';
40             console.log('SETTING VALUES:date1');
41             document.getElementById('date1').value = '1/1/2008';
42             console.log('SETTING VALUES:date2');
43             document.getElementById('date2').value = '1/1/2013';
44             console.log('SUBMIT');
45             document.forms[0].submit();
46             
47             return;
48         }
49         
50         console.log('form values - ok - checking if empty');
51         
52          if (   reportTable.oContentProvider.bRequestPending
53                 || !reportTable.isInLoadedState()) {
54                 console.log('report not ready?');
55                 NS.timeout--;
56                 if (NS.timeout < 0) {
57                     console.log('SENDING EMTPY..?');
58                     console.log(
59                          JSON.stringify ({
60                             requesturl : '',
61                             method: 'nsdownloadpage',
62                             filename : NS.id + '.csv',
63                             headers : '',
64                             contentType: '',
65                             data: []
66                         }));
67                     return;
68                     
69                     
70                 }
71                 
72                 window.setTimeout(NS.rungrab,1000);
73                 
74                 return;
75                 
76             }
77         console.log('no empty - trying download');
78             
79         var ar = document.forms[0].elements;
80         var data= {};
81         for(var i =0; i < ar.length; i++) {
82             var e = ar[i];
83             data[e.name] = e.value;
84         }
85         console.log(JSON.stringify(data));
86         console.log(reportTable.getTotalRowCount());
87          NS.downloaddata(
88             'https://system.netsuite.com/app/reporting/reportrunner.nl',
89             {
90                 id: data.id,
91                 reportaction: 'exportcsv',
92                 apptype: 'html',
93                 visibleranges: '0,' +  reportTable.getTotalRowCount()
94             }
95             
96         );
97         
98         
99         
100         return;
101         
102         
103         
104         var ifr = document.createElement('iframe');
105         
106         ifr.setAttribute('src','https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=' + id);
107         ifr.setAttribute('width','900');
108         ifr.setAttribute('height','700');
109         ifr.addEventListener('load', frameload);
110         document.body.innerHTML = '';
111         
112         document.body.appendChild(ifr);
113         
114         /*
115         var ifd = ifr.contentWindow.document;
116         ifd.getElementById('lines_chkbox').
117         
118         var ar = ifd.getElementById('footerform').elements;
119         for(var i =0; i < ar.length; i++) {
120             var e = ar[i];
121             data[e.name] = e.value;
122         }
123         console.log(data);
124         
125         data.lines_chkbox = 'T';
126         data.reportaction = 'exportcsv'
127         //data.visibleranges =
128         data.apptype = 'html';
129         this.downloaddata(
130             'https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403',
131             data
132         )
133         */
134         
135         
136     },
137     downloaddata : function (url,data) {
138         
139        
140         var xmlhttp = new XMLHttpRequest();
141                     
142         xmlhttp.open("POST", url,true);
143         xmlhttp.responseType = 'arraybuffer';
144         xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
145         var kv = [];
146         for(var i in data) {
147             kv.push(i +'=' + encodeURIComponent(  data[i]));
148         }
149         
150
151         xmlhttp.onload=function() {
152            
153             var ar= [];
154              
155             var r = new Uint8Array(xmlhttp.response);
156             for (i=0;i<r.byteLength;i++) {
157                 ar.push(r[i]);
158             }
159             
160             console.log(
161                 JSON.stringify ({
162                     requesturl : url,
163                     method: 'nsdownloadpage',
164                     filename : NS.id + '.csv',
165                     headers : xmlhttp.getAllResponseHeaders(),
166                     contentType:xmlhttp.getResponseHeader("Content-Type"),
167                     data: ar
168                 }));
169            
170         }
171         
172         xmlhttp.send(kv.join('&'));
173            
174     },
175     login : function(u,p,id)
176     {
177         
178         try {
179             document.forms[0].email.value = u;
180             document.forms[0].password.value = p;
181             
182             
183             
184             document.forms[0].elements['jsenabled'].value = 'T';
185             setLoginCookie(document.forms[0].elements['rememberme'].checked);
186             
187             document.forms[0].submit();
188             
189             
190             
191         } catch( e) {
192             this.rungrab(id);
193             
194         }
195     }
196     
197     
198     
199 }