Project

General

Profile

Bug #2763 ยป getCssRule.js

Marco Kinski, 03/06/2014 06:12 PM

 
1
// slightly modified, based on: http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
2
this.getCssRule = function(selector, deleteFlag) {
3
//  selector = selector.toLowerCase(); // only used from within Wt, so case is allways exact matching
4
  deleteFlag = deleteFlag == 'delete';
5

    
6
   if (document.styleSheets) {
7
      for (var i = 0; i < document.styleSheets.length; i++) {
8
         var styleSheet = document.styleSheets[i];
9

    
10
         try {
11
            var rules = styleSheet.cssRules;
12
            if (!rules) rules = styleSheet.rules;
13

    
14
            // the items we are looking for are mostly at the ende of the stylesheet
15
            for (var ii = rules.length -1; ii > -1; ii--) {
16
               var rule = rules[ii];
17
               if (rule && rule.selectorText) {
18
//                if (rule.selectorText.toLowerCase() == selector) { // only used from within Wt, so case is allways exact matching
19
                  if (rule.selectorText == selector) {
20
                     if (deleteFlag) {
21
                        if (styleSheet.deleteRule) styleSheet.deleteRule(ii);
22
                        else styleSheet.removeRule(ii);
23
                        return true;
24
                     }
25
                     return rule;
26
                  }
27
               }
28
            }
29
         }
30
         catch (err) {
31
            // firefox security error 1000 when access a stylesheet.cssRules hosted
32
            // from another domain
33
         }
34
      }
35
   }
36

    
37
  return false;
38
};
    (1-1/1)