domains/remove_print_css.js
[app.webkitpdf] / inliner.js
1 /**
2  *
3  * could this work by findind all the visible text,
4  * then making them hidden..
5  *
6  * -- make a snapshot of the background..
7  *
8  * -- delete the whole page...
9  * -- show the image as the background.
10  * -- then overlay all the text given their calculated positions..
11  * 
12  *
13  *
14  */
15
16
17 (function(){
18     
19     var camelize = function(a,b){
20         return b.toUpperCase();
21     }
22     
23      
24     
25     var fillStyle = function(dom, pstyle)
26     {
27         if (!dom || dom.nodeType != 1) {
28             //console.log("SKIP: " + dom.nodeName);
29             return;
30         }
31         //console.log(dom.nodeName);
32         
33         var style = window.getComputedStyle(dom, null);
34         if (style.display == 'none' || dom.nodeName == "NOSCRIPT" || dom.nodeName == "SCRIPT" ) {
35             dom.parentElement.removeChild(dom);
36             return;
37         }
38         var cn = [];
39         
40         if (dom.childNodes.length > 100) {
41             console.log(dom);
42             throw "too many child nodes?" + dom.childNodes.length ;
43         }
44         for (var i = 0;i < dom.childNodes.length;i++) {
45             cn.push(dom.childNodes[i]);
46         }
47         
48         if (cn.length > 100) {
49             console.log(dom);
50             throw "too many child nodes? cn";
51         }
52         for (var i = 0;i < cn.length;i++) {
53             
54             //console.log( i + ':'+ cn[i].nodeName);
55             fillStyle(cn[i], style);
56         }    
57               
58         
59         //var pstyle = false;
60         //if (dom.nodeName != 'BODY') {
61         //    pstyle = window.getComputedStyle(dom.parentElement, null);
62         //}
63          
64         if (dom.nodeName == 'SPAM') {
65             pstyle = false; //?? others??
66         }
67         
68         //if (dom.nodeName == 'LI') {            throw 'done';        }
69         
70         
71         for(var i=0;i<style.length;i++){
72             var prop = style[i];
73             var camel = prop.replace(/\-([a-z])/g, camelize);
74             var val = style.getPropertyValue(prop);
75             //returns[camel] = val;
76             
77             // idea... if the parent has the same style.. then do not apply it to the child?
78             if (pstyle && pstyle[prop] == val) {
79                 continue;
80             }
81             //console.log(prop + '=' + val);
82             
83             dom.style[camel] = val;
84          
85             
86         }
87         var es = dom.getAttribute('style');
88         //console.log(dom.nodeName + '::' + es);
89         dom.setAttribute('style', es);
90         
91         //return returns;
92     }
93     
94     function changeit() {
95         
96         
97         var el = document.querySelector('body');
98         fillStyle(el), false;
99         
100         var matches = document.getElementsByTagName('style');
101         var rm = [];
102         for(var i =0;i < matches.length;i++) {
103             rm.push(matches[i]);
104             
105         }
106         var matches = document.getElementsByTagName('link');
107         for(var i =0;i < matches.length;i++) {
108             rm.push(matches[i]);
109         }
110         for(var i =0 ; i < rm.length;i++) {
111             rm[i].parentNode.removeChild(rm[i]);
112         }
113         
114         
115         
116     }
117     changeit();
118     console.log("done");
119     
120 })();