SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript modify HTML - Greasemonkey

    Trying to scrap together a script to use with grease monkey.

    When using firefox and viewing one of MS's sites, I come across the html of:
    >
    Which is suposed to display > but instead it just displays as >.

    I'd like to write a script to go through and replace > with just >.

    There are also others l'd like to check for " to ", etc..

    Anyone know how to go about this? Sample script?

    Will the below function work? How do I send the html to it for editing?

    function htmlspecialchars(str) {
    str = str.replace('&, '&' );
    return str;
    }

    Thanks in advance!
    Andy

  2. #2
    SitePoint Enthusiast Gridlight's Avatar
    Join Date
    Apr 2005
    Location
    Oxford, UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you want this to work? Do you want to permanently change the html files and re-save? This would then be better done on the desktop, rather than as JS, as JS can't manipulate files.
    If you just want to dump some JS at the top of an HTML page so that it'll make the changes temporarily (which seems a little pointless???) you'd have to loop thorugh your containing elements, get the innerHTML do the search and replace, and then re-insert it into the parent element.
    If you have loads of files and want to change them all, then like I said, you shoulf do this on the desktop, but if for some reason this isn't possible, you could wright something using a server-side language to edit your HTML files...???
    Cheers,
    Gridlight

    BrowserCam for 30?! I'm in, are you?

  3. #3
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gridlight, greasemonkey is a Firefox extension that modifies any HTML files the browser recieves. I've never written a greasemonkey script, tho I intend to in the future.

    avkb03, this should work: str = str.replace(/&((?:gt|lt);)/g, '&$1');
    Last edited by Maian; May 18, 2005 at 19:12.

  4. #4
    SitePoint Member
    Join Date
    May 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maian, awesome, thanks.

    Now how do I go about modifying it at the html level?

    Thanks,
    Andy

  5. #5
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I looked up the greasemonkey documentation. Try this:

    Code:
    (function() {
      window.addEventListener("load", function(e) {
        document.body.innerHTML = document.body.innerHTML.replace(/&((?:gt|lt);)/g, '&$1');
      }, false);
    })();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •