inject.js
[app.wkmirror] / inject.js
1 //file which parses document body
2
3 // is prototype insane?
4 try {
5     delete Array.prototype.toJSON;
6     delete Object.prototype.toJSON;
7
8     delete Hash.prototype.toJSON;
9     delete String.prototype.toJSON;
10 } catch (e) {
11     
12 }
13
14 BrowserMirror = {
15     gatherlinks : function () { 
16         var urls= [];
17         for (var i= document.links.length; i-->0;){
18             urls.push( {
19                 href: document.links[i].href,
20                 label : document.links[i].textContent
21             });
22             
23         }
24         console.log(JSON.stringify( {
25             requesturl : document.location.href, // buggy - we should return the requested path.
26             method: 'gatherlinks' ,
27             data : urls
28         }));
29           
30         //return urls;
31         
32     },
33     downloadhead : function (url) {
34         var xmlhttp = new XMLHttpRequest();
35                     
36         
37         xmlhttp.open("HEAD", url,true);
38         xmlhttp.onreadystatechange=function() {
39             if (xmlhttp.readyState==4) {
40                 console.log(
41                     JSON.stringify ({
42                         requesturl : url,
43                         method: 'downloadhead',
44                         headers : xmlhttp.getAllResponseHeaders(),
45                         contentType:xmlhttp.getResponseHeader("Content-Type"),
46                         data:''
47                     }));
48             }
49         }
50         
51         xmlhttp.send(null);
52            
53     },
54
55     downloadpage : function (url) {
56         
57        
58         var xmlhttp = new XMLHttpRequest();
59                     
60         xhr.responseType = 'arraybuffer';
61         xmlhttp.open("GET", url,true);
62         xmlhttp.onreadystatechange=function() {
63             if (xmlhttp.readyState==4) {
64                 
65                 
66                 console.log(
67                     JSON.stringify ({
68                         requesturl : url,
69                         method: 'downloadpage',
70                         headers : xmlhttp.getAllResponseHeaders(),
71                         contentType:xmlhttp.getResponseHeader("Content-Type"),
72                         data: new Uint8Array(xmlhttp.response);
73                     }));
74             }
75         }
76         
77         xmlhttp.send(null);
78            
79     }
80     string2byteArray : function(str){
81         var ba= new air.ByteArray();
82         for(var i=0;i<str.length; i++){
83             ba.writeByte(str.charCodeAt(i));
84         }
85         return ba;
86     }
87 }
88
89         
90
91
92
93 //downloadpage();
94 //gatherlinks();