开发者

Possible to refresh site until change with Javascript?

开发者 https://www.devze.com 2023-02-10 10:13 出处:网络
I am a relatively new coder and have been searching around a bit on here and on google before asking but I have come up empty.

I am a relatively new coder and have been searching around a bit on here and on google before asking but I have come up empty.

I was 开发者_运维百科wondering if there was a way to create a script in say Javascript, and have it refresh a page until there is a change on the page by inputting the page into a string.

Something along the lines of (excuse the soddy psuedo-code but I realise that some functions would need to be written around this):

Start

currentPage = webclient.getPage("www.somesite.com")
boolean diff = false
string pageText = currentPage.astext()

do {
   currentPage.refresh()
} until (currentPage.astext() != pageText)

string alert = "Change Found"
string address = "me@somesite.com"
e-mail(address,alert)

END

Thanks for any help anyone can offer a new coder on this :)


PHP seems better suited for this kind of operation. Here is what I would do:

  • Fetch the page content with cURL
  • Wait a bit (e.g. 1 min) *
  • Fetch the page content again
  • Compare both page contents
  • e-mail if there is a change and start over anyway

* You don't want to refresh the page as much as your pseudo-code does, as your script would eat a lot of bandwidth and would be most likely to saturate your targeted website.

Do you need code samples ?

EDIT
Here's my working PHP script:

<?php
////////////////
// Parameters //
////////////////
$url = 'http://www.example.com';
$sleepyTime = 60; // seconds
$recipient = 'xxx@xxx.xx';
$subject = 'Change found';
$message = 'Change found in ' . $url;

////////////////
// Functions  //
////////////////
function fetchWebsiteContent($url) {
    // init curl handle
    $ch = curl_init($url);

    // Tells curl to return the content
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // fetch content
    $res = curl_exec($ch);

    // close handle and return content
    curl_close($ch);
    return $res;
}

////////////////////
// The comparison //
////////////////////
$firstContent = fetchWebsiteContent($url);

// This is an endless checking scope
while (1) {
    // sleep a bit and fetch website content again
    sleep($sleepytime);
    $secondContent = fetchWebsiteContent($url);

    // check if change occured
    if ($firstContent == $secondContent) {
        mail($recipient, $subject, $message);
    }

    $firstContent = $secondContent;
}
?>

Helpful ressources:
cURL manual
mail manual

Hope you like it ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号