domains/remove_print_css.js
[app.webkitpdf] / domains / remove_print_css.js
1
2 (function (args) {
3     
4     var i;
5     var del = [];
6     for (var ss = 0; ss < document.styleSheets.length; ss++) {
7         var css =document.styleSheets[ss];
8         console.log("sheet " + ss + '/' + document.styleSheets.length + ": " + css.ownerNode.outerHTML) ;
9         
10         var node = css.ownerNode;
11         
12         if (node.nodeName == 'STYLE' && node.getAttribute('media') && node.getAttribute('media').match(/print/)) {
13             node.innerHTML = '';
14             continue;
15         }
16         if (node.nodeName == 'LINK' && node.getAttribute('media') && node.getAttribute('media').match(/print/)) {
17             node.setAttribute('href' , '');
18             node.setAttribute('media' , 'speech');
19             continue;
20         }
21         
22         // now we are dealing with non-print styles...
23         try {
24             var ar = document.styleSheets[ss].rules;
25         } catch(e) {
26             console.log("could not access stylesheet:" + ss);
27             continue;
28         }
29         if (!ar) {
30             del.push(node);
31             continue;
32         }
33         
34         var newsheet = '';
35         
36         
37         for(i=0; i < ar.length; i++){
38             if(ar[i].cssText.indexOf("@media") < 0 ) {
39                 newsheet += ar[i].cssText +"\n";
40                 continue;
41             }
42             //console.log(ar[i].cssText);
43             if(ar[i].cssText.match(/@media\s+print/)) {
44                 continue;
45             }
46             
47             
48             // see if getting rid of 'only' from screen works.
49             if(ar[i].cssText.match(/only\s+screen/)) {
50                 var str = ar[i].cssText.replace(/only\s+screen/, '');
51                 newsheet += str +"\n";
52                 continue;   
53             }
54             if(ar[i].cssText.match(/screen\s+and/)) {
55                 var str = ar[i].cssText.replace(/screen\s+and/, '');
56                 newsheet += str +"\n";
57                 continue;   
58             }
59             newsheet += ar[i].cssText +"\n";
60                 
61             
62             
63             
64         }
65         
66         
67         if (node.nodeName == 'STYLE' ) {
68             node.innerHTML = newsheet;
69             continue;
70         }
71         var newnode = document.createElement('STYLE');
72         newnode.innerHTML = newsheet;
73         node.parentNode.replaceChild(newnode,node);
74         
75     
76     }
77     for(i = 0; i < del.length;i++) {
78         del[i].parentNode.removeChild(del[i]);
79     }
80
81     
82     
83 })();