nsinject.js
[app.wkmirror] / nsinject.js
1 NS = {
2     timeout : 10,
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         
31         
32         console.log('report ready?');
33         
34         if (chk.value !='T') {
35             chk.value= 'T';
36             document.forms[0].daterange.value = 'CUSTOM';
37
38             document.getElementById('date1').value = '1/1/2008';
39             document.getElementById('date2').value = '1/1/2013';
40             
41             document.getElementById('footerform').submit();
42             
43             return;
44         }
45         
46         
47         
48          if (   reportTable.oContentProvider.bRequestPending
49                 || !reportTable.isInLoadedState()) {
50                 console.log('report not ready?');
51                 NS.timeout--;
52                 if (NS.timeout < 0) {
53                     console.log('SENDING EMTPY..?');
54                     console.log(
55                          JSON.stringify ({
56                             requesturl : '',
57                             method: 'nsdownloadpage',
58                             filename : NS.id + '.csv',
59                             headers : '',
60                             contentType: '',
61                             data: []
62                         }));
63                     return;
64                     
65                     
66                 }
67                 
68                 window.setTimeout(rungrab,1000);
69                 
70                 return;
71                 
72             }
73         
74         
75         console.log("got rungrab");
76         
77         console.log("TRY downloading!");
78             
79         var ar = document.getElementById('footerform').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(data);
86         console.log(document.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,' +  document.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 }