inliner.js
[app.webkitpdf] / inliner.js
1
2
3
4 (function(){
5     
6     var camelize = function(a,b){
7         return b.toUpperCase();
8     }
9     
10      
11     
12     var fillStyle = function(dom)
13     {
14         if (!dom || dom.nodeType != 1) {
15             console.log("SKIP: " + dom.nodeName);
16             return;
17         }
18         console.log(dom.nodeName);
19         
20         var style = window.getComputedStyle(dom, null);
21         if (style.display == 'none' || dom.nodeName == "NOSCRIPT" || dom.nodeName == "SCRIPT" ) {
22             dom.parentElement.removeChild(dom);
23             return;
24         }
25         var cn = [];
26         
27         if (dom.childNodes.length > 100) {
28             console.log(dom);
29             throw "too many child nodes?" + dom.childNodes.length ;
30         }
31         for (var i = 0;i < dom.childNodes.length;i++) {
32             cn.push(dom.childNodes[i]);
33         }
34         
35         if (cn.length > 100) {
36             console.log(dom);
37             throw "too many child nodes? cn";
38         }
39         for (var i = 0;i < cn.length;i++) {
40             
41             console.log( i + ':'+ cn[i].nodeName);
42             fillStyle(cn[i]);
43         }    
44         
45         return;
46         
47         
48         //var pstyle = false;
49         //if (dom.nodeName != 'BODY') {
50         //    pstyle = window.getComputedStyle(dom.parentElement, null);
51         //}
52          
53         
54         
55         //if (dom.nodeName == 'LI') {            throw 'done';        }
56         
57         
58         for(var i=0;i<style.length;i++){
59             var prop = style[i];
60             var camel = prop.replace(/\-([a-z])/g, camelize);
61             var val = style.getPropertyValue(prop);
62             //returns[camel] = val;
63             
64             // idea... if the parent has the same style.. then do not apply it to the child?
65             //if (pstyle && pstyle[prop] == val) {
66             //    continue;
67             //}
68             console.log(prop + '=' + val);
69             
70             dom.style[camel] = val;
71          
72             
73         }
74         var es = dom.getAttribute('style');
75         console.log(dom.nodeName + '::' + es);
76         dom.setAttribute('style', es);
77         
78         //return returns;
79     }
80     
81     function changeit() {
82         
83         
84         var el = document.querySelector('body');
85         fillStyle(el);
86         
87         var matches = document.getElementsByTagName('style');
88         for(var i =0;i < matches.length;i++) {
89             matches[i].parentElement.removeChild(matches[i]);
90         }
91         var matches = document.getElementsByTagName('link');
92         for(var i =0;i < matches.length;i++) {
93         //    matches[i].parentElement.removeChild(matches[i]);
94         }
95     }
96     changeit();
97     
98 })();