【发布时间】:2011-09-01 05:43:39
【问题描述】:
我编写了一个脚本来更改设计不佳页面上的 CSS 元素。我一直在 Chrome 中对其进行测试,现在它功能齐全。我已将它放在网络上,现在将其用作书签文本:
javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://myexamplewebsite.com/files/adjustPage.js';})();
它在 Chrome 上完美运行,但在最新的 Firefox 或 IE 中无法运行。我查看了 Chrome 和 Firefox 的错误控制台,没有人抱怨任何问题。当我将代码放入错误控制台的“代码”字段并进行评估时,Firefox 也没有反应。
Firefox 确实在底部的小状态栏中说:“Read myexamplewebsite.com”,但没有别的。
这不是我在上面运行的小书签代码的怪癖,因为我在同一台服务器上放置了一个“hello world”脚本,它运行良好。
有没有更好的方法来找出 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;}
}
【问题讨论】:
-
看起来不错,但可能与您的检查不匹配;)也许这是计算出来的样式,而不是 html plage 提供的样式:)我们需要触发此脚本的页面跨度>
-
好点。我看到页面上的尺寸都是绝对的,并假设它们会被硬编码(页面那个不好)。我无法共享该页面,因为它是一个专有系统(ClearQuest 错误跟踪系统),但是您对我可以查找的内容有什么建议吗?
标签: javascript firefox bookmarklet