Букмарклет не работает в Firefox - PullRequest
1 голос
/ 19 августа 2011

Я написал скрипт, который изменяет элементы CSS на плохо оформленной странице.Я тестировал его в Chrome, и теперь он полностью функционален.Я разместил его в Интернете и теперь использую его в качестве текста букмарклета:

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

Он отлично работает в Chrome, но не работает в последних версиях Firefox или IE.Я посмотрел в консоли ошибок Chrome и Firefox, и ни один из них не жалуется на какие-либо проблемы.Firefox также не реагирует, когда я помещаю код в поле «code» консоли ошибок и выполняю оценку.

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;}
}

Ответы [ 2 ]

3 голосов
/ 20 августа 2011

innerText - это нестандартное свойство (пока; сейчас предпринимаются попытки создать для него стандарт), которое не поддерживается в Gecko. Используете ли вместо вас textContent что-то исправляет?

0 голосов
/ 01 сентября 2011

Вот исправленный код, который работает на Chrome, Firefox 3.6 и последней версии Firefox (по состоянию на август 2011 г.).

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, если у вас возникли проблемы с выяснением того, что доступно для использования и т. Д.

...