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