remove html comment?

Subscribe to remove html comment? 10 posts, 4 voices

 
gelat User

I use a site that has invalid comments like this <!-----------blah-------> instead of <!-- blah -->

firefox wont display the page properly. Opera and IE will.

How can I write a script to remove/change those extra ----'s ?

Seems like all code I write ignores comment sections.

I tried this:

(function() {
var replacements, regex, key, textnodes, node, s;

replacements = {

"----" : "--",
"---" : "--",

};

regex = {};
for (key in replacements) {
regex[key] = new RegExp(key, 'g');
}

textnodes = document.evaluate( "//body//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
s = node.data;
for (key in replacements) {
s = s.replace(regex[key], replacements[key]);
}
node.data = s;
}

})();

 
Pnoexz Scriptwright

You should contact them and tell them that you cant see the page properly because of that. They will mostlikely fix it theirselves.

 
gelat User

They won't. I need to adapt on my own. the official policy is IE only

 
lucideer Scriptwright

Well.. firstly <!-----------blah-------> is a perfectly valid comment and doesn't display in Firefox (when I tested it anyway - FF3 - do correct me if I'm wrong)
Also.... Opera treats all comments according to xhtml spec. The firefox difference is that they follow the SGML spec for some odd reason.
Either way, a link to the site, or posting some of it's code would be a great help as I'm not sure what the problem comments look like.

On your code above, Firefox doesn't store the comment markup in the DOM, only it's contents, so you'd really need to do some kind of regEx replacement on document.body.innerHTML

 
gelat User

I cant post the site, as its a secure login thing. the exact comments are:
<!-----------------------------------BEGIN SECTION I-------------------------------------><!-----------------------------------END SECTION I-------------------------------------><!-----------------------------------BEGIN SECTION VI------------------------------------><!-----------------------------------END SECTION VI------------------------------------->

each of these are basically commenting out whole sections of code, as firefox is reading the next two dashes as another comment.

I am new to greasemonkey, and have no idea on how to use innerHTML - can you assist?

 
Mikado Scriptwright

Well, these comments are valid (as lucideer have mentioned) and can not comment out anything. Try yourself if you don't believe us.

visible text 1<br><!-----------------------------------BEGIN SECTION I------------------------------------->visible text 2<br><!-----------------------------------END SECTION I------------------------------------->visible text 3<br><!-----------------------------------BEGIN SECTION VI------------------------------------>visible text 4<br><!-----------------------------------END SECTION VI------------------------------------->visible text 5

The issue must be somewhere else, and nobody could tell where unless you provide us access to that page in some way.

PS

lucideer wrote:

Firefox doesn't store the comment markup in the DOM

That's wrong, you can access comments in DOM, as DOM Inspector clearly shows.

 
lucideer Scriptwright

@Mikado
I know you can access the contents of comments in the DOM, what I meant is that the DOM does not return the surrounding markup (the dashes, brackets and !)
For example if you do: alert(someCommentNode.outerHTML);, it returns undefined, etc. So in order to replace those characters, you would need the innerHTML of a parent node.

Edit No wonder it returns undefined. There is no outerHTML in Firefox... oh well. Either way, you get my point..

 
gelat User

so could I get an example of code to remove those 4 lines entirely? I know they are the cause, b/c if I view source in firefox, the syntax highlighting comments everything out after that comment line

 
lucideer Scriptwright

I've copied and pasted those lines into random pages and put them in Firefox. No effect on any of them. What I'm guessing is happening, is the code before or after them is effecting the rendering in some way that simply makes it appear as though it's those comments. If you can't tell us the page, maybe you could try posting what's immediately above and below those comments.
Alternatively you could try using the DOM Inspector to delete the comments from the source manually, just to see if that changes anything.

var comments='<!-----------------------------------BEGIN SECTION I-------------------------------------><!-----------------------------------END SECTION I-------------------------------------><!-----------------------------------BEGIN SECTION VI------------------------------------><!-----------------------------------END SECTION VI------------------------------------->';

document.body.innerHTML=document.body.innerHTML.replace(comments,'');

That would remove the comments, but as I said I doubt it's your problem.

 
gelat User

that did it lucideer, thanks