简体   繁体   English

书签无法在Firefox中使用

[英]Bookmarklet doesn't work in Firefox

I've written a script that changes the CSS elements on a badly designed page. 我编写了一个脚本,用于更改设计不良的页面上的CSS元素。 I was testing it in Chrome as I went along, and now it's fully functional. 我一直在Chrome中对其进行测试,现在它已经可以正常使用了。 I've placed it on the web and am now using this as the bookmarklet text: 我将其放置在网络上,现在将其用作书签文本:

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

It works perfectly on Chrome, but doesn't work in the latest Firefox or IE. 它可以在Chrome上完美运行,但不能在最新的Firefox或IE中运行。 I've looked in the error consoles of Chrome and Firefox and neither one complains of any issues. 我在Chrome和Firefox的错误控制台中查看过,但没人抱怨任何问题。 Firefox also doesn't react when I put the code in the 'code' field of the error console and evaluate. 当我将代码放入错误控制台的“代码”字段并进行评估时,Firefox也没有反应。

Firefox does say in the little status bar at the bottom: "Read myexamplewebsite.com", but nothing else. Firefox确实在底部的小状态栏中说:“阅读myexamplewebsite.com”,但仅此而已。

It isn't a quirk in the bookmarklet code I'm running above, because I've placed a 'hello world' script on the same server and it worked fine. 这不是我上面运行的书签代码的怪癖,因为我已经在同一台服务器上放置了“ hello world”脚本,并且效果很好。

Is there a better way to work out what Firefox/IE don't like about my script? 有没有更好的方法来解决我的脚本不喜欢Firefox / IE的问题?

If you're interested, the code I'm running is here. 如果您有兴趣,我正在运行的代码在这里。 Please excuse the ugliness of the code, it was a very quick and dirty hack: 请原谅代码的丑陋之处,这是一个非常快捷和肮脏的技巧:

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

innerText is a nonstandard property (so far; there are now efforts to create a standard for it) which is not supported in Gecko. innerText是非标准属性(到目前为止;现在正在努力为其创建标准),在Gecko中不受支持。 Does using textContent instead fix things for you? 使用textContent是否可以为您解决问题?

Here is the fixed code, that works on Chrome, Firefox 3.6 and the latest Firefox (as of Aug 2011). 这是固定的代码,适用于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;}
}

See the Gecko DOM if you're having issues finding out what's available for use, etc. 如果您在查找可用内容等方面遇到问题,请参阅Gecko DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM