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             console.log( i + ':'+ cn[i].nodeName);
41             cn.push(cn[i]);
42         }    
43         
44         return;
45         
46         
47         //var pstyle = false;
48         //if (dom.nodeName != 'BODY') {
49         //    pstyle = window.getComputedStyle(dom.parentElement, null);
50         //}
51          
52         
53         
54         //if (dom.nodeName == 'LI') {            throw 'done';        }
55         
56         
57         for(var i=0;i<style.length;i++){
58             var prop = style[i];
59             var camel = prop.replace(/\-([a-z])/g, camelize);
60             var val = style.getPropertyValue(prop);
61             //returns[camel] = val;
62             
63             // idea... if the parent has the same style.. then do not apply it to the child?
64             //if (pstyle && pstyle[prop] == val) {
65             //    continue;
66             //}
67             console.log(prop + '=' + val);
68             
69             dom.style[camel] = val;
70          
71             
72         }
73         var es = dom.getAttribute('style');
74         console.log(dom.nodeName + '::' + es);
75         dom.setAttribute('style', es);
76         
77         //return returns;
78     }
79     
80     function changeit() {
81         
82         
83         var el = document.querySelector('body');
84         fillStyle(el);
85         
86         var matches = document.getElementsByTagName('style');
87         for(var i =0;i < matches.length;i++) {
88             matches[i].parentElement.removeChild(matches[i]);
89         }
90         var matches = document.getElementsByTagName('link');
91         for(var i =0;i < matches.length;i++) {
92         //    matches[i].parentElement.removeChild(matches[i]);
93         }
94     }
95     changeit();
96     
97 })();