Bookmarklet在Firefox中不起作用

时间:2011-08-19 11:01:34

标签: javascript firefox bookmarklet

我编写了一个脚本,用于更改设计糟糕的页面上的CSS元素。我一直在Chrome中测试它,现在它完全正常运行。我把它放在网上,我现在使用它作为书签文字:

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

它在Chrome上完美运行,但在最新的Firefox或IE中无效。我查看了Chrome和Firefox的错误控制台,没有人抱怨任何问题。当我将代码放在错误控制台的“代码”字段中并进行评估时,Firefox也没有做出反应。

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

我在上面运行的bookmarklet代码中并不是一个怪癖,因为我在同一台服务器上放置了一个'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;}
}

2 个答案:

答案 0 :(得分:3)

innerText是一个非标准属性(到目前为止;现在正在努力为它创建一个标准),这在Gecko中是不受支持的。是否使用textContent为您解决问题?

答案 1 :(得分:0)

以下是固定代码,适用于Chrome,Firefox 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;}
}

如果您在查找可供使用的内容时遇到问题等,请参阅Gecko DOM。