/    Sign up×
Community /Pin to ProfileBookmark

Need help to find element tag combination and delete certain parts

1.) Assume I have a (HTML) page which contains none, one or more occurencies of


<p>
….
<br>
<br>
</p>

If a <br> tags follows another, second <br> element with only whitespaces in between
then BOTH <br> tags should be deleted.

How can I perform this replacement/deletion with javascript?

2.) Assume I have a (HTML) page which contains none, one or more occurencies of

….
<h2>….</h2>
<br>

Similar to 1.) when the closing tag </h2> is followed by a <br> (only whitespaces are allowed between) element, then the <br> tag should be deleted.

How can I perform this replacement/deletion with javascript?

Thank you
Peter

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@rootJun 22.2014 — To be clear, JavaScript can not remove those elements from HTML code directly only what exists in the DOM tree on load. This means if the HTML code that the web browser requests has those elements in the page, having javascript remove them on every page load is pointless, simpler to edit the HTML document in the first instance.
Copy linkTweet thisAlerts:
@psteinauthorJun 22.2014 — To be clear, JavaScript can not remove those elements from HTML code directly only what exists in the DOM tree on load. This means if the HTML code that the web browser requests has those elements in the page, having javascript remove them on every page load is pointless, simpler to edit the HTML document in the first instance.[/QUOTE]

Yes, I know, this all.

However I have no access to HTML source.

So I need to modify the HTML in loaded browser by DOM Javascript.

If its easier for you then think of a corresponding XML document.

I just need the suitable JS cmds.
Copy linkTweet thisAlerts:
@rootJun 22.2014 — Sorry, that doesn't track, if you are running a script in a HTML page then you do have access to the HTML code.
Copy linkTweet thisAlerts:
@xelawhoJun 22.2014 — Sorry, that doesn't track, if you are running a script in a HTML page then you do have access to the HTML code.[/QUOTE]
well, not quite. one scenario would be that pstein might be loading a userscript, in which case the only way to modify the html would be through javascript.

you could try something like this:
[CODE]
document.body.innerHTML=document.body.innerHTML.replace(/<br>n<br>/g,"").replace(/</h2>n<br>/g,"</h2>");
[/CODE]
Copy linkTweet thisAlerts:
@psteinauthorJun 23.2014 — 
you could try something like this:
[CODE]
document.body.innerHTML=document.body.innerHTML.replace(/<br>n<br>/g,"").replace(/</h2>n<br>/g,"</h2>");
[/CODE]
[/QUOTE]


Thank you, but two more questions:

1.) When I look at

replace(/</h2>n<br>/g,"</h2>");

Shouldn't the forward slash be marked too like in:

replace(/</h2>n<br>/g,"</h2>");

?

2.) Looking at

replace(/<br>n<br>/g,"")

I guess n represents only ONE page break. What about other white spaces like blanks+tabs?

What about if more than one page break occurs? Wouldn't

replace(/<br>s*<br>/g,"")

better? Or is

replace(/<br>s<br>/g,"")

needed?


@

.





@ . : Have a look at great Greasemonkey plugin tool for Firefox.

Wit hthat tool you can apply CSS and javascript to webpages AFTER loading!
Copy linkTweet thisAlerts:
@xelawhoJun 23.2014 — 
  • 1. no - because /</h2>n<br>/g is a regular expression whereas "</h2>" is a string


  • 2. yes, you will have to tweak it to suit your specific needs
  • ×

    Success!

    Help @pstein spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...