2011-08-19 41 views
1

我写了一个脚本,改变设计糟糕的页面上的CSS元素。我一直在使用Chrome进行测试,现在它功能齐全。我已经把它在网络上,我现在用这个作为书签文字:小书签不起作用在Firefox

javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://myexamplewebsite.com/files/adjustPage.js';})(); 

它完全在Chrome,但在最新的Firefox或IE浏览器无法正常工作。我查看了Chrome和Firefox的错误控制台,没有人抱怨任何问题。当我将代码放入错误控制台的“代码”字段并进行评估时,Firefox也不会作出反应。

Firefox没有说在底部的小状态栏:“读myexamplewebsite.com”,但没有别的。

这是不是在我上面跑的书签码怪癖,因为我已经放在一个“你好世界”脚本在同一台服务器上,并且它工作得很好。

有没有更好的方式来制定出什么样的Firefox/IE不喜欢我的剧本?

如果你有兴趣,我运行的代码是在这里。请原谅代码的丑陋,这是一个非常快速和肮脏的黑客:

var section = document.getElementsByClassName('cqGroupBox'); 
for(var i in section) { 
var children = section[i].childNodes; 

for(var j in children){ 
    if(children[j].innerText == 'Detected on configuration') { 
     var style = section[i].getAttribute('style'); 
     style += ' display:none;'; 
     section[i].setAttribute('style', style); 
     break; 
    } 
} 
} 

var tables = document.getElementsByTagName('table'); 
for(var i in tables) { 
try{ 
    var styleNode = tables[i].attributes.getNamedItem('style'); 
    var style = styleNode.firstChild.nodeValue; 

    if(style == null) { 
     continue; 
    } 

    if(style.match('top: 434px; left: 120px;') 
    || style.match('top: 461px; left: 120px;') 
    || style.match('top: 434px; left: 456px;') 
    || style.match('top: 461px; left: 456px;')){ 
     style += ' display:none;'; 
     tables[i].attributes.getNamedItem('style').firstChild.nodeValue = style; 
    } 
} catch(err){continue;} 
} 


var labels = document.getElementsByTagName('label'); 
for(var i in labels) { 
try{ 
    var styleNode = labels[i].attributes.getNamedItem('style'); 
    var style = styleNode.firstChild.nodeValue; 

    if(style == null) { 
     continue; 
    } 

    if(labels[i].innerText == 'OS' 
    || labels[i].innerText == 'App. Server' 
    || labels[i].innerText == 'DBMS' 
    || labels[i].innerText == 'Web Server'){ 
     style += ' display:none;'; 
     labels[i].attributes.getNamedItem('style').firstChild.nodeValue = style; 
    } 
} catch(err){continue;} 
} 

var divs = document.getElementsByTagName('div'); 
for(var i in divs) { 
try { 
    var styleNode = divs[i].attributes.getNamedItem('style'); 
    var style = styleNode.firstChild.nodeValue; 

    if(style == null) { 
     continue; 
    } 

    if(style.match('top: 291px; left: 23px;')){ 
     style.replace('height: 109px;',''); 
     divs[i].attributes.getNamedItem('style').firstChild.nodeValue = style; 

     innerDivs = divs[i].getElementsByTagName('div'); 
     for(var j in innerDivs){ 
      try { 
       innerStyle = innerDivs[j].attributes.getNamedItem('style').firstChild.nodeValue; 
       if(innerStyle.match('width: 665px; height: 109px;')){ 
        innerStyle = innerStyle.replace('height: 109px;',''); 
        innerStyle += ' font-size: 130%'; 
        innerDivs[j].attributes.getNamedItem('style').firstChild.nodeValue = innerStyle; 
       } 
      } catch(err){continue;} 
     } 
    } 
} catch(err){continue;} 
} 
+1

似乎不错,但也许它不符合您的检查;)也许这就是计算的风格,而不是那个html plage服务的那个:)我们需要这个脚本应该被触发的页面 – walialu

+0

好点。我看到页面上的尺寸都是绝对的,并且假定它们会被硬编码(页面是_that_ bad)。我无法共享该页面,因为它是一个专有系统(ClearQuest错误跟踪系统),但您对我所能找到的内容有任何建议吗? – HXCaine

回答

3

innerText是一个非标准特性(到目前为止,现在有努力为它创建一个标准),这是不壁虎支持。是否使用textContent为您解决问题?

+0

这确实使我朝着正确的方向前进。当我做出这种改变时,四个循环中的第一个在FF中工作,这很好地表明我正在使用Chrome专用的代码。我会继续研究修复我的脚本有什么不同。你知道有什么简单的方法来看这个吗? – HXCaine

+0

@HXCaine一个好的开始就是看看Firefox中的错误控制台,看看有什么异常会被抛出,如果有的话......当然,如果脚本静静地捕获所有的异常,这将无济于事。另一种方法是在脚本中添加警报,以查看是否可以确定未到达的第一行是什么,然后看看它之前的内容。 –

+0

我已经注意到这个脚本的主要问题是赋值给字段。例如,'tables [i] .attributes.getNamedItem('style')。firstChild.nodeValue = style'不会改变'nodeValue'的值。 我意识到这不是原来的问题,但我不想为同样的问题做一个新的问题。你知道为什么这个任务在Firefox中不起作用吗? – HXCaine

0

这里是固定的代码,即在Chrome,火狐3.6和最新的Firefox(如2011年8月)的作品。

var section = document.getElementsByClassName('cqGroupBox'); 
for(var i in section) { 
var children = section[i].childNodes; 

for(var j in children){ 
    if(children[j].textContent == 'Detected on configuration') { 
     var style = section[i].getAttribute('style'); 
     style += ' display:none;'; 
     section[i].setAttribute('style', style); 
     break; 
    } 
} 
} 

var tables = document.getElementsByTagName('table'); 
for(var i in tables) { 
try{ 
    var styleNode = tables[i].attributes.getNamedItem('style'); 
    var style = styleNode.firstChild.nodeValue; 

    if(style == null) { 
     continue; 
    } 

    if(style.match('top: 434px; left: 120px;') 
    || style.match('top: 461px; left: 120px;') 
    || style.match('top: 434px; left: 456px;') 
    || style.match('top: 461px; left: 456px;')){ 
     style += ' display:none;'; 
     styleNode.firstChild.nodeValue = style; 
     tables[i].setAttribute('style', style); 
    } 
} catch(err){continue;} 
} 


var labels = document.getElementsByTagName('label'); 
for(var i in labels) { 
try{ 
    var styleNode = labels[i].attributes.getNamedItem('style'); 
    var style = styleNode.firstChild.nodeValue; 

    if(style == null) { 
     continue; 
    } 

    if(labels[i].textContent == 'OS' 
    || labels[i].textContent == 'App. Server' 
    || labels[i].textContent == 'DBMS' 
    || labels[i].textContent == 'Web Server'){ 
     style += ' display:none;'; 
     styleNode.firstChild.nodeValue = style; 
     labels[i].setAttribute('style', style); 
    } 
} catch(err){continue;} 
} 

var divs = document.getElementsByTagName('div'); 
for(var i in divs) { 
try { 
    var styleNode = divs[i].attributes.getNamedItem('style'); 
    var style = styleNode.firstChild.nodeValue; 

    if(style == null) { 
     continue; 
    } 

    if(style.match('top: 291px; left: 23px;')){ 
     style = style.replace('height: 109px;',''); 
     styleNode.firstChild.nodeValue = style; 
     divs[i].setAttribute('style', style); 

     innerDivs = divs[i].getElementsByTagName('div'); 
     for(var j in innerDivs){ 
      try { 
       innerStyleNode = innerDivs[j].attributes.getNamedItem('style'); 
            innerStyle = innerStyleNode.firstChild.nodeValue; 

            if(innerStyle == null) { 
          continue; 
          } 

            if(innerStyle.match('width: 665px;') 
             && innerStyle.match(' height: 109px;')){ 
        innerStyle = innerStyle.replace('height: 109px;',''); 
        innerStyle += ' font-size: 130%'; 
        innerDivs[j].setAttribute('style', innerStyle); 
       } 
      } catch(err){continue;} 
     } 
    } 
} catch(err){continue;} 
} 

看到壁虎的DOM,如果你有发现什么是可供利用的问题,等等。