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             // see if getting rid of 'only' from screen works.
48             if(ar[i].cssText.match(/only\s+screen/)) {
49                 var str = ar[i].cssText.replace(/only\s+screen/, '');
50                 newsheet += str +"\n";
51                 continue;   
52             }
53             if(ar[i].cssText.match(/screen\s+and/)) {
54                 var str = ar[i].cssText.replace(/screen\s+and/, '');
55                 newsheet += str +"\n";
56                 continue;   
57             }
58             newsheet += ar[i].cssText +"\n";
59                 
60             
61             
62             
63         }
64         
65         
66         if (node.nodeName == 'STYLE' ) {
67             node.innerHTML = newsheet;
68             continue;
69         }
70         var newnode = document.createElement('STYLE');
71         newnode.innerHTML = newsheet;
72         node.parentNode.replaceChild(newnode,node);
73         
74     
75     }
76     for(i = 0; i < del.length;i++) {
77         del[i].parentNode.removeChild(del[i]);
78     }
79
80     
81     
82 })();