990dd0ffcbbc918be39e81fc1217da0edf59fd03
[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         
24         var ar = document.styleSheets[ss].rules;
25         if (!ar) {
26             del.push(node);
27             continue;
28         }
29         
30         var newsheet = '';
31         
32         
33         for(i=0; i < ar.length; i++){
34             if(ar[i].cssText.indexOf("@media") < 0 ) {
35                 newsheet += ar[i].cssText +"\n";
36                 continue;
37             }
38             //console.log(ar[i].cssText);
39             if(ar[i].cssText.match(/@media\s+print/)) {
40                 continue;
41             }
42             
43             // see if getting rid of 'only' from screen works.
44             if(!ar[i].cssText.match(/only\s+screen/)) {
45                 newsheet += ar[i].cssText +"\n";
46                 continue;   
47             }
48             var str = ar[i].cssText.replace(/only\s+screen/, 'screen');
49             newsheet += str +"\n";
50             
51             
52         }
53         
54         
55         if (node.nodeName == 'STYLE' ) {
56             node.innerHTML = newsheet;
57             continue;
58         }
59         var newnode = document.createElement('STYLE');
60         newnode.innerHTML = newsheet;
61         node.parentNode.replaceChild(newnode,node);
62         
63     
64     }
65     for(i = 0; i < del.length;i++) {
66         del[i].parentNode.removeChild(del[i]);
67     }
68
69     
70     
71 })();