/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] HTML grabber – it works, but only once

Here’s some code that goes in a frame that sits in a window with another frame called “GameScreen”. The point of my code is that clicking a button will fill the text area with the body HTML of the GameScreen frame. And it does- the first time you do so. After that, nothing happens when you click the button.

Actually, it seems that it will only even show the HTML of the page that initially loads (via the parent window) into the GameScreen frame, and nothing happens if I click the button after follwing any links in the GameScreen frame.

Any help as to what the problem is?

(For the curious, this is just a testbed for a more complicated project that will actually do something with the info I’m just grabbing and stuffing in the textbox right now. But what I want now is to be sure that I can surf along in the GameView frame, then use the “Grab Game Screen HTML” button whenever I want to to see the body HTML from the GameScreen frame.)

[CODE]
<html>
<head>
<title>title</title>
<script language=”javascript” type=”text/javascript”>
function GrabGamescreenHTML() {
showtext = window.parent.GameFrame.document.body.innerHTML
document.Grabby.ShowsGameScreenHTML.value = showtext;
}
</script>
</head>

<body>
<form name=”Grabby”>
<textarea name=”ShowsGameScreenHTML” cols=”80″ rows=”5″>
Game Screen HTML should show up here when you click “take screenshot”
</textarea>
</form>
<p><button name=”GrabIt” type=”button”>onClick=”GrabGamescreenHTML();”>Grab Game Screen HTML</button>
</body>
</html>
[/CODE]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@SwiersauthorJun 03.2007 — Some more info- it seems that when the problem arises, I'm getting the following in the Error Console:

Error: uncaught exception: Permission denied to get property HTMLDocument.body

Is that what is preventing this from working? Why (and how) would access to the HTML code be blocked in this manner, when its already downloaded and sitting on your machine, and very easy to view in other ways? Is there some (code based) work-around for this, or an alternative method I can use to achieve the same result?
Copy linkTweet thisAlerts:
@SwiersauthorJun 03.2007 — Huh, a search for "HTMLDocument.body" on google turned up some interesting stuff. Apparently Firefox considers what I'm doing to be a security risk- its called "cross domain scripting" and it can be used to set up Phishing sites. So just because the DOM supports it, doesn't mean javascript can do it (not in my browser of choice, at least).

And apparently there isn't even an option to allow cross domain scripting. Which is at the very core of what I'm trying to do- I want to grab / send information from one domain (the game) over to another (my communications website).

This is pretty sad- I (and some other people) were really psyched on this project.
Copy linkTweet thisAlerts:
@PhilDogJun 04.2007 — Huh, a search for "HTMLDocument.body" on google turned up some interesting stuff. Apparently Firefox considers what I'm doing to be a security risk- its called "cross domain scripting" and it can be used to set up Phishing sites. So just because the DOM supports it, doesn't mean javascript can do it (not in my browser of choice, at least).

And apparently there isn't even an option to allow cross domain scripting. Which is at the very core of what I'm trying to do- I want to grab / send information from one domain (the game) over to another (my communications website).

This is pretty sad- I (and some other people) were really psyched on this project.[/QUOTE]


  • 1. First of all, that DOM object is only there for the intend use of browsers from the same domain & sub-domain to work.


  • 2. You could use server side scripting to get the code, however, you would be stealing bandwidth from another website, and using their content without their permission. If they wanted you to use part of there site for your own, you would be able to do it.
  • Copy linkTweet thisAlerts:
    @SwiersauthorJun 04.2007 — 
  • 1. So it seems. However, the books I read about javascript and the DOM never mentioned that limitation. And its pretty rare that ANYTHING intended for use on the WWW gets used only for its "intended use", eh?


  • 2. I've had contact with the designer of the game website, and have full and documented permission to use the content in this manner. He's pretty supportive of the idea, in fact. AFAIK, its not that he's blocking it in any way- it seems I'd have the same problem with ANY website, even one that I wrote myself that had a separate domain.

    What sort of server side scripting do you mean? If you mean essentially having my site run as a proxy so that the code gets sent there, and from there to the users, I don't think that would work. It wouldn't cost the game website any extra bandwidth, but it would likely cost me a great deal of of extra bandwidth. And for various reasons most players are limited to 150 hits per IP address. So either my site would be limited to those same 150 hits, or (if I got the site designer to lift that restriction for my IP or found some work around) it would serve as a way for folks to bypass that limit. Neither is acceptable.
  • Copy linkTweet thisAlerts:
    @rootJun 04.2007 — ... the DOM supports it, ...[/QUOTE]
    [b]D[/b]ocument [b]O[/b]bject [b]M[/b]odel is another Jargonese word which is Javascript at the end of the day.
    Copy linkTweet thisAlerts:
    @SwiersauthorJun 05.2007 — [url=http://www.w3.org/DOM/#what]According to the W3C[/url], "the Document Object Model is a platform- and language-neutral interface." Which sounds to me like it goes beyond the bounds of javascript, although I suppose that may be the only way people can currently take advantage of that interface without some serious low level programing.

    Anyhow, it looks like "bookmarklets" (which are just hypertext links that make a javascript call instead of an http call) will do the trick. I hardly even need to change my code.
    Copy linkTweet thisAlerts:
    @steveaJun 05.2007 — If I understand what you'd like this script to do I believe a multi-language approach would be able to do what you want.

    Using javascript to get the src of the frame that contains the page you'd like the source of, you could pass the url to a server side script (Perl, PHP, etc.) then run some jiggery-pokery on that url (I believe LWP::UserAgent in Perl could do this) and return the HTML to Javascript via an XMLHttpRequest object.

    I'm not much of an AJAX guy but I think that would accomplish what you're looking to get done.
    Copy linkTweet thisAlerts:
    @SwiersauthorJun 05.2007 — Yes, that's exactly what I'm planning. The javascript will grab the HTML and pass it (via a form) to a PHP based sight that will do various fun data-base type things. I don't think what I'm planning counts as ajax though- its not really any more complex than a blog or forum that uses client side JS and server side PHP to each do seprate tasks they are well suited for.

    But without the javascript, I was hosed. And a javascript page can not grab code from another domain. However, those bookmarklets are just the thing I need- even the games designer has responded to suggest them.

    Bookmarklets really rock- I suggest anybody who works with Javascript should learn about them. Not only are they useful on your own, but they are so simple to make and use, and so flexible, that I expect they can hugely reduce development time (or speed up learning) when you are putting together any reasonably simple function, especially ones that interact with the document objects. I find that I'm constantly playing "poke and try" when doing that, and the more times I can poke, and the more times I try, the more and faster I learn.
    ×

    Success!

    Help @Swiers 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.17,
    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,
    )...